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.
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.
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.
SpreadsheetApp.SheetLoggerKeep going
Put it to work
- Task guide Add a custom menu to a Google Sheet
Add a menu named Helpful tools to the menu bar of a Google Sheet. Its one item shows which cell is selected. Save the script, reload the Sheet, and the menu appears.
- Task guide Run an Apps Script function every day
Make a function run on its own once a day. Run the setup function once, and from then on Apps Script calls the reminder function each day around the hour you chose.
- Task guide Send an email for each row in a Google Sheet
Send one email to each person listed on a Customers tab. The script marks each row Sent as it goes, so a later run sends only to rows that are not marked yet.
- Basics Run your first Apps Script function
Your first script, from opening the editor to reading its output. You replace the starter function, save, click Run, and read what Logger.log printed in the Execution log.
- Basics Google Apps Script triggers explained
Google Apps Script triggers run functions automatically when a file opens, a cell changes, a form is submitted, or a scheduled time arrives. Use a simple onEdit or onOpen for work in the bound file. Choose an installable trigger for a schedule, form submission, or work that needs authorization.
- Basics Common Apps Script errors and what they mean
Apps Script error messages say what failed, for example TypeError: Cannot read properties of null (reading 'getRange') or ReferenceError. Find the exact message below for its cause and fix.
- Diagnostic Attempted to execute myFunction, but it was deleted
Fix the error when the toolbar dropdown or a trigger still targets the deleted myFunction starter.
- Diagnostic Script function not found
Point a custom menu item, assigned drawing, or installable trigger at a function name that exists.