Need to add new column in for each row

Hi,

I have one Data table which has some values (in Value column) and from that data table I need to put those values in another excel in its columns. Check the below screenshot of the excel

So I will use for each row loop to go through the first data table and get the values and assign each value to each column of the above excel. The excel will consist only one row.

The number of rows in the data table is equal to number of columns in Excel.

Any suggestion on this.

Hi @Debartha_Mitra_DE
Can you please share an example of the data (using dummy data if you prefer)?
Are you looking for something like a transpose operation, where you have datatable having values in key-value pairs which you then want to put in respective columns?

Please share the structure of datatable for comparison with this excel sheet.

1 Like

in the for each row create a string variable with spaces in between with all the row.item() values
within the loop use generate data table using this structured string to create a new data table for each row

Thanks for the reply, yes kind of that. Please see below screenshot for reference. This is the first excel from I am collecting the data in Datatable.

image

After collecting it I am going to put those data in to the respective columns in the second excel which I posted in the beginning. (similar like key value pairs)

sorry but I didn’t quite understand this

1 Like

Hello,

  1. Define: initial excel data as DT1 and, final excel data as DT2

  2. For each row1 in DT1:
    2.a. Try assign
    DT2.Rows(DT2.Rows.Count-1)(row1.Item(0).ToString) = row1.Item(1).ToString (is adding data if your column exists)
    2.b. Catch add data column in DT2 (ColumnName: row1.Item(0).ToString) + the assignment written at 2.a.

  3. Finally use the write range activity to write DT2 within your output excel

Best regards,
Marius

2 Likes

Hi, thanks for your input.

Sorry, but I didn’t understand the step 2b so, after assigning values in 2a step I need to use add data column activity in the same for each row and after what I need to do ?

Thanks in advance.

Hi,

Yes. You need to use add data column activity in order to create your new column (this column from the DT2 is equal with row data content from DT1 - your first column), and
Afterwards, the new column can be filled with data as in 2a step, by using the same assign activity

Best regards,
Marius

1 Like

Hi,
Can you please share a small workflow on this, I used the code that you shared but I am facing error. You can see my flow below,
image

The first read range will get the values for DT1 and the second read range use the DT2 which will use on the second excel (in that excel only column names presents)

Next in for each row loop

image

values present in row.item(2)
Thanks in advance.

I am getting the below error

image

Hi,

Please see the following file
Pay attention when you are building the DT2 table. It seems that a blank row (with a blank space) needs to be added in order for UiPath to run smoothly

Best regards,
Marius

2 Likes

Thanks a lot, it worked

2 Likes

@Debartha_Mitra_DE @Marius_Puscasu
The try-catch block is a great way to handle unexpected situations, for instance, a missing column.

Also, in this scenario, another approach could be to use the Contains check on table’s columns.
If DT.Columns.Contains(<columnNameVariable>)
→→Add value
Else
→→Add Data Column (the catch part of your try-catch block)

2 Likes

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.