in one of my solutions I needed to import data to certain files, with a specific field-order, and field-seperator, etc..etc..
Because the import-plugin won't allow us to do scriptable import (yet), I made a method of my own.
make a method, called: importFile
- Code: Select all
var fieldnames = arguments[0] // boolean, first row contains fieldnames
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 filename = arguments[4] // string, the file to be imported.
var fileContents = application.readTXTFile(filename)
// split the columnames into an Array
var columnArray = columnnames.split(";")
// determine how many columns are used
var columnCount = columnArray.length
//put each line into an array item
var fileLines = fileContents.split("\n");
//determ if the first line has fieldnames, which should not be imported
if(fieldnames == true) var begin = 1
else var begin = 0
//fileLines is now an array with 1 item per line
//now loop through the array to get to the cells
for ( i = begin ; i < fileLines.length ; i++ )
{
foundset.newRecord();
var cellsArray = fileLines[i].split(seperator);
for (var x = 0 ; x < columnCount ; x++)
{
var index = foundset.getSelectedIndex()
var record = foundset.getRecord(index)
if(cellsArray[x]) //check if cellArray is not empty or null
{
var column = cellsArray[x]
var columnlength = column.length
if(quotes == true)
{
var a = column.substring(1,columnlength - 1)
record[columnArray[x]] = a
}
}
}
}
make another method: startImportFile
- Code: Select all
//first argument: boolean, first row contains fieldnames
//second argument: string, choose seperator, for example: "\t" for TAB "," or ";"
//third argument: boolean, ColumnNames with quotes.
//fourth argument: string, ColumnNames seperated by: ";"
//fifth argument: string, the file to be imported.
importFile(true,";",true,"customerid;companyname","D:/Mijn Documenten/file_export.mer");
If you do some more studying , you could make it work with related-fields, which is not even possible Servoy itself!
Enjoy!