I am receiving a server error on a very simple script, and I'm not sure why. The intent of the script is to check if a sheet already exists. If the sheet does exist, it should be deleted and re-created. If it doesn't exist, it should be created.
The script works fine if the sheet doesn't exist already. If it does exist, the script will delete the sheet and then throw the following error on the line with .insertSheet("Test").
"We're sorry, a server error occurred. Please wait a bit and try again."
Any help is greatly appreciated!
function newSheet() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet(),
sheet = spreadsheet.getSheetByName("Test");
if(sheet != null){
spreadsheet.deleteSheet(sheet);
}
spreadsheet.insertSheet("Test");
}