Skip to main content
12-Amethyst
July 29, 2019
Solved

[Help] Get field values dynamically from InfoTable

  • July 29, 2019
  • 1 reply
  • 2450 views

Hi there!

I'm writing a service to convert any InfoTable to CSV text.

The input is InfoTable without DataShape, it can be any InfoTable, with any filed definition.

Now to make this service possible, I need to read data from InfoTable rows dynamically.

Normally, I can use row.FieldName to get field value.

But in my case, the feild name is a variable, so I want to use some function like row.values[myVariable].

And I cannot find any possible method to read data as I expected.

Any advise?

Thanks a lot!

Best answer by tallrain

Thanks for the information, but I cannot access into that article.

 

And I've fixed this issue with below codes :):

---------------------------------------------------------------------------------------------------------

var iLF;
var tmp = 'TIME';
var i = 0;
var fd = []; // array to store Fields
fd[0] = 'TIME';

// Get CSV Header
if ((myInfoTable.dataShape===null)||(myInfoTable.dataShape===undefined)) {
iLF = myInfoTable.ToJSON().dataShape.fieldDefinitions;
} else {
iLF = myInfoTable.dataShape.fields;
}
for (var key in iLF) {
if(key != 'TIME') {
i = i + 1;
fd[i] = key; // Store field sequence
tmp = tmp + ',' + key;
}
//var result = key;
//Logger.info("Field Name "+key+" baseType: "+iLF[key].baseType);
}
tmp = tmp + ' \n';
//logger.info('JSON2CSV, i='+i);

// Get CSV Data
var tableLength = myInfoTable.rows.length;
for (var x=0; x < tableLength; x++) {
var row = myInfoTable.rows[x];
//logger.info('JSON2CSV, row='+row);
tmp = tmp + dateFormatISO(row.TIME); // Get TIME Value

var fd2 = []; // To output data in sequence
for (var property in row) {
tmp2 = property; //row property
//logger.info('JSON2CSV, tmp2='+tmp2);
tmp3 = row[property]; //row value
//logger.info('JSON2CSV, tmp3='+tmp3);
var j = fd.indexOf(tmp2);
fd2[j] = tmp3;
}

for (var k=1; k<fd2.length; k++) {
tmp = tmp + ',' + fd2[k];
}
tmp = tmp + ' \n';
}

var result = tmp;

---------------------------------------------------------------------------------------------------------

1 reply

5-Regular Member
July 29, 2019

Hello.

 

Would some of the examples offered in this article about Dynamic Queries get you started in the right direction? Let me know if this is helpful.

 

-- Craig A.

tallrain12-AmethystAuthorAnswer
12-Amethyst
July 30, 2019

Thanks for the information, but I cannot access into that article.

 

And I've fixed this issue with below codes :):

---------------------------------------------------------------------------------------------------------

var iLF;
var tmp = 'TIME';
var i = 0;
var fd = []; // array to store Fields
fd[0] = 'TIME';

// Get CSV Header
if ((myInfoTable.dataShape===null)||(myInfoTable.dataShape===undefined)) {
iLF = myInfoTable.ToJSON().dataShape.fieldDefinitions;
} else {
iLF = myInfoTable.dataShape.fields;
}
for (var key in iLF) {
if(key != 'TIME') {
i = i + 1;
fd[i] = key; // Store field sequence
tmp = tmp + ',' + key;
}
//var result = key;
//Logger.info("Field Name "+key+" baseType: "+iLF[key].baseType);
}
tmp = tmp + ' \n';
//logger.info('JSON2CSV, i='+i);

// Get CSV Data
var tableLength = myInfoTable.rows.length;
for (var x=0; x < tableLength; x++) {
var row = myInfoTable.rows[x];
//logger.info('JSON2CSV, row='+row);
tmp = tmp + dateFormatISO(row.TIME); // Get TIME Value

var fd2 = []; // To output data in sequence
for (var property in row) {
tmp2 = property; //row property
//logger.info('JSON2CSV, tmp2='+tmp2);
tmp3 = row[property]; //row value
//logger.info('JSON2CSV, tmp3='+tmp3);
var j = fd.indexOf(tmp2);
fd2[j] = tmp3;
}

for (var k=1; k<fd2.length; k++) {
tmp = tmp + ',' + fd2[k];
}
tmp = tmp + ' \n';
}

var result = tmp;

---------------------------------------------------------------------------------------------------------

tallrain12-AmethystAuthor
12-Amethyst
July 30, 2019

I use this service to convert InfoTable into CSV, so that I can display data into Dygraph widget.

And for Dygraph, the 1st field is always Datatime, so this Service put field of TIME for Time Sequence.

Now it works just fine.