Home > Mobile >  Copy values from a sheet to another sheet when checkbox is checked using Google Script
Copy values from a sheet to another sheet when checkbox is checked using Google Script

Time:02-11

I have an onEdit function for my Google Sheet where when a checkbox in column X of the source sheet is checked, it will copy the values of the cells in column Y and Z, then paste them into column A and B of the destination sheet. It worked fine previously but it suddenly stopped working and I can't seem to find the problem. I've tried adding a trigger for it to my Script Editor project as well, but nothing happens when I check the checkbox, and there is no error message. I'm quite new to this so any help is much appreciated.

Here's the code that I've been using:

function onEdit(e) {
  const src = e.source.getActiveSheet();
  const r = e.range;
  if(src.getName() !="Source" || r.columnStart != 24 || r.rowStart == 1,2) return;
  const dest = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Destination");
 
src.getRange(r.rowStart,25,1,2).copyTo((dest.getRange(dest.getLastRow() 1,1,1,2)),SpreadsheetApp.CopyPasteType.PASTE_VALUES,false);
}

CodePudding user response:

I believe your goal is as follows.

  • When the ckeckbox of the column "X" is checked, you want to copy the values of columns "Y" and "Z" from "Source" sheet to "Destination" sheet.

Although, unfortunately, I cannot understand the script of It worked fine previously, when your showing script is modified, how about the following modification?

Modified script:

function onEdit(e) {
  const src = e.source.getActiveSheet();
  const r = e.range;
  if (src.getName() != "Source" || r.columnStart != 24 || r.rowStart <= 2 || !r.isChecked()) return;
  const dest = e.source.getSheetByName("Destination");
  src.getRange(r.rowStart, 25, 1, 2).copyTo((dest.getRange(dest.getLastRow()   1, 1, 1, 2)), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
}
  • I thought that you wanted to do r.rowStart == 1,2 as r.rowStart <= 2.
  • In order to check the checkbox, isChecked() is used.

Reference:

  • Related