Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

178
Views
Delete Last X Number of Sheets Using Google App Script

I need to delete the last x number of sheets (tabs) from a spreadsheet (workbook) using Google App Script. All I have been able to scrounge up through Googling is this bit of code, which does not do what I need.

function DeleteSheet() {
  var spreadsheet = SpreadsheetApp.getActive();
  for (i = 0; i < 10; i++) {
  spreadsheet.deleteActiveSheet();}
};

This obviously deletes the first X number of sheets (tabs) at the beginning of a spreadsheet. What I need is a similar script that will delete the last x number of sheets at the end of a spreadsheet. In other words I need to delete sheets from right to left rather than from left to right as is done in the code above.

Below is the exact code that I need in a vba format, but I don't know how to write it appropriately in Google App Script.

Sub DeleteAll()
    i = Worksheets.Count
    For x = i to 21 Step -1 '# <- please note the change here
        Application.DisplayAlerts = False
        Worksheets(x).Delete
        Application.DisplayAlerts = True
    Next x
End Sub

Thanks in advance!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Try this:

function delete_sheets() {
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var sheets = spreadsheet.getSheets();
  var len = sheets.length;
  for (var i = len-1; i > len-11; i--) spreadsheet.deleteSheet(sheets[i]);
}

Another variant:

function delete_sheets() {
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var sheets = spreadsheet.getSheets();
  var x = 10;
  while (x--) spreadsheet.deleteSheet(sheets.pop());
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!