Home > Mobile >  How to get a script to work on all desired sheets at once
How to get a script to work on all desired sheets at once

Time:10-27

I have a script I am using that works on a single tab/sheet but I would like the same process & script to work on 4 tabs. Should be easy enough?

Each tab is an employee name, and when the clicking a checkbox "sold" it moves the entire row to the "sold" tab. But again, it only works on one and I need it to work on four.

Additionally, once I get this squared away I would like to do the same but when checking "lost" it goes to the "lost" tab. I have a screenshot to show what I mean, and also the script I am using below. Screenshot

1 function onEdit(e) {
2  const src = e.source.getActiveSheet();
3 const r = e.range;
4  if (src.getName() != "Cynthia" || r.columnStart != 9 || r.rowStart == 2) return;
5 const dest = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sold");
6  src.getRange(r.rowStart,1,1,8).moveTo(dest.getRange(dest.getLastRow() 1,1,1,8));
7  src.deleteRow(r.rowStart);
8 }

CodePudding user response:

Use Sheet.getName() and String.match() with a regular expression, like this:

function onEdit(e) {
  let sheet;
  if (e.range.columnStart !== 9
    || e.range.rowStart === 2
    || !(sheet = e.range.getSheet()).getName().match(/^(Cynthia|Dweezil|Moon Unit|Ringo)$/i)) {
    return;
  };
  const targetSheet = SpreadsheetApp.getActive().getSheetByName('Sold');
  const targetRange = targetSheet.getRange(targetSheet.getLastRow()   1, 1);
  sheet.getRange(e.range.rowStart, 1, 1, 8).moveTo(targetRange);
  sheet.deleteRow(e.range.rowStart);
}
  • Related