Community Tip - Want the oppurtunity to discuss enhancements to PTC products? Join a working group! X
hi all,
I would like to read values from csv file, and count sum of all values in each column.
please look at code below and give me answer - why commented row is wrong??
(The goal is to create dynamic dataShape with dynamic names of its column.)
var fileRepository = Things["SystemRepository"];
var fileContent = fileRepository.LoadText({
path: "suma.csv" }
);
var rows = fileContent.split("\n");
var header = rows[0].split(",");
var columnsQty = header.length;
var myInfoTable = { dataShape: {fieldDefinitions : { } }, rows: [] };
var abc = 0;
var header_Sum;
for ( var i=0; i<2; i++) {
header_Sum = "Sum_" + i;
myInfoTable.dataShape.fieldDefinitions[header_Sum]={
name: header_Sum,
baseType: "NUMBER"
}
abc = abc + parseFloat(rows[i]);
};
//myInfoTable.rows.push({header_Sum:abc});
myInfoTable.rows.push({Sum_0:abc});
result = myInfoTable;
thx
gucio
Solved! Go to Solution.
Build the row object before:
var newRow = {}; newRow[""+header_Sum] = abc; myInfoTable.rows.push(newRow);
Better you got with Infotable object instead of JSON infotable.
var fileRepository = Things["SystemRepository"]; var fileContent = fileRepository.LoadText({ path: "suma.csv" } ); var rows = fileContent.split("\n"); var header = rows[0].split(","); var columnsQty = header.length; /*var myInfoTable = { dataShape: {fieldDefinitions : { } }, rows: [] }; */
// -- Instead you should use the corresponding snippet:
var myInfoTable = Resources["InfoTableFunctions"].CreateInfoTable();
var abc = 0; var header_Sum; for ( var i=0; i<2; i++) { header_Sum = "Sum_" + i; /* myInfoTable.dataShape.fieldDefinitions[header_Sum]={ name: header_Sum, baseType: "NUMBER" }
*/
// -- Instead you should use the corresponding snippet
myInfoTable.AddField({ name: header_Sum, baseType: "NUMBER" }); abc = abc + parseFloat(rows[i]); }; //myInfoTable.rows.push({header_Sum:abc}); /* myInfoTable.rows.push({Sum_0:abc});
*/
// -- Instead you should use the corresponding snippet
myInfoTable.AddRow({ "Sum_0": abc }); result = myInfoTable;
But on your code I don't see it iterating over rows, it's right? You are iterating, if I understand well, over columns (2 columns) but not rows.
hi,
My code was written just to ask how to do it. I couldn't find solution why infoTable was not populated with values when I used "dynamic" names of headers:
//myInfoTable.rows.push({header_Sum:abc}); - doesn't work;
myInfoTable.rows.push({Sum_0:abc}); - works;
Your code works perfectly - thx.
I am at the beginning of the road - so sometimes my questions may look strange, but not all is clear for me at the moment.
regards
use istead:
myInfoTable.rows.push({(""+header_Sum):abc}); - doesn't work;
Typing:
myInfoTable.rows.push({(""+header_Sum):abc});
I receive invalid property id :(
Build the row object before:
var newRow = {}; newRow[""+header_Sum] = abc; myInfoTable.rows.push(newRow);
thx
Hi @Gucio.
If the provided responses answered your question, please mark as Accepted Solution, for the benefit of others who may have the same question.
Regards.
--Sharon