How to Use App Script in Google Sheets

How-to basics · Updated November 23, 2026

Google Apps Script is a JavaScript-based platform that lets you automate, extend, and integrate Google Workspace apps, including Google Sheets. With it, you can write custom functions, create menus, send emails based on sheet data, and even build simple web apps. This guide walks you through the essentials: opening the script editor, writing your first custom function, running it, and deploying it as a menu item or trigger. You’ll need a Google account and a web browser—no prior coding experience is required, though basic JavaScript familiarity helps.

What You’ll Need

  • A Google account (free) and access to Google Sheets in a web browser.
  • A Google Sheet where you want to use the script (you can create a new one).
  • Basic understanding of JavaScript syntax (variables, functions, loops) is helpful but not mandatory.

Step-by-Step

  1. Open your Google Sheet in a browser. Go to sheets.new to create a blank sheet, or open an existing one.
  2. Open the Apps Script editor. Click Extensions in the top menu, then select Apps Script. This opens a new tab with the script editor. The default file is named Code.gs.
  3. Write a simple custom function. In the editor, replace any default code with this:
function sayHello() {
  return "Hello from Apps Script!";
}

This function returns a string. You can use it directly in a cell like a built-in function.

  1. Save your script. Click the floppy disk icon or press Ctrl+S (Windows) or Cmd+S (Mac). Give the project a name, like “My First Script”, and click Rename if prompted.
  2. Use your custom function in a cell. Go back to your sheet (the tab labeled “Sheet1” or the sheet name). In any cell, type =sayHello() and press Enter. The cell will display “Hello from Apps Script!”. This works because Apps Script functions that return a value can be called like built-in functions.
  3. Create a function that manipulates sheet data. Let’s write a script that sets a value in a specific cell. In the script editor, add this function below the first one:
function setGreeting() {
  var sheet = SpreadsheetApp.getActiveSheet();
  sheet.getRange("A1").setValue("Hello from script!");
}
  1. Run the function manually. In the script editor, select setGreeting from the function dropdown (next to the play button). Click the Run button (▶). If prompted, review the permissions—click Review permissions, choose your Google account, and click Allow on the warning screen. This grants the script access to your sheet. After running, go back to the sheet—cell A1 now contains “Hello from script!”.
  2. Add a custom menu to run the function without opening the editor. In the script editor, add this function:
function onOpen() {
  var ui = SpreadsheetApp.getUi();
  ui.createMenu("My Script")
    .addItem("Set Greeting", "setGreeting")
    .addToUi();
}

This creates a menu named “My Script” in the sheet’s top menu bar. The onOpen trigger runs automatically when the sheet opens. To see it, reload the sheet (close and reopen the tab). You’ll see the new menu next to Help.

  1. Set up a time-driven trigger to run a function automatically. In the script editor, click the clock icon (Triggers) on the left sidebar. Click Add Trigger. Set the function to setGreeting, choose “Time-driven” as the event source, and select an interval (e.g., “Hour timer” or “Day timer”). Click Save. The function will now run automatically at the chosen interval.
  2. Debug your script. If something goes wrong, use Logger.log() to print values. For example, modify setGreeting to log the sheet name:
function setGreeting() {
  var sheet = SpreadsheetApp.getActiveSheet();
  Logger.log("Active sheet: " + sheet.getName());
  sheet.getRange("A1").setValue("Hello from script!");
}

After running, click Executions in the left sidebar to see the logs. You can also use the Debug button (bug icon) to step through your code.

Common Mistakes

  • Forgetting to save before running—the script editor runs the last saved version. Always press Ctrl+S (Cmd+S on Mac) before clicking Run.
  • Using the wrong function name in a cell. Custom functions must be typed exactly, including parentheses. If you get #NAME?, check spelling.
  • Not handling permissions—the first time you run a function that accesses your sheet, you must authorize it. If you skip this, the script fails with an error.
  • Calling a function that doesn’t return a value in a cell. For example, =setGreeting() will show an error because setGreeting returns nothing. Only functions that return a value can be used as custom functions.
  • Using getActiveSheet() in a trigger—when a time-driven trigger runs, there may be no active sheet. Use SpreadsheetApp.openById() to reference a specific sheet instead.

Tips

  • Use the built-in editor features—Apps Script has autocomplete and syntax highlighting. Press Ctrl+Space to see suggestions.
  • Keep functions small—break complex tasks into multiple functions for easier debugging.
  • Use SpreadsheetApp.getActiveSpreadsheet() vs getActiveSheet()—the former gets the whole spreadsheet, the latter gets the current tab. Use the appropriate one.
  • Test custom functions with simple inputs—for functions that take arguments, test with literal values first, e.g., =myFunction(5,10).
  • Take advantage of the built-in onEdit trigger—you can create a simple trigger by naming a function onEdit(e) that runs whenever a cell changes. This is great for automation.
  • Use the Range class for bulk operations—instead of setting cells one by one, use setValues() with a 2D array for speed.

Troubleshooting

  • Script runs but nothing changes—Check if you’re writing to the correct sheet. getActiveSheet() returns the sheet that was active when the script ran, which may not be the one you expect. Use SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1") to be specific.
  • “Authorization is required” error—You need to grant permissions. Click Review permissions in the dialog, select your account, and click Allow. If you’re using a Google Workspace account, an admin may need to allow access.
  • Custom function shows #ERROR!—Open the script editor and click Executions to see the error message. Common issues include referencing a sheet that doesn’t exist or using a function that returns an array in a single cell (use =ARRAYFORMULA() or press Ctrl+Shift+Enter).
  • Trigger doesn’t run—Check that you saved the trigger correctly and that the function name matches exactly. Also, time-driven triggers have a minimum interval of 1 hour for most free accounts; you can’t set it to every minute.
  • Script editor is blank—If you opened a new sheet, the editor should have a default Code.gs. If not, click + next to Files to create a new script file.

Frequently Asked Questions

  1. Can I use Apps Script on mobile?—The script editor is only available in a desktop browser. You can view and run scripts from the Google Sheets mobile app, but you can’t edit them.
  2. Is Apps Script free?—Yes, for personal use. There are quotas for daily usage (e.g., triggers, URL fetch), but they are generous for most tasks. Business users may have higher limits through Google Workspace.
  3. Can I share a script with others?—Yes. When you share a spreadsheet, the script is shared too. However, collaborators need their own authorization to run scripts that access their own data. You can also publish a script as an add-on.
  4. How do I delete a trigger?—Go to the Triggers page (clock icon), click the three-dot menu on the right of the trigger, and select “Delete”.
  5. Why does my custom function recalculate only when the sheet changes?—Custom functions are recalculated when the sheet is edited or when the spreadsheet is opened. To force recalculation, you can edit any cell or use a trigger to re-run the function.
  6. Can I use Apps Script to send emails?—Yes, use MailApp.sendEmail(). For example, MailApp.sendEmail("recipient@example.com", "Subject", "Body"). This requires additional permissions.

Related

Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.

More in How-to basics

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.