Hi,
I am trying to generate a CSV file with all of the properties of the thing (stored in a valuestream).
I am using the example code for WriteCSV file given in PTC as a service in a thing and a CSV file is being generated with the headers from the DataShape. However, I am not getting the values for the respective properties in the CSV file. The code is as follows and I would like to know what changes are to be made (or a new example) in order to populate the rows with the corresponding values from the value stream.
Thanks!
Best regards,
Tushar
var params = {
infoTableName : "result",
dataShapeName : "Test4_DataShape"
};
// CreateInfoTableFromDataShape(infoTableName:STRING("InfoTable"), dataShapeName:STRING):INFOTABLE(ds1)
var result = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape(params);
var newRow = new Object();
newRow.firstfield = "Speed";
newRow.secondfield = "Distance";
//infotable1Object.rows[0] = newRow;
result.AddRow(newRow);
var params2 = {
path: "test3.csv" /* STRING */,
data: result /* INFOTABLE */,
fileRepository: "SystemRepository" /* THINGNAME */,
withHeader: true /* BOOLEAN */
};
// no return
var result = Resources["CSVParserFunctions"].WriteCSVFile(params2);
Solved! Go to Solution.
Hey Pai,
Thanks for the info. It works now. I passed the properties in the newObject() through result = Thing[ThingName].QueryPropertyHistory(newObject) before invoking the writeCSV service. The CSV file with all the property values is generated now.
Thanks!
What are you getting in the csv output?
Hey Pai Chung,
I am getting an empty csv file with the first row(headers) as the property values of the selected datashape. The code mentioned above has been directly taken from the support page of PTC for CSV Parser
When you comment out the last line and set the service output to InfoTable do you see the appropriate infotable being returned when running this service?
Hi Pai,
After following your above-mentioned suggestion, I am getting an InfoTable with all the column names but still no corresponding property values in the columns.
It's just an empty table.
this code is wrong
var newRow = new Object();
newRow.firstfield = "Speed";
newRow.secondfield = "Distance";
//infotable1Object.rows[0] = newRow;
result.AddRow(newRow);
Change to:
var newRow = new Object();
newRow.Speed= 23;
newRow.Distance= 129;
//infotable1Object.rows[0] = newRow;
result.AddRow(newRow);
I'm assuming your datashape has the fields defined as numbers. (and uses Speed (capital S) etc )
Hi Pai,
Thanks for pointing out the changes, I understood what I was doing wrong and I am getting the values which I entered manually and through my thing's property such as by using me.Speed and me. Distance.
However, I am only getting one row and every time the service is executed I am just getting the recent values of the properties and the previous values are nowhere to be seen in the CSV file.
Since I want to have a csv file with the whole value stream (including the historic values), how can that be accomplished?
Thanks!
Use QueryPropertyHistory and output that to CSV
There is a number of QueryPropertyHistory queries that are used to retrieve values from the valuestream
Hey Pai,
Thanks for the quick response.
I know about the Query services and I am using them to view the results but they only provide the option to execute. I am a little confused as to how to output that data. That's why I chose the CSV parser extension as I thought that it will create the csv file with the value stream entries (historic as well) by making it as a service
Could you please elaborate on the steps of csv creation from the QueryPropertyHistory.
Thanks!
Every service has the ability to output whatever is assigned to result
just like before you were getting that infotable.
if you do result = Thing[ThingName].QueryPropertyHistory
result now represents a table of information (InfoTable) that either is the output of the service and can be displayed on a mashup and mapped to widgets.
or you can take that result and now invoke the writeCSV to send that to CSV
no different from what you are already doing, except the 'payload' the table of information is generated differently.
Hey Pai,
Thanks for the info. It works now. I passed the properties in the newObject() through result = Thing[ThingName].QueryPropertyHistory(newObject) before invoking the writeCSV service. The CSV file with all the property values is generated now.
Thanks!