How To Run A Script In Google Sheets

In this tutorial, you will learn how to run a script in Google Sheets.

Google Sheets is a powerful tool right out of the box, but it becomes even more powerful once you begin using scripts to add additional, custom features to your spreadsheets. Scripts can be used to process data, apply formatting, and much more. This tutorial outlines how to run an already-written script, but does not go into details of developing scripts

Script Setup

Before running a script, you have to add it to your Google Sheet

Step 1

Open the Extensions Menu and choose Apps Script to launch the Script Editor. If you haven’t switched to the new menu layout, you’ll find the Script Editor in the Tools menu.

Step 2

The Script Editor will load in a new browser tab. Delete the existing text from the editor region

Step 3

Type in or copy paste the script you want to run. If you don’t have a script already, you can use the following code that writes “Hello World” to the selected cell.

 

function helloWorld() {

const cell = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getActiveRange();

cell.setValue(“Hello World”);

}

Step 4

Add this tag to your code to make sure the script only affects the current spreadsheet

/**

* @OnlyCurrentDoc

*/

Step 5

Name your script project by clicking on the heading that says Untitled Project then typing in a descriptive name. For this example, we’ll call the project “Hello World”.

Step 6

Click the save button to save your script.

Step 7

The script is now ready to run

Running Your Script

The first time you run a script in Google Sheets under a given Google Account, you’ll have to authorize the code to run

Here’s how:

Step 1

In the script editor, make sure the script you want to run is selected in the Select Function to Run dropdown, then click the Run button

Step 2

Wait for the popup to appear, then click Review Permissions

Step 3

Step 4

Click on the account you want to run the script under

Step 5

Review the listed permissions, then click Allow if you are ok with granting the script these permissions and trust the source

Step 6

The script is now authorized. Click Run one more time to run the script and paste “Hello World” into the active cell(s)

Run a Script from a Button

You can run scripts from the script editor at any time, but it’s easier to run them directly from the spreadsheet. One way to do this is using a button.

Here’s how:

Step 1

Open the Insert menu and choose Drawing

Step 2

Use the Drawing Editor tools to create a button, then click Save and Close to add it to your spreadsheet

Step 3

Select the button and click on the 3 dots that appear to open the menu, then choose Assign Script

Step 4

Type the name of the function you want to run exactly as it appears in the Script Editor. Then click Ok.

Step 5

You can now run the script by clicking the button

Summary

Example Spreadsheet: Make a copy of the example spreadsheet

In this tutorial, I covered how to run a script in Google Sheets. Want more? Check out all the Google Sheets Tutorials.

Get more Google Sheets stuff

Subscribe to our mailing list and get interesting Google Sheets stuff and updates to your email inbox.

Thank you for subscribing.

Something went wrong.