0
votes

I'm trying to apply one script that will run on an entire Google Sheets spreadsheet, not just one tab. I have multiple tabs all with the same format; I want the script to look at the data validation in column F for every tab and if "Not Applicable" is selected for any row, move the contents of that entire row to a tab called "Not Applicable Items"

I have no experience writing scripts, so I copied the script I'm currently using from a forum topic. It successfully moves the rows to the correct tab, but only for the specified active sheet. I want the script to look at the entire spreadsheet and move any row marked "Not Applicable."

How can I do this? Here's my code:

function onEdit() {
  var sheetNameToWatch = "Floorplan + Calendars";

  var columnNumberToWatch = 5; // column A = 1, B = 2, etc.
  var valueToWatch = "Not Applicable";
  var sheetNameToMoveTheRowTo = "Not Applicable Items";

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = SpreadsheetApp.getActiveSheet();
  var range = sheet.getActiveCell();

  if (
    sheet.getName() == sheetNameToWatch &&
    range.getColumn() == columnNumberToWatch &&
    range.getValue() == valueToWatch
  ) {
    var targetSheet = ss.getSheetByName(sheetNameToMoveTheRowTo);
    var targetRange = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
    sheet
      .getRange(range.getRow(), 1, 1, sheet.getLastColumn())
      .moveTo(targetRange);
    sheet.deleteRow(range.getRow());
  }
2

2 Answers

1
votes

All of the scripts in a given project have access to all of the sheets in the spreadsheet. Thats why we recommend that you write you onEdit(e) script like this:

function onEdit(e) {
  var sh=e.range.getSheet();
  if(sh.getName()!="Sheet Name") return;

This limits the script from performing operations on unwanted sheets.

However, in your case you want all sheets involved in the onEdit(e) function so this is not necessary.

Personally, I wouldn't run this function in an onEdit() function because it's liable to take longer than 30 seconds. But the functionality you requested is accomplished with the following script. exclA is an array of sheet names that you don't want to include in your "Not Applicable" search like the sheet named "Not Applicable Items".

function notApplicable() {
  var ss=SpreadsheetApp.getActive();
  var dsh=ss.getSheetByName('Not Applicable Items')
  var exclA=["Not Applicable Items"];
  var shts=ss.getSheets();
  for(var i=0;i<shts.length;i++) {
    var name=shts[i].getName();
    if (exclA.indexOf(shts[i].getName())==-1) {
      var sh=shts[i];
      var rg=sh.getDataRange();
      var vA=rg.getValues();
      var d=0;
      for(var j=0;j<vA.length;j++) {
        if(vA[j][5]=="Not Applicable") {
          dsh.appendRow(vA[j]);
          sh.deleteRow(j+1-d++)
        }
      }
    }
  }
}

I think that a lot of users over use the onEdit() capability.

0
votes

You can grab each spreadsheet in a for loop by doing

for(var i = 0; i < [number of sheets]; i++){
  sheet = ss.getSheets()[i];

  . . . [rest of the code]
}

and include what you're trying to do inside the for loop to do with each sheet.