How can we update cell value at column index and rowindex

How can we update cell value at column index and rowindex

for (int i = 0; i < getColumnCount(); i++) {
columnName = getColumnName(i);
if(c_ColumnNames.equals(columnName)){
// new column value
value2 = c_newcolumn;
// matched column value
value1 = getCell(columnName, tString);
// trying to set value at that particular column index
value1 = value2;
// gets me index of column
stringValue = String.valueOf(i);
}
}

Hi @shwetaoza , on a separate note, in any language, I would recommend not to use a Count function as condition in a for loop, but rather retrieve the count and assigned the result to a variable before the loop, and then use the variable instead.

The reason for this is that, at each iteration, that Count function (getColumnCount()) is being called, and you expect it to be the same result everytime. It is more expensive calling the function everytime vs using a variable instead. Basically:
for (int i = 0; i < getColumnCount(); i++) {
vs
int ColumnCount = getColumnCount();
for (int i = 0; i < ColumnCount; i++) {

The second version will run faster and is much more efficient.

3 Likes

Hi @shwetaoza ,

I’m trying to understand your use case for updating a cell based on column index and row index. When dealing with tables we typically have a column name rather than a number to work with, and one of the reasons for that is that when processing, we may at some point wish to change order of columns, or simply insert a new column somewhere and then your flow could be easily broken.

However, that all said, I’ll go with the idea that this is what you are really wanting to do. The question then that you ask is how?

Well, I should start by saying that the java snippet (which I assume is what you are using based on the piece of java code posted) is really not the node to do this. Whilst the java snippet node is applied to every row of the table, it is applied only on a per-row basis, so a single invocation of your code in this node will be able to “see” only the ‘current’ row. It would be possible within the snippet to test if you are on the “required” row, and then act accordingly but that would be horribly inefficient. Imagine a table with 100,000 rows, and then to update just row 3, the snippet would scan every one of those rows to check if it was on row 3, and only then apply the required change.

Ok, so I’ve discussed that you don’t really have efficient access to the a specific row, but you can identify if the snippet is currently processing the required row, by testing the ROWINDEX, but the next problem you hit is that there isn’t a mechanism within a Java Snippet for updating a column dynamically. The update of columns in a java snippet is pre-defined by columns you specify in the “Output” panel at the bottom. To update my “column2” in my dataset, I have to tell the snippet in advance that it will be outputting new values for “column2”. It isn’t possible within the java snippet to dynamically affect a column other than one that is defined by the Output. You could of course define every column as output, but then the snippet must provide a new value for every column or they will get set to missing on each invocation. You could possibly do something fancy prior to invoking the java snippet to get flow variables populated with the required row index, and column index and then have perhaps a java snippet that returns the required column name for a given column index into another flow variable, which is then used to specify the the output column (set via flow variable) in the next *java snippet. It’s probably doable but messy.

So Java snippets really aren’t the way to go for this use case!

If you really have to work with your data programmatically and you wish to work with the entire table in a single hit, with direct access to “cells”, then the Python Script node would give you access to the specific “cell” that you require. You’ll ideally need a brief understanding of pandas dataframes, and some basic python, but what you want is readily achievable using that node. You will of course have to set up a python environment to do that (Python 3, rather than Python 2 recommended unless you have a compelling reason to use the older version). If that’s of interest and you need to know more, then I, or somebody else could provide some basic examples.

3 Likes