« [Tip] Calculate a week number | Main | [Article] How to create a startup item on OS X »

November 22, 2004

[Tip] A scriptable export

by Harjo Kompagnie
www.directict.nl

Here is a great tip to do your own scriptable export (already posted on the HOWTO forum!)
 
First, make a globalmethod called "createExportFile" with the following code:

var form = arguments[0]            //string, formname
var fieldnames = arguments[1]    // boolean, export with ColumnNames at top
var seperator = arguments[2]    // string, choose seperator, for example:  "\t" for TAB "," or ";"
var quotes = arguments[3]       // boolean, ColumnNames with quotes
var columnnames = arguments[4]   // 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 <= forms[form].foundset.getSize();i++)
{
   var record = forms[form].foundset.getRecord(i);
   for(var x=0; x <= columnCount-1;x++)
   {
      if(record[columnArray[x]])
      {
        utils.stringReplace(record[columnArray[x]],'\n', '')
        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');
   }
}

Now make a global method called "startExportFile" with the following code:

//first argument: give the form name
//second argument: boolean, export with ColumnNames at top
//third argument: string, choose seperator, for example: "\t" for TAB "," or ";"
//fourth argument: boolean, ColumnNames with quotes.
//fifth argument: string, ColumnNames seperated by: ";"
createExportFile('companies_list',true,"\t",true,"customerid;companyname");

ENJOY!

| Posted by IT2Be on November 22, 2004 at 09:47 AM in Tips | Permalink

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/t/trackback/118167/1446914

Listed below are links to weblogs that reference [Tip] A scriptable export:

Comments

Post a comment