Home > Back-end >  Delete Sheets Based On Sheet Positions In Workbook
Delete Sheets Based On Sheet Positions In Workbook

Time:05-10

I'm extremely new to scripts. I'm trying to delete sheets between sheet 2 and sheet 10. I'm using sheet() to name the sheets so they are named their respective positions.

CodePudding user response:

function myFunction() {
  
  SpreadsheetApp.getActiveSpreadsheet()
                .getSheets()
                .slice(1, 10)
                .forEach(i => 
                  SpreadsheetApp.getActiveSpreadsheet().deleteSheet(i)
                )

}

Here is a dynamic way to delete all sheets from the 2nd to 10th sheet.

This works by getting all sheets (.getSheets()) and only keeping a segment of them (.slice()). For each of these sheets (.forEach()) we delete them.

CodePudding user response:

function Delete() {
  var spreadsheet = SpreadsheetApp.getActive();
  spreadsheet.getRange('A1').activate();
  spreadsheet.setActiveSheet(spreadsheet.getSheetByName('sheet(3)'), true);
  spreadsheet.deleteActiveSheet();
  spreadsheet.setActiveSheet(spreadsheet.getSheetByName('sheet(4)'), true);
  spreadsheet.deleteActiveSheet();
  spreadsheet.setActiveSheet(spreadsheet.getSheetByName('sheet(5)'), true);
  spreadsheet.deleteActiveSheet();
  spreadsheet.setActiveSheet(spreadsheet.getSheetByName('sheet(6)'), true);
  spreadsheet.deleteActiveSheet();
  spreadsheet.setActiveSheet(spreadsheet.getSheetByName('sheet(7)'), true);
  spreadsheet.deleteActiveSheet();
  spreadsheet.setActiveSheet(spreadsheet.getSheetByName('sheet(8)'), true);
  spreadsheet.deleteActiveSheet();
  spreadsheet.setActiveSheet(spreadsheet.getSheetByName('sheet(9)'), true);
  spreadsheet.deleteActiveSheet();
};

WHERE sheet(?) IS NAME

  • Related