hamd01
hamd01

Reputation: 73

add a new row to all sheets in spreadsheet

I have a very small simple script that inserts a new row in a sheet. However, currently I specify Sheet2, so it only performs the action on one sheet.

Does anyone know how I could adjust the code so that it performs the action on ALL sheets?

Thank you.

//this function should run once per day as close after midnight as possible, and will create a new row in the sheet for the day
// for a specific sheet
function addnewrowsheet2() {
  var sh = SpreadsheetApp.getActive().getSheetByName("Sheet2");
  sh.insertRows(6);
  var date = Utilities.formatDate(new Date(), "GMT+2", "dd/MM/yyyy")
  sh.getRange("A6").setValue(date);  
}

Upvotes: 0

Views: 42

Answers (1)

Cooper
Cooper

Reputation: 64082

Run the second function at least once.

function yourfunk() {
  const ss = SpreadsheetApp.getActive();
  ss.getSheets().forEach(sh => {
    sh.insertRows(6);
    sh.getRange("A6").setValue(Utilities.formatDate(new Date(), "GMT+2", "dd/MM/yyyy"));
  });
}

only creates one trigger whose handler is name yourfunk

function createTrigger() {
  if(ScriptApp.getProjectTriggers().filter(t => t.getHandlerFunction() == 'yourfunk').length == 0) {
    ScriptApp.newTrigger('yourfunk').timeBased().everyDays(1).atHour(0).create();
  }
}

Upvotes: 1

Related Questions