2

i've been coding this:

    function deleteSheets(){
 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var sheets = ss.getSheets();


  for(var i = 0; i<=sheets.length; i++){

    var name = sheets[i].getName();

    if (name == "aux"){
      ss.deleteSheet(sheets[i]);
    }else if(name == "nulos"){

      ss.deleteSheet(sheets[i]);

      }



    /* switch(sheets[i].getName()){

      case sheets[i].getName() == "aux":
        ss.deleteSheet(sheets[i]);
        break;

      case sheets[i].getName() == "nulos":
        ss.deleteSheet(sheets[i]);
        break;

      case name == "aux":
        ss.deleteSheet(sheets[i]);
        break; 
  }

  */

}

}

Tried to do a simple "deleteSheet" with switch, which didn't work, then with a if just to see the same error:

TypeError: Cannot call method "getName" of undefined. (line 8, file "Code")

What am I missing? Pretty sure i've already done a similar thing before... (If it's obvious, i'm sorry, i'm noob)

1 Answer 1

1

You make a mistake on your for statement.

You wrote:

for(var i = 0; i<=sheets.length; i++){}

but when i = sheets.length, you're out of your array, so you try to call getName() of nothing. You should have

for(var i = 0; i<sheets.length; i++){}

To avoid mistake like this, please consider using

var i = 0
for(i in sheets){}

where i is automatically increment. Here some more details about for/in statement

1
  • Dude,again you solved my problems, you rock! I'll start using this for this way from now on.... Was going crazy on that hahaha. Thanks! May 11, 2017 at 14:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.