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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.