Henrique Barcelos
Henrique Barcelos

Reputation: 491

Clear both content and checkboxes from the first n sheets

I know there are the functions getSheetId() and getSheetByName(), but the name of the spreadsheets may change over time and they wouldn't apply anymore (or maybe they get deleted).

Is there a way to get the first ten sheets, except the last two (10 -2)? I want to create a reset button that will clear all fields from specific range, but I can't attach the function to a specific name. The last two spreadsheets can't be included in this function because they can't lose data.

Here it's the script that clears text cells in a single spreadsheet, but it doesn't work for clearing FALSE cells (checkboxes):

function ClearCells() {
  var sheet = SpreadsheetApp.getActive().getSheetByName('Master');
  sheet.getRange('C5:C34').clearContent();
   
}

Upvotes: 1

Views: 683

Answers (2)

Amit Agarwal
Amit Agarwal

Reputation: 11268

This can work.

function ClearCells() {
  var ss = SpreadsheetApp.getActive();
  var sheets = ss.getSheets();
  for (var i=0; i<10 && i<sheets.length; i++) {
    sheets[i].getRange('C5:C34').clearContent();
  }
}

Upvotes: 1

Marios
Marios

Reputation: 27350

Solution to the updated question:

You can use removeCheckboxes() to delete the checkboxes:

function ClearCells() {
   const ss = SpreadsheetApp.getActive();
   const sheets = ss.getSheets().slice(0,10);
   sheets.forEach(sh=>
     {
       sh.getRange('C5:C34').clearContent();
       sh.getRange('C5:C34').removeCheckboxes();
     }
   );
}

References:

Upvotes: 2

Related Questions