Google Sheets Row deletion script

I have this spreadsheet that I need to have the rows deleted every day. I have found the script but when I try and use it I get an error.

Error: TypeError: Cannot call method "deleteRows" of undefined. (line 4, file "Code")

And this is the code that I am using:

function deleteResponses() {
var ss = SpreadsheetApp.openById("SPREADSHEETKEY"); 
var sheet = ss.getSheets()[3]; 
sheet.deleteRows(2, 30);
};

I don't understand what I'm doing wrong. Any help would be greatly appreciated.

Upvotes: 0

Views: 146

Answers (1)

Wicket
Wicket

Reputation: 38416

This line var sheet = ss.getSheets()[3] assigns undefined to sheet because your spreadsheet has three or less sheets, as Javascript use 0 based indexes for arrays.

The fix is to change 3 by the proper index number.

Upvotes: 1

Related Questions