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.
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.