Home > Mobile >  add a new row to all sheets in spreadsheet
add a new row to all sheets in spreadsheet

Time:10-12

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);  
}

CodePudding user response:

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();
  }
}
  • Related