Skip to content

Diagnostic reference

A sheet with the name '...' already exists

Fix Exception: A sheet with the name '...' already exists in Google Apps Script insertSheet() and setName() by checking getSheetByName() first.

Updated December 6, 2026

Apps Script error

Exception: A sheet with the name "Audit Log" already exists. Please enter another name.

What it means

Every tab inside a Google Spreadsheet must have a unique name (matched case-insensitively). Calling spreadsheet.insertSheet('Audit Log') or sheet.setName('Audit Log') throws this error on the second run if the tab already exists, even if the existing tab is hidden.

Related error messages

  • Exception: A sheet with the name "Summary" already exists. Please enter another name.
  • Exception: Sheet name cannot be empty.

Before and after

The code before and after the fix.

Problem 1 changed line
function getAuditSheet() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  // Works on the first run, then throws on every later run.
  return ss.insertSheet('Audit Log');
}
Fixed 2 changed lines
function getAuditSheet() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const existing = ss.getSheetByName('Audit Log');
  if (existing) return existing;
  return ss.insertSheet('Audit Log');
}

How to fix it

  • Check const sheet = ss.getSheetByName('Audit Log') before calling ss.insertSheet('Audit Log'), and reuse or clear the existing tab.
  • If you want a fresh tab on every run, append a timestamp or counter suffix (for example Audit Log 2026-12-06) or check getSheetByName() in a loop until you find an unused name.
  • Check the All Sheets menu at the bottom-left of Google Sheets for hidden tabs with the same name.

Things to check

  • Sheet names are case-insensitive in Google Sheets, so "audit log" collides with "Audit Log".
  • myFunction warns on unguarded insertSheet() calls with literal names and offers a quick fix to reuse an existing tab.

In the Apps Script editor

Catch errors like this before clicking Run.

myFunction (published as Error Checker for Google Apps Script on the Chrome Web Store) checks your code while you edit at script.google.com. Diagnostics, autocomplete, and rule-based quick fixes are free without an account, run in your browser, and request no Google Drive, Gmail, or Apps Script project OAuth scopes. Writing Apps Script with an AI coding agent? Connect the Apps Script MCP server to run the same static checks before pasting code.