Skip to content

Basics

What is function myFunction() in Apps Script?

Every new Google Apps Script file opens with an empty placeholder named function myFunction() {}. Google treats myFunction as an ordinary function name. Rename it after the task it performs, save, and select the new name before clicking Run.

1 of 4

Why every new project starts with function myFunction()

When you open Extensions, then Apps Script from a Google Sheet, Doc, or Form, Google creates a file named Code.gs with three lines: function myFunction() { }. Creating a standalone project at script.google.com inserts the same starter block.

That starter block gives the editor a valid function to select in the toolbar dropdown. Unlike onOpen(e), onEdit(e), doGet(e), or doPost(e), Google never calls myFunction on its own.

  • myFunction is a starter name you can keep, rename, or replace.
  • Running the untouched starter function succeeds and does nothing because there are no statements between the curly braces.
  • A single project can hold many functions across one or more .gs files.

2 of 4

How to rename myFunction and run your code

Change the word myFunction to a descriptive camelCase name that starts with a letter and contains no spaces, such as logSheetSummary or sendWeeklyReport. Clear names matter as soon as a project has more than one function in the Run dropdown or in Triggers.

Press Ctrl+S (or Cmd+S on macOS) after renaming. Then open the function dropdown next to the Run button, pick the new function name, and click Run.

Replace the empty myFunction() starter with a named task
function logSheetSummary() {
  const sheet = SpreadsheetApp.getActiveSheet();
  Logger.log(sheet.getName() + ' has ' + sheet.getLastRow() + ' rows.');
}

3 of 4

Common errors involving myFunction

Because myFunction is pre-selected in the editor toolbar and often left in early test triggers, renaming or deleting it causes two common messages: "Attempted to execute myFunction, but it was deleted" and "Script function not found: myFunction".

Both messages mean the editor toolbar, a trigger, a custom menu item, or a drawing button is still asking Apps Script to run the old name. Save your file, pick the new function in the Run dropdown, and update any trigger or menu string that still mentions myFunction.

  • If you paste a script from a guide below function myFunction() {} and forget to switch the dropdown, clicking Run executes the empty myFunction instead of the code you pasted.
  • If you delete myFunction without saving, the toolbar still lists it until you save with Ctrl+S or Cmd+S.
  • If a time-driven or form-submit trigger still points to myFunction, delete that trigger in the Triggers sidebar and create a new one for the renamed function.

4 of 4

Keeping more than one function in Code.gs

You do not need a separate Apps Script project for each task in a spreadsheet. Write a second function below the first, save, and both names appear in the Run dropdown.

Avoid giving two functions the same name in the same project, even across different .gs files. Apps Script loads all .gs files into one global scope, so the later declaration silently overwrites the earlier one.

Two functions in one project, one calling a shared helper
function logActiveSheetName() {
  Logger.log(formatSheetLabel(SpreadsheetApp.getActiveSheet()));
}

/** @param {SpreadsheetApp.Sheet} sheet */
function formatSheetLabel(sheet) {
  return sheet.getName() + ' (' + sheet.getLastRow() + ' rows)';
}

Remember

The short version

  • function myFunction() {} is an empty starter template that Google treats like any other function name.
  • Rename myFunction after the task your code performs, save with Ctrl+S or Cmd+S, and select the new name before clicking Run.
  • If clicking Run does nothing, check whether the toolbar dropdown is still set to an empty myFunction.
  • Renaming a function leaves existing triggers, custom menus, and drawing buttons pointing to the old name.

Keep going

Put it to work

All basics