Add Script Google Sheets: How to Install & Run Apps Script

How-to basics · Updated November 25, 2026

To add a script to Google Sheets, open your spreadsheet, go to Extensions > Apps Script, paste your code into the editor, and click Save. This opens the built-in Apps Script editor, which is the only official way to run custom JavaScript code in Sheets.

What Google Apps Script Actually Is

Apps Script is a cloud-based JavaScript platform developed by Google. It runs on Google’s servers, so you don’t need to install anything on your computer. The editor is directly linked to your spreadsheet, and any script you write there can access the current sheet, other sheets in the same file, or even external APIs. It is not the same as a macro in Excel; it is more like a mini web server that reacts to triggers or runs on demand.

You can write functions that read or write cell values, create menus, send emails, fetch URLs, or interact with other Google services like Gmail, Calendar, and Drive. Every script is stored inside the spreadsheet file itself, which means it travels with the file when you share or copy it.

How to Open the Apps Script Editor

There are two main ways to get to the editor:

  • Menu path: In Google Sheets, click Extensions in the top menu, then select Apps Script. This opens a new tab with the editor.
  • Direct URL: If you know the spreadsheet ID (the long string in the URL), you can go to https://script.google.com/d/{SCRIPT_ID}/edit – but the easiest is the menu.

Once the editor is open, you will see a default file called Code.gs. That is where you paste or type your script. The editor has a toolbar with a Save icon (a floppy disk) and a Run button (a play triangle).

Writing Your First Script: A Simple Example

Suppose you want a script that adds the current date to the active cell. In the editor, replace any existing code with:

function addDate() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var cell = sheet.getActiveCell();
  cell.setValue(new Date());
}

Then click Save, give your project a name (e.g., “Date Adder”), and press Run. The first time you run, Google will ask you to authorize the script – it needs permission to read and write your spreadsheet. After authorization, the script will run and place the current timestamp into whatever cell is selected.

To make it easier to run later, you can add a custom menu. Use this code:

function onOpen() {
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('My Tools')
    .addItem('Insert Date', 'addDate')
    .addToUi();
}

After saving and reloading the spreadsheet, you will see a new menu called My Tools next to Help. Click it and choose Insert Date to run the script.

How to Run a Script Automatically (Triggers)

You don’t always have to run scripts manually. Apps Script supports triggers that run your code when a specific event happens. To set one up:

  1. In the Apps Script editor, click the clock icon on the left sidebar (labeled Triggers).
  2. Click Add Trigger at the bottom right.
  3. Choose which function to run (e.g., addDate).
  4. Select the event source: Time-driven (every hour, day, week) or From spreadsheet (on open, on edit, on change).
  5. Click Save and authorize if needed.

Common use cases: sending an email when a cell value changes, archiving old rows at midnight, or generating a PDF report every Monday. Note that time-driven triggers have a minimum interval of 1 hour, and simple triggers like onEdit() have a 30-second execution limit.

Common Problems and How to Fix Them

New users often hit the same issues:

  • “Authorization required” every time you run: This is normal the first few times. After you grant permission, it should stop. If it persists, clear your browser cache or use an incognito window.
  • Script runs but does nothing: Check that you are referencing the correct sheet. Use getActiveSheet() only if you want the sheet the user is viewing; otherwise, use getSheetByName('Sheet1').
  • Error: “Cannot call SpreadsheetApp.getActiveSheet() in this context”: This happens when you run a function from the editor that is not bound to a spreadsheet (e.g., a standalone script). Make sure you created the script from the spreadsheet’s Extensions menu.
  • Script takes too long: Apps Script has a 6-minute execution limit for regular functions. If you are processing many rows, optimize by using batch operations like setValues() instead of setValue() in a loop.

How to Add a Script from a Third-Party Source

Sometimes you find a script online (on GitHub, a blog, or a forum). To add it, copy the entire code, open your Apps Script editor, delete any existing code, and paste the new code. Then save and run. Be cautious: only run scripts from trusted sources, because they can read and modify your data, send emails, or access other Google services. Always read the code first, especially if it asks for broad permissions.

There is also a Library feature if you want to reuse code across multiple projects. In the editor, click the + next to Libraries, paste a script ID, and choose a version. This is more advanced and not needed for most users.

Apps Script vs. Excel VBA vs. Google Sheets Macros

If you are coming from Excel, you might wonder how Apps Script compares to VBA. The key differences:

  • Language: Apps Script uses JavaScript; VBA uses Visual Basic. JavaScript is more widely used and easier to learn for many people.
  • Environment: Apps Script runs in the cloud; VBA runs on your desktop. That means Apps Script can be triggered by time or events even when your computer is off.
  • Macros: Google Sheets has a built-in macro recorder (Extensions > Macros > Record macro) that creates Apps Script code behind the scenes. It is simpler for basic automation but limited for complex logic.

For most automation tasks in Sheets, Apps Script is the recommended tool. It is free, integrated, and can interact with other Google services.

Frequently Asked Questions

Q: Can I add a script to a shared Google Sheet?
Yes, but only if you have edit access. Anyone with edit access can open the Apps Script editor and modify the script. Viewers cannot. If you share the sheet, the script travels with it, and collaborators can run it (with their own authorization).

Q: Why does my script run but not affect the sheet?
Check that you are targeting the correct sheet and range. The most common mistake is using getActiveSheet() when the active sheet is not the one you intend. Use getSheetByName('Sheet1') to be specific. Also, ensure you call flush() if you are reading data right after writing.

Q: Is Apps Script free?
Yes, for personal use. Google provides a free quota of daily usage (e.g., 20,000 read/write calls per day). For heavy commercial use, you may need Google Workspace, but most personal and small-business scripts stay well within free limits.

Q: Can I use Python or another language instead of JavaScript?
No, Apps Script only supports JavaScript. However, you can call external APIs from Apps Script using UrlFetchApp, so you could have a Python server do heavy work and then return results to Sheets.

Q: How do I remove a script from a spreadsheet?
Open the Apps Script editor, select the project in the left panel, click the three-dot menu, and choose “Delete project.” This removes the script from the spreadsheet. Alternatively, you can just delete all code and save an empty project.

Q: What is the difference between a bound script and a standalone script?
A bound script is created from within a spreadsheet and is attached to that file. A standalone script is created at script.google.com and is not tied to any sheet – you have to open the spreadsheet by ID to access it. Bound scripts are easier for most tasks.

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.