Custom Functions in Google Sheets  |  Apps Script  |  Google for Developers (2024)

Google Sheets offers hundreds ofbuilt-in functions likeAVERAGE,SUM, andVLOOKUP. When these aren’tenough for your needs, you can use Google Apps Script to write custom functions— say, to convert meters to miles or fetchlive content from the Internet — then use themin Google Sheets just like a built-in function.

Getting started

Custom functions are created using standard JavaScript. If you're new toJavaScript, Codecademy offers agreat course for beginners.(Note: this course wasn't developed by and isn't associated with Google.)

Here's a simple custom function, named DOUBLE, which multiplies aninput value by 2:

/** * Multiplies an input value by 2. * @param {number} input The number to double. * @return The input multiplied by 2. * @customfunction*/function DOUBLE(input) { return input * 2;}

If you don't know how to write JavaScript and don't have time to learn,check the add-on store tosee whether someone else has already built the custom function you need.

Creating a custom function

To write a custom function:

  1. Createor open a spreadsheet in Google Sheets.
  2. Select the menu item Extensions >Apps Script.
  3. Delete any code in the script editor. For the DOUBLE function above, simplycopy and paste the code into the script editor.
  4. At the top, click Save save.

Now you can use the custom function.

Getting a custom function from the Google Workspace Marketplace

The Google Workspace Marketplace offers several customfunctions asadd-ons for Google Sheets.To use or explore these add-ons:

  1. Createor open a spreadsheet in Google Sheets.
  2. At the top, click add-ons > Get add-ons.
  3. Once the Google Workspace Marketplaceopens, click the search box in the top right corner.
  4. Type "custom function" and press Enter.
  5. If you find a custom function add-on you're interested in, click Installto install it.
  6. A dialog box might tell you that the add-on requires authorization. If so,read the notice carefully, then click Allow.
  7. The add-on becomes available in the spreadsheet. To use the add-on in adifferent spreadsheet, open the other spreadsheet and at the top, clickadd-ons > Manage add-ons. Find the add-on you want to use and clickOptions more_vert > Use in thisdocument.

Using a custom function

Once you've written a custom function or installed one from theGoogle Workspace Marketplace, it's as easy to use as abuilt-in function:

  1. Click the cell where you want to use the function.
  2. Type an equals sign (=) followed by the function name and any input value —for example, =DOUBLE(A1) — and press Enter.
  3. The cell will momentarily display Loading..., then return the result.

Guidelines for custom functions

Before writing your own custom function, there are a few guidelines to know.

Naming

In addition to the standard conventions for naming JavaScript functions, beaware of the following:

  • The name of a custom function must be distinct from the names ofbuilt-in functions likeSUM().
  • The name of a custom function cannot end with an underscore (_), whichdenotes a private function in Apps Script.
  • The name of a custom function must be declared with the syntaxfunction myFunction(), not var myFunction = new Function().
  • Capitalization does not matter, although the names of spreadsheet functionsare traditionally uppercase.

Arguments

Like a built-in function, a custom function can take arguments as input values:

  • If you call your function with a reference to a single cell as an argument(like =DOUBLE(A1)), the argument will be the value of the cell.
  • If you call your function with a reference to a range of cells as anargument (like =DOUBLE(A1:B10)), the argument will be a two-dimensionalarray of the cells' values. For example, in the screenshot below, thearguments in =DOUBLE(A1:B2) are interpreted by Apps Script asdouble([[1,3],[2,4]]). Note that the sample code for DOUBLEfrom above would need to bemodified to accept an array as input.

    Custom Functions in Google Sheets | Apps Script | Google for Developers (1)

  • Custom function arguments must bedeterministic. Thatis, built-in spreadsheet functions that return a different result each timethey calculate — such as NOW() or RAND() — are not allowed as argumentsto a custom function. If a custom function tries to return a value based onone of these volatile built-in functions, it will display Loading...indefinitely.

Return values

Every custom function must return a value to display, such that:

  • If a custom function returns a value, the value displays in the cellthe function was called from.
  • If a custom function returns a two-dimensional array of values, the valuesoverflow into adjacent cells as long as those cells are empty. If this wouldcause the array to overwrite existing cell contents, the custom function willthrow an error instead. For an example, see the section onoptimizing custom functions.
  • A custom function cannot affect cells other than those it returns a value to.In other words, a custom function cannot edit arbitrary cells, only thecells it is called from and their adjacent cells. To edit arbitrary cells,use a custom menu to run a function instead.
  • A custom function call must return within 30 seconds. If it does not, thecell displays #ERROR! and the cell note is Exceeded maximum execution time(line 0).

Data types

Google Sheets stores data indifferent formats depending onthe nature of the data. When these values are used in custom functions, AppsScript treats them as theappropriate data type in JavaScript.These are the most common areas of confusion:

  • Times and dates in Sheets becomeDate objects in Apps Script. If the spreadsheet and thescript use different time zones (a rare problem), the custom function willneed to compensate.
  • Duration values in Sheets also become Date objects, butworking with them can be complicated.
  • Percentage values in Sheets become decimal numbers in Apps Script. Forexample, a cell with a value of 10% becomes 0.1 in Apps Script.

Autocomplete

Google Sheets supports autocomplete for custom functions much like forbuilt-in functions. As youtype a function name in a cell, you will see a list of built-in and customfunctions that matches what you enter.

Custom functions will appear in this list if their script includes aJsDoc@customfunction tag, as in the DOUBLE() example below.

/** * Multiplies the input value by 2. * * @param {number} input The value to multiply. * @return The input multiplied by 2. * @customfunction */function DOUBLE(input) { return input * 2;}

Advanced

Using Google Apps Script services

Custom functions can call certainGoogle Apps Script services to perform more complextasks. For example, a custom function can call theLanguage service to translate an Englishphrase into Spanish.

Unlike most other types of Apps Scripts, custom functions never ask users toauthorize access to personal data. Consequently, they can only call servicesthat do not have access to personal data, specifically the following:

Supported services Notes
Cache Works, but not particularly useful in custom functions
HTML Can generate HTML, but cannot display it (rarely useful)
JDBC
Language
Lock Works, but not particularly useful in custom functions
Maps Can calculate directions, but not display maps
Properties getUserProperties() only gets the properties of the spreadsheet owner. Spreadsheet editors can't set user properties in a custom function.
Spreadsheet Read only (can use most get*() methods, but not set*()).
Cannot open other spreadsheets (SpreadsheetApp.openById() or SpreadsheetApp.openByUrl()).
URL Fetch
Utilities
XML

If your custom function throws the error message You do not have permission tocall X service., the service requires user authorization and thus cannot beused in a custom function.

To use a service other than those listed above, create acustom menu that runs an Apps Script functioninstead of writing a custom function. A function that is triggered from a menuwill ask the user for authorization if necessary and can consequently use allApps Script services.

Sharing

Custom functions start out bound to thespreadsheet they were created in. This means that a custom function written inone spreadsheet can't be used in other spreadsheets unless you use one of thefollowing methods:

  • Click Extensions > Apps Script toopen the script editor, then copy thescript text from the original spreadsheet and paste it into the script editorof another spreadsheet.
  • Make a copy of the spreadsheet that contains the custom function by clickingFile > Make a copy. When a spreadsheet is copied, any scripts attached toit are copied as well. Anyone who has access to the spreadsheet can copy thescript. (Collaborators who have only view access cannot open the script editorin the original spreadsheet. However, when they make a copy, they become theowner of the copy and can see the script.)
  • Publish the script as a Google Sheets Editor Add-on.

Optimization

Each time a custom function is used in a spreadsheet, Google Sheets makes aseparate call to the Apps Script server. If your spreadsheet contains dozens (orhundreds, or thousands!) of custom function calls, this process can be quiteslow.

Consequently, if you plan to use a custom function multiple times on a largerange of data, consider modifying the function so that it accepts a range asinput in the form of a two-dimensional array, then returns a two-dimensionalarray that can overflow into the appropriate cells.

For example, the DOUBLE() function shown above can be rewritten to accept asingle cell or range of cells as follows:

/** * Multiplies the input value by 2. * * @param {number|Array<Array<number>>} input The value or range of cells * to multiply. * @return The input multiplied by 2. * @customfunction */function DOUBLE(input) { return Array.isArray(input) ? input.map(row => row.map(cell => cell * 2)) : input * 2;}

The above approach uses themap method of JavaScript's Array object to recursivelycall DOUBLE on every value in the two-dimensional array of cells. It returns atwo-dimensional array that contains the results. This way, you can call DOUBLEjust once but have it calculate for a large number of cells at once, as shown inthe screenshot below. (You could accomplish the same thing with nested ifstatements instead of the map call.)

Custom Functions in Google Sheets | Apps Script | Google for Developers (2)

Similarly, the custom function below efficiently fetches live content from theInternet and uses a two-dimensional array to display two columns of results withjust a single function call. If each cell required its own function call, theoperation would take considerably more time, since the Apps Script server wouldhave to download and parse the XML feed each time.

/** * Show the title and date for the first page of posts on the * Developer blog. * * @return Two columns of data representing posts on the * Developer blog. * @customfunction */function getBlogPosts() { var array = []; var url = 'https://gsuite-developers.googleblog.com/atom.xml'; var xml = UrlFetchApp.fetch(url).getContentText(); var document = XmlService.parse(xml); var root = document.getRootElement(); var atom = XmlService.getNamespace('http://www.w3.org/2005/Atom'); var entries = document.getRootElement().getChildren('entry', atom); for (var i = 0; i < entries.length; i++) { var title = entries[i].getChild('title', atom).getText(); var date = entries[i].getChild('published', atom).getValue(); array.push([title, date]); } return array;}

These techniques can be applied to nearly any custom function that is usedrepeatedly throughout a spreadsheet, although the implementation details willvary depending on the function's behavior.

Custom Functions in Google Sheets  |  Apps Script  |  Google for Developers (2024)
Top Articles
The 6 best strategies to minimize tax on your retirement income
How I Built A $3 Million High-Yield Portfolio With 11 Blue-Chips
Victory Road Radical Red
The Largest Banks - ​​How to Transfer Money With Only Card Number and CVV (2024)
Amc Near My Location
Tyson Employee Paperless
craigslist: kenosha-racine jobs, apartments, for sale, services, community, and events
Dollywood's Smoky Mountain Christmas - Pigeon Forge, TN
Identifont Upload
Team 1 Elite Club Invite
St Petersburg Craigslist Pets
Jesus Calling December 1 2022
What Auto Parts Stores Are Open
Retro Ride Teardrop
Ribbit Woodbine
Craigslist Phoenix Cars By Owner Only
Weather In Moon Township 10 Days
Aquatic Pets And Reptiles Photos
Simon Montefiore artikelen kopen? Alle artikelen online
Clarksburg Wv Craigslist Personals
Moparts Com Forum
How to find cash from balance sheet?
Swedestats
Jinx Chapter 24: Release Date, Spoilers & Where To Read - OtakuKart
Walmart stores in 6 states no longer provide single-use bags at checkout: Which states are next?
G Switch Unblocked Tyrone
Cta Bus Tracker 77
Testberichte zu E-Bikes & Fahrrädern von PROPHETE.
Faurot Field Virtual Seating Chart
How many days until 12 December - Calendarr
Utexas Iot Wifi
2011 Hyundai Sonata 2 4 Serpentine Belt Diagram
2023 Ford Bronco Raptor for sale - Dallas, TX - craigslist
Skidware Project Mugetsu
Medline Industries, LP hiring Warehouse Operator - Salt Lake City in Salt Lake City, UT | LinkedIn
Sacramento Craigslist Cars And Trucks - By Owner
Ringcentral Background
Does Circle K Sell Elf Bars
How to Draw a Bubble Letter M in 5 Easy Steps
Nicole Wallace Mother Of Pearl Necklace
Audi Q3 | 2023 - 2024 | De Waal Autogroep
Rise Meadville Reviews
Magicseaweed Capitola
D3 Boards
Toonily The Carry
Red Dead Redemption 2 Legendary Fish Locations Guide (“A Fisher of Fish”)
8 Ball Pool Unblocked Cool Math Games
The Angel Next Door Spoils Me Rotten Gogoanime
Sc Pick 3 Past 30 Days Midday
Craigslist Pets Lewiston Idaho
Verilife Williamsport Reviews
라이키 유출
Latest Posts
Article information

Author: Reed Wilderman

Last Updated:

Views: 5348

Rating: 4.1 / 5 (52 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Reed Wilderman

Birthday: 1992-06-14

Address: 998 Estell Village, Lake Oscarberg, SD 48713-6877

Phone: +21813267449721

Job: Technology Engineer

Hobby: Swimming, Do it yourself, Beekeeping, Lapidary, Cosplaying, Hiking, Graffiti

Introduction: My name is Reed Wilderman, I am a faithful, bright, lucky, adventurous, lively, rich, vast person who loves writing and wants to share my knowledge and understanding with you.