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

how to sum values

SOLVED
Amethyst

how to sum values

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

Krzysztof
1 ACCEPTED SOLUTION

Accepted Solutions

Re: how to sum values

Build the row object before:

var newRow = {};
newRow[""+header_Sum] = abc;
myInfoTable.rows.push(newRow);

 

View solution in original post

7 REPLIES 7

Re: how to sum values

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.

 

Re: how to sum values

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

 

Krzysztof

Re: how to sum values

use istead:

myInfoTable.rows.push({(""+header_Sum):abc}); - doesn't work;

Re: how to sum values

Typing:

myInfoTable.rows.push({(""+header_Sum):abc}); 

I receive invalid property id :(

Krzysztof

Re: how to sum values

Build the row object before:

var newRow = {};
newRow[""+header_Sum] = abc;
myInfoTable.rows.push(newRow);

 

View solution in original post

Re: how to sum values

thx

Krzysztof

Re: how to sum values

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

Announcements

Thingworx Navigate content has a new home! Click here to access the new Thingworx Navigate forum! ______________________________