Google sheet script to update multiple values in selected rows in another sheet

I need to get script that updates multiple values in another spreadsheet from row marked with checkbox to row with common ID (ID xxx xx xxxx) using manual trigger.

  1. In blue table I select rows with jobs to be updated.
  2. Run script clicking icon image
  3. Receive updated status in yellow table.

Here are sample spreadsheets:
https://docs.google.com/spreadsheets/d/1bjqauqiyEzUkECaxqswRmOQPYWydpSAvLf97t5ovbXQ/
https://docs.google.com/spreadsheets/d/11ZVtFJ5Ul5nUu3PLxUu_xBlfS0bbeAWRj8-ZKwKUTG8/

I already have script that work with OnEdit fution but it works only for one row at once but it’s not what I need to get. First of all manual trigger is the most important. Second is multiple rows update. Thanks for help!

enter image description here

>Solution :

From your sample Spreadsheets, how about the following sample script?

Sample script:

The function name of dorozliczenia is from a button on your Spreadsheet.

function dorozliczenia() {
  // 1. Retrieve source and destination sheets.
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const srcSheet = ss.getSheetByName("arkusz 1"); // Please confirm your sheet name again.

  // 2. Please set your destination Spreadsheet ID.
  const dstSS = SpreadsheetApp.openById("1bjqauqiyEzUkECaxqswRmOQPYWydpSAvLf97t5ovbXQ");
  const dstSheet = dstSS.getSheetByName("arkusz 1"); // Please confirm your sheet name again.

  // 3. Retrieve source values from the source sheet, and create an object for searching IDs.
  const obj = Object.fromEntries(srcSheet.getRange("A3:C" + srcSheet.getLastRow()).getValues().map(([a, , c]) => [c, a]));

  // 4. Retrieve destination values from the destination sheet and an array for putting to the destination sheet.
  const dstRange = dstSheet.getRange("A3:B" + dstSheet.getLastRow());
  const values = dstRange.getValues().map(([, b]) => [obj[b] ? "DONE" : "IN PROGRESS", b]);

  // 5. Put the created array to the destination sheet.
  dstRange.setValues(values);
}
  • When this script is run, the values of DONE and IN PROGRESS are put into column "A" of the destination sheet by checking the ID of column "B".

  • If you don’t want to put the value of IN PROGRESS, please modify [obj[b] ? "DONE" : "IN PROGRESS", b] to [obj[b] ? "DONE" : null, b].

References:

Leave a Reply