1
votes

In Google Sheets, I have found and edited code that allows me to delete rows if a cell value in a specified column is 0 or blank. I want to run this same task across 2 of the 4 sheets in my spreadsheet. How do I edit the code to have it function in multiple sheets simultaneously. The 2 sheets are not related, but I have it structured so the same column will be searched in both sheets for the 0.

function readRows() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var rows = sheet.getDataRange();
  var numRows = rows.getNumRows();
  var values = rows.getValues();

  var rowsDeleted = 0;
  for (var i = 0; i <= numRows - 1; i++) {
    var row = values[i];
    if (row[4] == 0 || row[4] == '') {
      sheet.deleteRow((parseInt(i)+1) - rowsDeleted);
      rowsDeleted++;
    }
  }
};
1
There is a way to make multiple instances of server code run at the same time, but you need to trigger it from a sidebar or dialog box with google.script.run.functionName() The google.script.run thing is called "Client Side API." You can not cause code in the server to run multiple instances of a function from the server. The difference is whether it's triggered from the client or from the server. If you really don't care about running the same function simultaneously, but you just want it to work on different sheet tabs, you can get the sheet tab by name. ss.getSheetByName()Alan Wells

1 Answers

0
votes

Class SpreadsheetApp getActiveSheet() returns a single sheet. To get all the sheets of your spreadsheet use Class Spreadsheet getSheets(), to get only certain sheets by their name use Class Spreadsheet getSheetByName(name).