Get started with running JavaScript in Google Sheets

Last updated: February 09, 2025

This tutorial will show you how to run JavaScript code in Google Sheets.

Prerequisites

Step 1 — Create a new Google Sheets spreadsheet

I personally use the URL https://spreadsheet.new to create a new spreadsheet but you can also create one using Google Drive.

Step 2 — Create a Custom Function to evaluate JavaScript

Open the script editor by selecting Extensions —> Apps Script from the menu.

A screenshot of the Google Sheets UI that shows the Tools menu open with the Script Editor menu item selected.

Delete any code in the script editor. Then copy paste the code below into the editor.

/**
 * Evaluates JS code.
 *
 * @param {code} code The code to evaluate.
 * @return The result from evaluating the code.
 * @customfunction
 */
function RUNJS(code) {
  return eval(code); 
}

Save the code by selecting File —> Save from the script editor's menu or by pressing CTRL + S on your keyboard.

Step 3 — Use the function to run JavaScript code in Google Sheets

You can now use the =RUNJS() function in your spreadsheet. To try it out, enter 1+1 in cell A2 and enter the formula =RUNJS(A2) in cell B2. The resulting value in cell B2 is 2, which is the output of evaluating the JavaScript expression 1 + 1.

Here are some other JavaScript expressions to try out:

JavaScript expression

Description

Expected result

1 + 1

Adding two numbers

2

true && false

An expression that uses the AND logical operator

false

false || (true || false)

An expression that uses the AND and OR logical operators

true

2**2

Exponentiation (similar to the =POW() function in the spreadsheet).

4

[1,2,3].length

Length of an array

3

1 + 2 * 2

An expression to illustrate operator precedence

Below is a screenshot of the results I got by evaluating the JavaScript expressions in the above table using the RUNJS() function.

A screenshot of a spreadsheet displaying the results of evaluating JavaScript expressions using the RUNJS function.

Conclusion

This tutorial showed you how you can run JavaScript code in Google Sheets. I thought of this approach while creating exercises for the Learn coding using Google Sheets and Apps Script tutorial. It worked perfectly for my use case so I decided to write this post to help others that might find this technique useful.

Thank you for reading!

DISCLAIMER: This content is provided for educational purposes only. All code, templates, and information should be thoroughly reviewed and tested before use. Use at your own risk. Full Terms of Service apply.

Small Scripts, Big Impact

Join 1,500+ professionals who are supercharging their productivity with Google Sheets automation

Exclusive Google Sheets automation tutorials and hands-on exercises
Ready-to-use scripts and templates that transform hours of manual work into seconds
Email updates with new automation tips and time-saving workflows

By subscribing, you agree to our Privacy Policy and Terms of Service