Skip to content

Instantly share code, notes, and snippets.

@ttsukagoshi
Last active November 17, 2020 15:34
Show Gist options
  • Save ttsukagoshi/8ffc740ad47fcb4a1b40ff506ee683f8 to your computer and use it in GitHub Desktop.
Save ttsukagoshi/8ffc740ad47fcb4a1b40ff506ee683f8 to your computer and use it in GitHub Desktop.
Delete all sheets in this Google spreadsheet except for the designated sheet IDs.
/**
* Delete all sheets in this spreadsheet except for the designated sheet IDs
* @param {Array} exceptionSheetIds [Optional] Array of sheet IDs to not delete
*/
function deleteSheets(exceptionSheetIds) {
exceptionSheetIds = exceptionSheetIds || [];
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheets = ss.getSheets();
for (var i = 0; i < sheets.length; i++) {
var sheet = sheets[i];
for (var j = 0; j < exceptionSheetIds.length; j++) {
var exceptionSheetId = exceptionSheetIds[j];
if (sheet.getSheetId() == exceptionSheetId) {
continue;
} else {
ss.deleteSheet(sheet);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment