Combine two Google Sheets into one?

Hello,

Is there a way I could take all rows from two different Google Sheets and create a third (new) Sheet containing all this data? My first thought was to use a Google Drive activity that finds all the Google Sheets that contain the same two words, such as “RPA Unsub”. These two sheets would have other words in each sheet name, but they would always have the prefix “RPA Unsub”. Once those files are found, is there a way to store those two data tables together and paste them onto a new Google Sheet? Then once that is done, delete the original two Sheets.

Thank you!

1 Like

Hi @Josh_James

That would be good to identify the files.

Once you identify the files you can use read range on both the sheets and read them into two datatables.

Now use merge datatable to club both of them and then use add new sheet to create new sheet and add data using write range.

Then you can delete your old sheets using delete sheet activity

Cheers

2 Likes

Hello Anil - Thank you for the reply. Once I have my output for all the files from the Drive activity “FindFiles”, I have a “ForEach” loop. How do I separate the two data tables once the “Read Range” happens? Wouldn’t the first dt be overwritten when the loop happens again for the second sheet?

Hi @Josh_James

create a second datatable and after retreiving the data using read range do a merge datatable on to the second datatable in the loop. So that the second time the first dt gets over written and the merge will merge it with the previous data which is already present in it

This is how it looks

I added dt1.clone when the loop starts or for the first iteration because we need to give a structure to the second dt2 before performing merge

Hope this is clear

cheers

1 Like

Hello @Josh_James

I think the below approach should work for you.
image

Thanks

1 Like

Read both into separate datatables, Merge Datatable, then write back to Google Sheets.

1 Like

What does the condition index=0 refer to?

Hi @Josh_James

It is the counter or the index variable from the for loop… You need a structure so I am cloning the datatable only for the first time and that if condition will take care of it.

image

You can as well create a Boolean and use it like below(This just to use as switch for first iteration and subsequent iterations)

Cheers

1 Like

I am getting this error

Hi @Josh_James

Can you look at my previous post and follow accordingly

You are getting this because you did not create the variable required which is index

cheers

1 Like

Hi @Josh_James

Hope you have no issues .DO let me know if you face anymore issues , happy to help.

Else please feel free to close the topic by marking solution so that it helps others as well

cheers

1 Like

I appreciate the help very much. I feel like I’m getting closer, but after I do what was in your screenshots and exit the loop, I then have Gdrive make a new sheet, write to that sheet, write dt2, but it only writes one data table. I feel like the second time in the loop is getting overwritten somewhere. Let me gather some screenshots.

1 Like






Hi @Josh_James

Can you check the scope of the datatables. May be you set them to the inner sequence

And is it reading both sheets?..like in logs are you able to see both sheets going into if condition?

can you share the xaml.I dont see any issue in what you are doing unless the scopes are wrong or all the sheets are not getting read

cheers

Ah! You were right about the scope, but it was the scope of the bool_Run variable. I needed to expand that out further. Now it works! Thank you very much! One last question. I assume this will only work if there are 2 sheets found. Do you have a method to combine the dt for as many sheets that are found? More than 2?

1 Like

Hi @Josh_James

This is not for two sheets it will merge any number of sheets it finds. So merge datatable will contain all the old datatables in it and dt1 will always have the new and will be merged

Hope this clears

cheers

1 Like

But doesn’t bool_Run become false after the first sheet is found every time?

Hi @Josh_James

It should become false …Clone should happen only once. That is the reason we added bool to be false in first if condition it self

This is how it works

so merge datatable needs some structure to merge so we are cloning the structure when we get the data for the first time.

And for all subsequent loops the datatable already has a structure and has data in it so we need not clone again…we just need to append the data that we are getting

cheers

1 Like

Oooh! I follow. Thank you very much for the explanation! I truly appreciate it!

1 Like

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