Get started with running JavaScript in Google Sheets

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


Step 1 — Create a new Google Sheets spreadsheet

I personally use the URL 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 Tools —> Script Editor 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.


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!

Stay up to date

Follow me via email to receive actionable tips and other exclusive content. I'll also send you notifications when I publish new content.
By signing up you agree to the Privacy Policy & Terms.

Have feedback for me?

I'd appreciate any feedback you can give me regarding this post.

Was it useful? Are there any errors or was something confusing? Would you like me to write a post about a related topic? Any other feedback is also welcome. Thank you so much!