How to Link Apps Script to Google Sheets
In this article
To link Apps Script to a Google Sheet, open the spreadsheet, select Extensions > Apps Script, add your code in the editor, save it, and run a function. This creates a script project associated with that spreadsheet, so it can access the file through Google’s spreadsheet services. You’ll need a Google account with edit access to the sheet, a web browser, and a clear idea of what you want the script to do.
What You'll Need
- A Google account and a Google Sheet you own or can edit.
- A modern web browser with JavaScript enabled.
- A small task for the script, such as writing a value into a cell. For this guide, the sample script writes a timestamp to cell A1 on the active sheet.
Step-by-Step
- Open the spreadsheet you want to connect. Go to Google Sheets in your browser and open the file. Confirm you are signed in to the account that has permission to edit it. If you have several sheets open, check the file name before continuing; the script project will be associated with the spreadsheet from which you open it.
- Open the linked Apps Script project. In the Sheets menu, select Extensions > Apps Script. A new browser tab opens with the Apps Script editor. The project is bound to the spreadsheet: it can work with that file using the active spreadsheet service, and the spreadsheet’s editor access generally determines who can open and edit the bound project.
- Name the project. Click the project name, which may initially be “Untitled project,” and enter something recognizable, such as “Sheet Timestamp Tool.” Project names help you distinguish scripts later; changing this name does not change the spreadsheet’s file name.
- Replace the starter code with a simple function. In the editor, select the existing sample code and replace it with the following. The function gets the sheet that is currently active and writes the current date and time into A1.
This code uses Apps Script’s built-in Spreadsheet service. It does not need a separate spreadsheet ID because it runs from a project opened through the target spreadsheet.function writeTimestamp() { const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); sheet.getRange("A1").setValue(new Date()); } - Save the project. Click the disk-shaped Save project button or press Ctrl+S on Windows or ChromeOS. On a Mac, use Command+S. If prompted, confirm the project name. Wait for the editor to indicate that the changes have been saved before running the function.
- Choose the function to run. Use the function selector in the toolbar to select
writeTimestamp, then click Run. If the function does not appear in the list, save the project and check that the function name is spelled correctly and that its braces and parentheses are balanced. - Authorize the script. On its first run, Google may ask you to review permissions because the script needs to access the spreadsheet. Select Review permissions, choose the Google account that has access, read the requested permissions, and approve them if they match what you intend the script to do. If a warning says the app is unverified, proceed only if you own or trust the script and understand its code; do not approve an unfamiliar script just to make it run.
- Check the spreadsheet. Return to the spreadsheet tab and look at cell A1 on the active sheet. It should contain the date and time when the function ran. If another tab was active when you ran it, A1 on that tab will be updated instead. You can format the cell using Format > Number if you want a different date or time display.
- Run it again or add an automation. To run the function manually later, return to Extensions > Apps Script, select
writeTimestamp, and click Run. If it should run automatically, open the Apps Script editor’s Triggers section (the clock icon), click Add Trigger, choose the function and event type, then save. For example, a time-driven trigger can run on a schedule. Review the event options carefully: a spreadsheet edit trigger is different from a time-driven trigger, and scheduled runs may not happen at an exact minute.
Common Mistakes
- Opening the wrong project or file: Start from the target spreadsheet using Extensions > Apps Script. A standalone project opened from the Apps Script home page is not automatically bound to the sheet.
- Expecting the code to run just because it is saved: Saving stores your changes; it does not execute the function. Click Run or configure a trigger.
- Forgetting authorization: A script may need permission the first time it runs, and changes to what it accesses can cause a new permission prompt.
- Using the wrong active tab:
getActiveSheet()refers to the sheet tab active when the function runs. If the script must always target a particular tab, use an explicit sheet name and check that the name matches exactly. - Assuming every editor can run every trigger: Installable triggers run under the account of the person who created them. That account needs access to the spreadsheet and any other services the script uses.
Tips
- Test new code on a copy of the spreadsheet, especially if it will change or clear many cells.
- Use descriptive function and project names. For example,
updateInventoryis clearer thanmyFunction. - Keep the script’s permissions proportional to its job. Avoid copying code from sources you do not trust, and inspect what it does before granting access.
- For a script that needs a specific tab, replace the active-sheet approach with
getSheetByName("Sheet1")and handle the possibility that the tab is missing before calling methods on it. - Use the Apps Script editor’s execution history or run details to inspect errors and confirm whether a manual or triggered execution completed.
Troubleshooting
The Apps Script option is missing or unavailable: Make sure the file is open in Google Sheets, not a preview, and that your account has edit access. In a work or school account, an administrator may restrict Apps Script. Try reloading the sheet or confirming access with its owner.
The function does not appear or the editor shows an error: Save the project, check the function declaration and punctuation, and read the error message shown in the editor. A missing brace, quote, or parenthesis can prevent the code from running.
The function runs but the cell does not change: Check which spreadsheet and tab were active, then inspect the execution details for errors. Confirm the target cell is not protected in a way that prevents the script from editing it, and verify the account running the code has the needed access.
Authorization fails or repeats: Confirm that you selected the correct Google account and that it can edit the file. If the script’s requested access seems unrelated or the code is unfamiliar, stop rather than approving it. Work-account policies may also block authorization; contact your administrator if so.
A trigger does not run as expected: Open the Triggers section and check that the trigger is enabled, uses the intended function and event type, and was created by an account that still has access. Review execution history for failures. Some events, such as edits made by another script or API, do not behave like a person editing a cell and may not activate a simple edit trigger.
Frequently Asked Questions
Is Apps Script automatically linked when I open it from Sheets?
Yes. Selecting Extensions > Apps Script from a spreadsheet creates or opens a script project bound to that spreadsheet.
Do I need the spreadsheet’s ID?
Not for the basic bound-script approach shown here. A script opened from the target sheet can use SpreadsheetApp.getActiveSpreadsheet().
Can I link one script to more than one spreadsheet?
A bound project is associated with its spreadsheet. To use code across multiple files, you can make a standalone Apps Script project and open files by ID, or create separate bound projects as appropriate.
Does saving the script run it automatically?
No. Save the code, then run a function manually or set up a trigger if you need it to run automatically.
Can spreadsheet editors see the bound script?
People who can edit the spreadsheet can generally access its bound Apps Script project. Treat the code as visible to spreadsheet editors, and do not store passwords or other secrets in it.
Will the script work in Excel?
No. Apps Script is Google’s platform for Google Workspace, including Google Sheets. Excel uses different automation tools, such as Office Scripts or VBA.