To run Apps Script in Google Sheets, open your spreadsheet, select Extensions > Apps Script, add and save a function, then choose that function in the editor and click Run. The first run may ask you to authorize access. Apps Script lets you automate spreadsheet tasks with JavaScript, such as writing values, formatting cells, or responding to a menu command.
What You’ll Need
- A Google account with permission to edit the spreadsheet. View-only access is not enough to add or run a script attached to the file.
- A Google Sheets file open in a web browser. Apps Script is available from the Sheets browser interface; the mobile app is not a practical place to edit and run scripts.
- A small task to automate. For the example below, use a test spreadsheet or choose an empty cell, because the sample writes a message into cell B2.
- Permission to approve the script’s requested access. A work or school Google Workspace administrator may restrict Apps Script or authorization.
Step-by-Step
- Open the spreadsheet and its script editor. In Google Sheets in your browser, open the file you want to work with. From the spreadsheet menu, select Extensions > Apps Script. A new browser tab opens with an Apps Script project connected to that spreadsheet. This is called a container-bound project: it can work with the spreadsheet it is attached to. If you do not see Extensions, check that you are in the desktop browser version of Sheets and have edit access.
- Identify the starter code. The editor may show a function named
myFunctionin a file such asCode.gs. A function is a named block of instructions that Apps Script can run. You can replace the starter function or add a separate function. For a first test, replace the starter code with the sample in the next step. Keep the function declaration and its opening and closing braces together; the editor needs a complete function to run. - Add a simple test function. Paste this code into the editor. It gets the active spreadsheet and sheet, then puts a short message in B2. If B2 contains something you need, change the cell reference to an empty cell before running it.
function writeMessage() { const spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); const sheet = spreadsheet.getActiveSheet(); sheet.getRange('B2').setValue('Hello from Apps Script'); }This is Apps Script JavaScript, not an Excel formula. The function name is
writeMessage; you will select that name in the editor before running. TheSpreadsheetAppservice is provided by Google Apps Script, whilegetRangeselects a cell andsetValuewrites to it. - Save the project. Click the Save project icon or press Ctrl+S on Windows or ChromeOS. On a Mac, use Command+S. If prompted, give the project a recognizable name, such as
Sheet message test. The project name does not change the spreadsheet name. Wait for the editor to show that the project has been saved before moving on; unsaved edits may not be included in the run. - Select the function to run. In the toolbar near the top of the Apps Script editor, open the function selector and choose
writeMessage. If the selector still showsmyFunctionor does not list your function, save the code and check that the name appears in a complete declaration spelledfunction writeMessage(). The selector lists runnable functions it can find in the project. - Run it and authorize access if asked. Click Run. On the first run, Google may display a permission flow because the script needs access to the spreadsheet. Review the account shown and the permissions requested, then follow the prompts to allow the access if you trust the code and intended to run it. Depending on the account and project, Google may show an unverified-app warning when the script requests certain permissions; do not bypass a warning for a script whose source or purpose you do not understand. A managed work or school account may require an administrator to approve access.
- Check the result in Sheets. Return to the spreadsheet tab and look at B2 on the active sheet. It should contain Hello from Apps Script. If another sheet was active when you ran the function, the message appears on that sheet instead. Running the same function again writes the same text to B2 again; it does not add a new row or preserve the previous contents of that cell.
- Review the run details if needed. In the Apps Script editor, check the execution status or open the execution log to see messages and errors from the latest run. You can also select Executions in the editor’s left-hand navigation to review past runs. An execution marked as completed means the function finished, but it is still worth checking the target cell to confirm it did the intended thing.
That is enough to run a function manually. You do not need to deploy a script as a web app just to run it from the editor. Deployment is for uses such as publishing a web app or add-on; it is a separate step with its own settings.
Common Mistakes
- Running the wrong function. The toolbar may still have
myFunctionselected. Check the selector before clicking Run, especially when a project contains more than one function. - Forgetting to save. The editor can contain newer text than the version you last saved. Save after edits, then run the function again.
- Expecting a formula result. Apps Script runs as code from the editor or a trigger; it is not entered into a cell like
=SUM(A1:A5). A custom function is a particular kind of script function that is called from a cell, and it has different restrictions. - Overwriting existing data. The sample sets the value of B2, replacing anything already there. Change the range to a safe, empty cell or work in a copy of the spreadsheet while learning.
- Assuming the active sheet is always the same. The example uses whichever sheet is active at run time. If a script should always use a particular tab, the code must identify that sheet rather than relying on the active one.
- Trying to run Apps Script in Excel. Apps Script is Google’s scripting platform for Google Workspace. It does not run in an Excel workbook; Excel uses different automation tools, including Office Scripts and VBA.
Tips
- Test on a copy or an empty area. Scripts can change spreadsheet data, and those changes are not always easy to reverse. Start with a duplicate file or harmless test cell until you understand what the code does.
- Use clear function names. Names such as
formatReportoraddDailyDatemake it easier to choose the right function than generic names such astest. - Make changes in small steps. Run a short function first, confirm its output, and then add one task at a time. This makes it easier to find the line that caused an unexpected result.
- Use an explicit sheet when reliability matters. The active-sheet approach is convenient for a demonstration, but a script used repeatedly can select a sheet by name and handle the case where that tab is missing.
- Know the difference between manual runs and automation. Clicking Run starts the function when you choose to. To run code on a schedule or in response to an event, set up an installable or simple trigger separately and review the permissions and trigger conditions.
- Keep the project attached when appropriate. A script opened through Extensions > Apps Script is associated with that spreadsheet. This is useful for spreadsheet-specific tasks, while a standalone Apps Script project is not opened from a particular sheet.
Troubleshooting
The Run button is disabled or the function is missing: Confirm the code has a valid function declaration, the braces are balanced, and the project is saved. Refreshing the editor after saving can help if the function list has not updated. Also verify that you are signed in to an account with edit access.
The function runs but the cell does not change: Check which spreadsheet and tab were active when you clicked Run, and confirm the code still points to B2. Look for an error in the execution log. If you changed the cell reference, make sure it is a valid A1-style range such as C4, and check whether the target cell is protected or otherwise unavailable to your account.
Authorization fails or loops: Make sure you are authorizing the Google account that can edit the spreadsheet. Review the requested access rather than approving prompts blindly. If it is a managed account, ask the Workspace administrator whether Apps Script or the requested scope is blocked. If you own the code and the prompt appears stale, close the authorization flow, return to the editor, and try running the saved function again.
The editor reports a syntax error: Read the error message and line number, then check for missing parentheses, braces, quotation marks, or misspelled names. A straight apostrophe or quotation mark is safest in code; smart punctuation copied from a document can cause parsing problems. Save and rerun after correcting the indicated line.
The script succeeds but affects the wrong tab: The example deliberately targets the active sheet, which can change based on which tab was selected. Select the intended tab before running, or revise the script to look up a named sheet. If multiple functions or triggers could have run, inspect Executions to identify which one completed.
Frequently Asked Questions
Do I have to install Apps Script?
No. Apps Script is integrated with Google Sheets. Open the editor through Extensions > Apps Script in a browser; there is no separate desktop program you need to install.
Can I run a script directly from a spreadsheet cell?
Not like a normal formula. You can create a custom function for certain calculations and call it from a cell, but custom functions cannot perform every action available to a script run from the editor. For actions that change other cells or require broader access, run a regular function from Apps Script or use an appropriate menu or trigger.
Does the script run automatically when I open the sheet?
No. A function you run manually only runs when started. Automatic behavior needs a trigger, such as an installable time-driven trigger or an appropriate spreadsheet event trigger. Configure one in the Apps Script editor and check its conditions and authorization before relying on it.
Do I need to deploy the project?
No, not for a manual run from the editor or for many spreadsheet-bound tasks. Deployment is required for certain published uses, such as a web app, but is not a general-purpose Run button requirement.
Can I undo a script with Ctrl+Z?
Do not count on the Sheets undo command to reverse script changes. Some script actions may not be undoable through the normal edit history. Protect important data by testing with a copy and having a recovery option before running code that makes substantial changes.
Will the same Apps Script code work in Excel?
No. Apps Script is designed for Google Sheets and other Google Workspace services. Excel uses its own features, such as Office Scripts or VBA, so the code and instructions need to be adapted rather than pasted into an Excel workbook.
Related
- Compress Photos in PowerPoint to Reduce File Size
- Compress Pictures in PowerPoint: Step-by-Step Guide
- Compare Two Documents in Word: Create a Redline
Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.
More in How-to basics
- Multiple Dependent Dropdowns in Google Sheets: SetupYes. In Google Sheets, you can create multiple dependent dropdowns by using a parent selection to filter a…
- Google Sheets FILTER Function: Basics and ExamplesGoogle Sheets’ FILTER function returns only the rows or columns that meet conditions you specify, while leaving the…
- How to Create a Professional Email Signature in OutlookTo create a proper signature in Outlook, open the signature settings for your version of Outlook, write a…
- Can Excel Read Outlook Emails, Contacts, and Calendar?Yes—Excel can work with Outlook data, but it does not automatically read your mailbox just because both apps…
- Excel Data Table Image: Convert Pictures to CellsTo turn a picture of a table into editable Excel data in Microsoft 365 for Windows, choose Data…
- How to Make a Booklet in Word Back to Back PrintingTo make a back-to-back booklet in Word, set the document’s page layout to Book fold, arrange your content…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.