
How Do You Create a Custom Function in Google Sheets?
Carlos GarciaPublished How Do You Create a Custom Function in Google Sheets?
Google Sheets ships with hundreds of built-in functions, and sooner or later you hit a calculation that none of them expresses cleanly. Your team's commission rule, a shipping band, a text format that only your warehouse uses.
At that point you can keep nesting IF statements, or you can write a function of your own and call it from a cell like any other formula. Sheets supports two completely different ways of doing that, and most guides only mention one of them.
What Is the Short Answer?
There are two routes, and which one you want depends on whether you are willing to write JavaScript. The Apps Script route gives you a real programming language; the named-function route gives you a reusable wrapper around a Sheets formula you already know.
For the Apps Script route, open Extensions > Apps Script, paste a JavaScript function into the editor, save it, and then call it from a cell by typing an equals sign and the function name.
For the named-function route, open Data > Named functions, give your function a name, list its argument placeholders, and paste in a Sheets formula as the definition. No code, no authorization prompt.
Google's documentation describes the second kind plainly: "Named functions let you create custom functions that can use built-in Sheets formulas." That single sentence is the whole distinction in a nutshell.
The Apps Script route is more powerful and more constrained at the same time, which sounds contradictory until you see the list of services a custom function is not allowed to touch. We get to that below.
Most people who search for this want the Apps Script version, so that is where the steps start. If you only need to package up a formula you already have, skip ahead to the named-function section and save yourself the editor entirely.
Not sure your spreadsheets and reporting are pulling their weight? Get a free audit from SEO Stuff and find out where your data work is leaking time.
Free audit
Where do you stand in AI search?
A manual SEO + AI search audit of your site: how ChatGPT, Gemini, Perplexity and Claude read and cite it today, and what to fix first. Report within 48 hours, no credit card.
Get my free auditOr have it all done: AI SEO services, $999 once
What Is a Custom Function in Google Sheets?
A custom function is a function you define yourself that behaves, from the cell's point of view, exactly like SUM or VLOOKUP. You type it with an equals sign, pass it arguments, and it returns a value into the cell.
The Apps Script kind lives in a script that is bound to one spreadsheet. The named-function kind lives in the spreadsheet file itself. Neither is a macro, and neither is an add-on, though add-ons can also supply custom functions.
How Does the @customfunction Tag Work?
A JavaScript function in your script is not automatically offered to you as you type in a cell. Google's JSDoc documentation is blunt about what makes the difference: the tag is "Required for the function to show up in the Sheets formula list."
You add it inside a JSDoc comment block directly above the function, as a bare tag line with no value after it. Google's own autocomplete page confirms the behavior: "Custom functions appear in this list if their script includes a JSDoc @customfunction tag".
The same comment block drives the little formula helper that pops up in the cell. Google documents real limits on it: the parameter name and description are shown, but a return description is not, optional-parameter syntax is not recognized so every parameter looks required, and default values are not supported.
What Data Does the Function Actually Receive?
Pass a single cell and your function receives that cell's value. Pass a range and it receives a two-dimensional array instead, so =DOUBLE(A1:B2) arrives in JavaScript as double([[1,3],[2,4]]).
Types get converted on the way in. Dates, times and durations all arrive as JavaScript Date objects, and Google states the percentage behavior explicitly: "Percentage values in Sheets become decimal numbers in Apps Script." A cell showing 10% hands your code 0.1.
How Do You Create One, Step by Step?
Step 1: Open the script editor. In your spreadsheet, choose Extensions > Apps Script. A new editor tab opens with a myFunction stub already in it.
Step 2: Replace the stub. Delete the placeholder code and paste your own function in its place, wrapped in a JSDoc comment that carries the custom-function tag.
/**
* Multiplies an input value by 2.
* @param {number} input The value to multiply.
* @return The input multiplied by 2.
* @customfunction
*/
function DOUBLE(input) {
return input * 2;
}Step 3: Save the script. Click the save icon at the top of the editor. There is no deploy step and no publish step for a custom function used in its own spreadsheet.
Step 4: Call it from a cell. Switch back to the spreadsheet, type =DOUBLE(A1) and press Enter. The cell shows "Loading..." for a moment while Sheets calls the Apps Script server, then displays the result.
Step 5: Make it handle ranges. The simple version above breaks if someone passes a range, because multiplying a two-dimensional array by two is meaningless in JavaScript. Google's own fix is to test with Array.isArray and use map to walk the array.
That fifth step is the one people skip, and it is the difference between a function that works in the cell you tested and one that works when a colleague drags it across a column of data.
If you would rather not maintain code at all, there is a third path: Add-ons > Get add-ons opens the Marketplace, where installed add-ons can supply custom functions that behave the same way in a cell.
Want to know which of your reporting workflows are worth automating at all? Start with a free audit and get a prioritized list instead of a guess.
What Are the Naming Rules?
For Apps Script custom functions, Google states a short list. The name must be different from a built-in function name such as SUM. It must be declared as function myFunction() rather than assigned with var myFunction = new Function().
There is one restriction people trip over constantly, and Google explains why it exists: a custom function name "can't end with an underscore (_), which denotes a private function in Apps Script." A trailing underscore makes the function invisible to the sheet on purpose.
Capitalization does not matter to Sheets. The convention of writing spreadsheet function names in capitals is exactly that, a convention, and your lowercase function will still resolve.
Named functions are governed by a much longer list. The name cannot match a built-in such as SUM, cannot be TRUE or FALSE, cannot use A1 or R1C1 reference syntax, cannot start with a number, must be under 255 characters, cannot contain spaces, and may use the underscore as its only special character.
Argument placeholders in a named function follow the same reference, space and special-character rules, and no two placeholders in one function may share a name. Oddly, a placeholder may be named after a built-in function such as SUM, and Google says the placeholder then takes precedence inside that definition.
The two systems also collide. You cannot create a named function with the same name as an Apps Script custom function that already exists in the sheet, and in the reverse case Google is explicit about who wins: "However, the existing named function supersedes it."
Here is the trap worth naming plainly, because it is everywhere in third-party write-ups: the A1-syntax rule, the no-spaces rule, the no-leading-digit rule and the 255-character limit are documented for named functions. Google does not state them for Apps Script custom functions, so do not assume they transfer.
What Can a Custom Function Not Do?
This is the part that surprises people who assume Apps Script means full access. The governing sentence in Google's documentation is that "custom functions never ask users to authorize access to personal data." No authorization prompt means no access to anything that would need one.
What survives that rule is a specific list of services: Cache, HTML, JDBC, Language, Lock, Maps, Properties, Spreadsheet, URL Fetch, Utilities and XML. Everything else, Gmail and Calendar and Drive among them, is out of reach from a custom function.
Even the Spreadsheet service is clipped. Its read methods work and its write methods do not, and you cannot reach another file with openById or openByUrl. The Properties service only returns the owner's user properties, and editors cannot set user properties at all.
When you cross that line, the error is specific rather than mysterious: "You do not have permission to call X service." That message means the service requires user authorization, so it can never be reached from a custom function, however your script is written.
There is a second hard boundary, on where output can land. Google states it directly: "A custom function can't affect cells other than those it returns a value to." A custom function cannot write a log row, cannot stamp a timestamp elsewhere, cannot tidy a column.
The documented workaround for both limits is the same: move the work into a function triggered from a custom menu. A menu-driven Apps Script function can request authorization and use the full range of services, because a human clicked it.
One more constraint catches teams rather than individuals. A custom function is bound to its spreadsheet and does not exist in other files unless you copy the script, copy the spreadsheet, or publish the script as an editor add-on.
Spending more time patching spreadsheets than acting on them? Ask for a free audit and we will tell you where the reporting effort is going.
Why Does It Say Loading, or Show an Error?
Three failure modes account for most of what goes wrong, and all three are documented rather than mysterious.
The first is the time limit, which is tighter than most Apps Script limits people have met elsewhere: "A custom function call must return within 30 seconds." Exceed it and the cell shows #ERROR! with a note reading "Exceeded maximum execution time (line 0)."
The second is volume, and it is the reason the array version matters. Google explains the mechanism: "Each time a custom function is used in a spreadsheet, Sheets makes a separate call to the Apps Script server." A thousand cells means a thousand calls, which is why the documentation pushes array-based functions for large ranges.
The third is a function that never resolves at all. Non-deterministic functions such as NOW and RAND "are not allowed as arguments to a custom function," and a custom function whose result depends on them displays "Loading..." indefinitely rather than failing cleanly.
Recalculation is the subtler one. For a custom function to re-run when your data changes, the cell or range has to be passed in directly as an argument. Reading values inside the function with getValue() does not pass a range, so Sheets has nothing to watch and the stale result sits there.
Overflow has its own rule too. A function returning a two-dimensional array spills into adjacent cells, but only into empty ones: where the spill would overwrite existing content, Google says "the custom function throws an error instead."
When Should You Use a Named Function Instead?
Reach for a named function whenever the logic you want to package is already expressible as a Sheets formula. Wrapping =NOT(ISERROR(MATCH(query,range,0))) as CONTAINS costs you one dialog and no code.
Creating one takes the same shape as the script route but without an editor. Open Data > Named functions, add a new function, then supply a name, a description, any argument placeholders and the formula definition. A formula already sitting in a cell can be converted in place by right-clicking it and choosing View more cell actions > Define named function.
Named functions also avoid two of the Apps Script route's costs. Google's own comparison notes that they "work seamlessly without requiring script authorization or exceeding daily quotas," which removes both the permission question and the quota question in one move.
They are file-scoped, which cuts both ways. Google states it clearly: "Named functions created in a Sheets file are available only in that file." To reuse one, you import it through Data > Named functions > Import function, and an imported function with a matching name overrides the one already there.
Two behaviors are worth knowing before you build on them. Ranges written into a definition become absolute, so pass relative ranges as arguments instead. And recursion works — Google's own example has a function calling itself — though no depth limit is published.
The honest dividing line: named functions for formula logic you want to name and reuse, Apps Script for anything needing a loop, an external request through URL Fetch, or string handling that formulas make painful.
What Google's Own Pages Never Tell You
Several things you would expect to be documented simply are not, and knowing which ones saves you from trusting a number someone invented.
Caching is the big one. The Cache service is on the approved list for custom functions, but nowhere in this documentation does Google state whether Sheets itself caches a custom function's results between recalculations. Any confident claim about that is somebody's inference.
Triggers are the second. The custom-function documentation does not mention installable or simple triggers in relation to custom functions at all, so the common claim that custom functions cannot be used with triggers is not something Google states on these pages.
Named-function limits are the third and most surprising. Google publishes no maximum number of named functions per spreadsheet, no cap on argument placeholders and no recursion depth. The only related failure is a "Calculation limit was reached..." error when a definition is too heavy, and the advice is simply to simplify the formula.
Nor does Google say whether the 30-second rule applies to named functions, or publish the numeric "daily quotas" that its own comparison references when recommending named functions over scripts.
That is four open questions on Google's own pages, each dated September 3, 2026 at the time of writing. Where a figure is not published, the useful move is to design around the uncertainty rather than quote a number you cannot support.
Custom Functions vs the Alternatives
Against nested formulas, a custom function wins on readability and loses on portability. A six-level nested IF is horrible to read but travels anywhere; a custom function reads beautifully and stops working the moment the data moves to a file without the script.
Against a macro, the comparison is about direction. A macro performs actions on the sheet, which is exactly what a custom function is forbidden to do. If your goal is to change cells rather than compute a value, you want a macro or a menu-driven script, and a custom function will fight you.
Against a full Apps Script project behind a custom menu, the custom function is the smaller tool by design. The menu route is how you reach the services custom functions cannot touch, which is the same pattern that makes something like a mail merge from Google Sheets possible when a cell formula could never send an email.
Against a Marketplace add-on, you are choosing between maintaining code and trusting someone else's. An add-on supplies functions without any work from you, at the cost of a dependency whose data handling and life span are not yours to control.
Against moving the calculation upstream into your database or BI tool, the custom function is the fast answer and usually the right one below a certain scale. Once the same logic needs to exist in three spreadsheets and a dashboard, the spreadsheet is the wrong home for it.
Want a second opinion on where your reporting logic should live? Get a free audit from SEO Stuff.
Final Thoughts
Custom functions are one of the few Google Sheets features where the ceiling is genuinely high and the floor is genuinely low. Twelve lines of JavaScript and a @customfunction tag gets you a function your whole team can call.
What catches people out is never the writing. It is the authorization boundary, the 30-second limit and the one-call-per-cell behavior, all three of which Google documents and almost nobody reads before building something that needs them.
Decide the route first. If a Sheets formula can already express the logic, name it and move on. If it cannot, open the script editor and accept the constraints, including that the thing you build lives in exactly one file until you deliberately move it.
Want this done for you?
SEO Stuff gets businesses cited by ChatGPT, Gemini, Perplexity and Claude, and ranking in Google. Start with the free audit, a call, or the package.
See where you stand
A free, manual SEO + AI search audit of your site, with a report within 48 hours. No credit card, no sales call.
Get my free auditTalk it through
A free 15-minute call with the founder about whether AI search is a meaningful opportunity for your business.
Free strategy callHave it done
The Done-For-You Package: audit, 10 pages of content, 3 DR50+ placements, dashboard. $999 once, delivered in 21 business days.
See the package

