in one of my solutions I needed to export data to certain files, with a specific field-order, and field-seperator.
Because the export-plugin won't allow us to do scriptable export (yet), I made a method of my own.
make a method, called: createExportFile
- Code: Select all
var fieldnames = arguments[0] // boolean, export with ColumnNames at top
var seperator = arguments[1] // string, choose seperator, for example: "\t" for TAB "," or ";"
var quotes = arguments[2] // boolean, ColumnNames with quotes
var columnnames = arguments[3] // string, ColumnNames seperated by: ";"
var exportfile = "" // create a text variable
// set the variable insertquotes to " or nothing
if (quotes == true) var insertquotes = '"'
else var insertquotes = "";
// split the columnames into an Array
var columnArray = columnnames.split(";")
// determine how many columns are used
var columnCount = columnArray.length
// if fieldnames is true than place the ColumnNames at top
if(fieldnames == true)
{
for(var x=0;x<= columnCount-1;x++)
{
if(x != columnCount-1) exportfile += columnArray[x] + seperator
else exportfile += columnArray[x]
}
// make an 'Enter'
exportfile += "\n"
}
// loop thru the foundset
for(var i=1;i<= foundset.getSize();i++)
{
var record = foundset.getRecord(i);
for(var x=0;x<= columnCount-1;x++)
{
if(record[columnArray[x]])
{
if(x != columnCount-1) exportfile += insertquotes + record[columnArray[x]] + insertquotes + seperator
else exportfile += insertquotes + record[columnArray[x]] + insertquotes
}
else
{
if(x != columnCount-1) exportfile += insertquotes + seperator
else exportfile += insertquotes
}
}
exportfile += "\n"
}
//open dialog, with a suggested filename
var save = application.showFileSaveDialog('file_export.mer')
if(save != null)
{
var success = application.writeTXTFile(save,exportfile);
if (success)
{
plugins.dialogs.showInfoDialog('Info', 'The File is saved!','OK');
}
else
{
plugins.dialogs.showErrorDialog('Warning', 'The File is NOT saved because of an error!','OK');
}
}
Here is an example how you can call the method:
- Code: Select all
//first argument: boolean, export with ColumnNames at top
//second argument: string, choose seperator, for example: "\t" for TAB "," or ";"
//third argument: boolean, ColumnNames with quotes.
//fourth argument: string, ColumnNames seperated by: ";"
createExportFile(true,"\t",true,"customerid;companyname");
You even can use related fields, for example:
- Code: Select all
createExportFile(true,"\t",true,"customerid;companyname;company_to_adresses.adres");
The method is pretty FAST. In my example it exported 2600 records in about 4 seconds.
BTW: because I am using new functions in Servoy 2.1, above only works in this version!
ENJOY!