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

225
Views
How to delete sheets based on a name criteria using Google Apps Script?

I've been trying to delete existing sheets whose names match a criteria, but I keep running into the error below and I can't find out why.

The code I'm running:

const SS = SpreadsheetApp.getActiveSpreadsheet();
const allSheets = SS.getSheets();
const category = 'Block-A';
const newSheetName = 'BOQ ' + category;

for (let a = 0; a < allSheets.length; a++) {
  let existingSheetName = allSheets[a].getName();
  if (existingSheetName == newSheetName) {
    SS.deleteSheet(allSheets[a]);
  }
}

The error: Sheet xxxxxxxxxx not found

Appreciate your help.

Antonio

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

0

After reading through the comments on the question, this is what has worked for me:

First, make sure that the sheet names are listed. In my case, I got a column clientCategories whose each value would become a sheet name:

let sheetsToDel = [];
for (let a = 0; a < clientCategories.length; a++) {
  sheetsToDel.push('BOQ ' + clientCategories[a])
}

Then, I'd go through all the sheets and delete them:

for (let a = 0; a < allSheets.length; a++) {
  const sheetName = allSheets[a].getName()
  for (let r = 0; r < sheetsToDel.length; r++) {
    if (sheetName == sheetsToDel[r]) {
      SS.deleteSheet(allSheets[a]);
    }
  }
}

There may be many better ways to it, but this is what I got.

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!