Delete a sheet in a Google Sheet using Apps Script

To delete a sheet in a Google Sheet using Apps Script, you can use the deleteSheet() method of the Spreadsheet class. Here’s an example code snippet:

function deleteSheet() {
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var sheetName = 'Sheet1'; // Replace with the name of the sheet you want to delete
  
  var sheet = spreadsheet.getSheetByName(sheetName);
  
  if (sheet) {
    spreadsheet.deleteSheet(sheet);
  }
}

In this code, the deleteSheet function first retrieves the active spreadsheet using SpreadsheetApp.getActiveSpreadsheet(). You can modify this line to access a specific spreadsheet if needed.

Next, you need to specify the name of the sheet you want to delete by assigning it to the sheetName variable. In this example, it’s set to 'Sheet1', but you should change it to the actual name of the sheet you want to delete.

The getSheetByName() method is used on the spreadsheet object to retrieve the sheet with the specified sheetName.

To avoid errors, it’s a good practice to check if the sheet variable is not null before attempting to delete it. This is done with the if (sheet) condition.

Finally, if the sheet exists, the deleteSheet() method is called on the spreadsheet object, passing the sheet as an argument, to delete the sheet from the Google Sheet.

You can call the deleteSheet function from another function or set up a trigger to execute it based on your needs. When executed, it will delete the specified sheet in the Google Sheet.

Feel free to modify the code to suit your specific requirements and work with different sheets in the spreadsheet.

Pamai Tech
Turning ideas into Reality

Products

Office Add-in

Enterprise Solutions

Cloud Consulting

UI UX Design

Data Transformation

Services

FAQ's

Privacy Policy

Terms & Condition

Team

Contact Us

Company

About Us

Services

Features

Our Pricing

Latest News

© 2023 Pamai Tech