cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Learn all about the Community Ranking System, a fun gamification element of the PTC Community. X

Append to CSV file with CSVParserFunctions WriteCSVFile

bmessenger
7-Bedrock

Append to CSV file with CSVParserFunctions WriteCSVFile

Hi,

Does anybody know how to append to a CSV file? If I use this

var paramsCSV = {

  path: sFile  /* STRING */,

  data: infoTab /* INFOTABLE */,

  fileRepository: "P116SQLXMLFileRepository" /* THINGNAME */,

  withHeader: false/* BOOLEAN */

};

// no return

Resources["CSVParserFunctions"].WriteCSVFile(paramsCSV);

I always get a new file each time. I actually want to append to the file each time. There seems to be no option in the params to tell it to append.

4 REPLIES 4

HI,

I'll answer my own question because I have found a way around it. If I read the data from the existing csv file into an existing datashape, then append the new data to this datashape and then write the resulting datashape out to file it works as an append. This does seem a rather long way to do things considering every language has the ability to append to a file.

SO this is what I have done in the code

//Note I have some XML incoming residing in xmlPage varaible and it is to be written to a CSV file aftedr parsing the XML

//Get an existing file

var sFile = "P116ProdLossesFromSQL.csv";

//Fill the datashape GetSQLProdLossData with data from the existing CSV file

var params4 = {

            path: sFile /* STRING */,

            columnMappings: undefined /* STRING */,

            hasHeader: true /* BOOLEAN */,

            longitudeField: undefined /* NUMBER */,

            dateFormat: "yyyy-MM-dd HH:mm:ss" /* STRING */,

            fileRepository: "P116SQLXMLFileRepository" /* THINGNAME */,

            latitudeField: undefined /* NUMBER */,

            fieldDelimiter: undefined /* STRING */,

            stringDelimiter: undefined /* STRING */,

            dataShape: "GetSQLProdLossData" /* DATASHAPENAME */

};

// result: INFOTABLE

var result = Resources["CSVParserFunctions"].ReadCSVFile(params4);

var xmlPage = me.XMLFileRepoString; ??This is XML that sits in an XML property of the thing

//Append the XML data that has come in to the datashape GetSQLProdLossData

var i = 0;

for each (var rT in xmlPage.tT.rT)

{

    logger.debug("got debug to row " + i);

    i++;

    var dateValue = parseDate(rT.D, "yyyy-MM-dd'T'HH:mm:ss");

    // GetSQLProdLossData entry object

    var newEntry = new Object();

    newEntry.DateAndTime = dateValue; // DATETIME

    newEntry.Val = parseInt(rT.V,10); // INT

    newEntry.Plant = rT.P; // STRING

    newEntry.Millitm = parseInt(rT.M,10); // INT

    newEntry.TagIndex = parseInt(rT.T,10); // INT

    if(rT.S != '')

    newEntry.Status = parseInt(rT.S,10); // INT

    else

        newEntry.Status = -1;

    newEntry.Marker = rT.N; // STRING

    result.AddRow(newEntry);

}

// result: INFOTABLE dataShape: "GetSQLProdLossData"

var infoTab = result;

var paramsCSV = {

  path: sFile  /* STRING */,

  data: infoTab /* INFOTABLE */,

  fileRepository: "P116SQLXMLFileRepository" /* THINGNAME */,

  withHeader: true /* BOOLEAN */

};

// no return

Resources["CSVParserFunctions"].WriteCSVFile(paramsCSV);

Hi Ben,

You can write your own Write CSV File service ( it's just a string with new line feeds \n or \r\n ) and use AppendToTextFile instead ( you will find this service on your FileRepository Thing not on snippets ).

If you don't won't to write your own Write CSV you can also write a temp file with the new data and WriteCSVFile (withHeader:false) and read this new temp file with LoadText( from FileRepository Thing ) and then AppendToTextFile the readed text, then of couse you will have to delete temp file.

Carles.

Thanks Carles. A very good couple of suggestions. Some simple JS could even do the job without resorting to an extension, but an extension would be better. I'll give it a go, because as you say it is not difficult code. I've never written an extension before but the guide seems to be complete enough to get started at least.

We already have TW CSV extension customized for our needs, just decompile the Java and customize per your needs.

Top Tags