Community Tip - New to the community? Learn how to post a question and get help from PTC and industry experts! X
I had a use case where I needed to add to an Infotable property by parsing the contents of a CSV file. For the rest of the community, I thought it might be useful to share the code that I used to make sure the changes persisted in the Infotable property.
//// Begin example
// Read in the CSV file using the standard snippet:
var params = {
longitudeField: undefined /* NUMBER /,
fieldDelimiter: undefined / STRING /,
path: CSVName / CSV filename -- STRING /,
dateFormat: "MM/dd/yyyy HH:mm:ss" / STRING /,
stringDelimiter: undefined / STRING /,
dataShape: "ResultsDS" / DATASHAPENAME /,
columnMappings: undefined / STRING /,
hasHeader: true / BOOLEAN /,
latitudeField: undefined / NUMBER /,
fileRepository: "DemoRepository" / THINGNAME /
};
// result: INFOTABLE
var result = Resources["CSVParserFunctions"].ReadCSVFile(params);
// Now loop through and record to Infotable property:
var tablelength = result.getRowCount();
for (var x = 0; x < tablelength; x++) {
var row = result.getRow(x);
// ResultsDS entry object
var newEntry = new Object();
newEntry.TimeStamp = row.TimeStamp; // DATETIME
newEntry.Parameter1 = row.Parameter1; // NUMBER
newEntry.Parameter2 = row.Parameter2; // NUMBER
newEntry.Parameter3 = row.Parameter3; // NUMBER
newEntry.Index = row.Index; // STRING - isPrimaryKey = true
me.MyInfoTableProperty.AddRow(newEntry);
};
// The following code will allow the additions to the infotable property to persist across a shutdown.
var TABLE = me.MyInfoTableProperty;
/ CORRECTION: The following code is not needed when doing it this way:
TABLE.AddRow({
TimeStamp : TimeStamp,
Parameter1 : Parameter1,
Parameter2 : Parameter2,
Parameter3 : Parameter3,
Index: Index
});
*/
me.MyInfoTableProperty = TABLE;
//// End example
It works, but if anyone has suggestions to improve it let me know.
Thanks Jeffrey,
I just edited my original post with a correction (see comment toward the end of the script). If those lines of code are used at that point and in that manner in the script, the infotable will not persist. One would use that technique only if using .AddRow() directly to the temporary table in-line with the main script body, not after as I originally had it.