简体   繁体   中英

Creating Multiple Sheets in Google Sheets w/Loop

I am attempting to put together this code that will create multiple sheets that I will eventually be putting data on.

Here is what I have:

  var activeSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var sheet_names_to_create = ["blah1", "blah2", "blah3", "blah4", "blah5", "blah6", "blah7", "blah8"]
  for (var i = 0; i < sheet_names_to_create.length; i++) {
    if(activeSpreadsheet.getSheetByName(sheet_names_to_create[i]) == null){
      var newSht = activeSpreadsheet.insertSheet();//Create new Sheet
      newSht.setName(sheet_names_to_create[i]);
    }
  }

The issue is that I ran it the first time and saw no added sheets. I ran it the second time and it says the sheet name already exists.

What am I missing or doing incorrectly?

Create Sheets

 function hisfunction() {
  var ss=SpreadsheetApp.getActive();
  var sA=["blah1", "blah2"]
  sA.forEach(function(s,i){var sh=ss.getSheetByName(s);if(!sh){ss.insertSheet(s);}else{sh.clearContents();}});
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM