Skip to content
Advertisement

Google Apps Script to find and update rows in target sheet by unique ID and add unique rows if unique ID is not in the target sheet

Good day folks! I have this codes in which I want to combine into one, but having trouble to do so.

This is the first code in which the data is copied from source sheet to target sheet added to the after the last row with data

JavaScript

And this is the code that I saw here which works perfectly in my spreadsheet, where this one updates the column B if there were changes based on unique ID (column A)

JavaScript

Now I am wondering how I am going to combine this 2, where if the source sheet has unique ID that needs updating it will update the target sheet and if there is a new unique ID, it will just add the data at the bottom

Advertisement

Answer

I believe your goal is as follows.

  • You have 2 sheets of the source sheet and the target sheet.
  • You want to update and append values from the source sheet to the target sheet by checking the column “A” of both sheets.

In this case, how about the following modified script?

Modified script:

JavaScript
  • From your script, this sample script supposes that your 1st row of both sheets is the header row. Please be careful about this.

Note:

  • I proposed the above script by guessing your Spreadsheet from your script and question. When this script is not useful for your situation, can you provide the sample Spreadsheet? By this, I would like to confirm it.

References:

Added 1:

From the following replying,

this works fine, but the data being added to the target sheet is just column A and B, here is the sample sheet: docs.google.com/spreadsheets/d/… where the range from source sheet that needs to transfer to target sheet is N:X

How about the following sample script?

Modified script:

JavaScript

Added 2:

About your following new question,

what if I just want to update the Column B in target sheet? and other column will stay the same?

How about the following script?

Modified script:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement