Skip to content

Instantly share code, notes, and snippets.

@john-hix
Last active July 10, 2018 17:58
Show Gist options
  • Save john-hix/3380512423d90bdd47aa43311b3586d9 to your computer and use it in GitHub Desktop.
Save john-hix/3380512423d90bdd47aa43311b3586d9 to your computer and use it in GitHub Desktop.
Google Sheets "sheetExists" function (detect if sheet exists)
function sheetExists(sheetName) {
if (typeof sheetName !== "string") {
throw ("The sheetExists function requires string input.")
}
var allSheets = SpreadsheetApp.getActive().getSheets();
var foundSheet = false;
var len = allSheets.length;
var chkThisIndex = 0;
while (!foundSheet && chkThisIndex < len) {
if (allSheets[chkThisIndex].getName() === sheetName) {
foundSheet = true;
} else {
chkThisIndex++
}
}
return foundSheet;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment