Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

176
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda