{"id":4156,"date":"2023-04-22T04:36:33","date_gmt":"2023-04-22T04:36:33","guid":{"rendered":"https:\/\/rengga.dev\/blog\/?p=4156"},"modified":"2023-06-10T11:31:18","modified_gmt":"2023-06-10T11:31:18","slug":"custom-functions-for-google-sheets-using-apps-script","status":"publish","type":"post","link":"https:\/\/rengga.dev\/blog\/custom-functions-for-google-sheets-using-apps-script\/","title":{"rendered":"GAS &#8211; Custom functions for Google Sheets using Apps Script"},"content":{"rendered":"<p>If you&#8217;re familiar with Google Sheets, you&#8217;ve probably used functions such as SUM, AVERAGE, COUNT, etc. in formulas.<\/p>\n<p class=\"para\">Using Apps Script, you can also write your own functions. These are called custom functions. Once you create these functions, you can use them in Sheets just like any other function.<\/p>\n<h2 class=\"content-header\">What is a function?<\/h2>\n<p>A function is a piece of code that has a name. You can run (or execute) this piece of code elsewhere in your program by using its name. This enables reuse of code. Instead of typing the same code in multiple places, you can define a function in one place and then run it from other places in your program.<\/p>\n<p class=\"para\">Functions in Apps Script have the following structure.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">function functionName (inputValues) {\r\n \r\n   return outputValue;\r\n\r\n}<\/pre>\n<p>The words\u00a0<code class=\"inline-code\">function<\/code>\u00a0and\u00a0<code class=\"inline-code\">return<\/code>\u00a0are called keywords.<\/p>\n<p class=\"para\">The keyword function tells Apps Script that you are about to define a function. A function can accept input values and it can also return an output value. The return keyword tells Apps Script that you&#8217;re about to return a value from the function. A function can only return a maximum of one value but you can have functions that don&#8217;t return any value.<\/p>\n<p class=\"para\">If the function accepts multiple input values, use a comma to separate them.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">function functionName (inputValue1, inputValue2, inputValue3) {\r\n   \/\/The input values are processed to generate the output\r\n   return outputValue;\r\n\r\n}<\/pre>\n<p>Here is a diagram that visualizes what the function is doing.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">function functionName (inputValue1, inputValue2, inputValue3) {\r\n\r\n}<\/pre>\n<p class=\"para\">There are functions that don&#8217;t need input values. Similarly, you can have functions that don&#8217;t return any value. It all depends on what the function does.<\/p>\n<p class=\"para\">A function that does not accept any input value but returns an outputValue.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">function functionName () {\r\n\r\n   return outputValue;\r\n\r\n}<\/pre>\n<p>A function that neither accepts input values nor returns any value.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">function functionName () {\r\n\r\n}<\/pre>\n<h2 class=\"content-header\">Naming your function<\/h2>\n<p class=\"para\">You must follow certain rules while naming your function.<\/p>\n<ul class=\"list\">\n<li class=\"list-item\">Function names can only contain letters, numbers, the dollar sign ($) and underscores.<\/li>\n<li class=\"list-item\">Function names cannot be a keyword.<\/li>\n<li class=\"list-item\">Function names cannot start with a number.<\/li>\n<\/ul>\n<p class=\"para\">Remember that function names in Apps Script are case-sensitive. So, MYFUNCTION is not the same as myFunction.<\/p>\n<p class=\"para\">Here are some best practices for naming your function.<\/p>\n<ul class=\"list\">\n<li class=\"list-item\">Use a descriptive name to help future readers of your code understand what the function does.<\/li>\n<li class=\"list-item\">If the name is made up of several words, either use\u00a0<a class=\"anchor\" href=\"https:\/\/en.wikipedia.org\/wiki\/Camel_case\" target=\"_blank\" rel=\"noopener\">camel case<\/a>\u00a0or use underscores to separate the words. If you choose to separate the words using underscores, ensure that the words are in lower case.<\/li>\n<\/ul>\n<h2 class=\"content-header\">Writing your first custom function<\/h2>\n<p>You&#8217;re going to write a function that returns a number that you believe will bring you luck. Let&#8217;s start by documenting a few details.<\/p>\n<ul class=\"list\">\n<li class=\"list-item\">What the function should do: It will return a number.<\/li>\n<li class=\"list-item\">Function name: Pick a descriptive name (eg. myLuckyNumber).<\/li>\n<li class=\"list-item\">Input values: This function does not need any input values.<\/li>\n<li class=\"list-item\">Output value: A number (your lucky number).<\/li>\n<\/ul>\n<p class=\"para\">I think the number 3 brings me luck so here is a function that returns my lucky number.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">function myLuckyNumber () {\r\n   return 3;\r\n}<\/pre>\n<p>Here is a screenshot of the above function in the Apps Script editor.<\/p>\n<figure id=\"attachment_4157\" aria-describedby=\"caption-attachment-4157\" style=\"width: 970px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-4157 size-large\" src=\"https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/04\/spreadsheetdev_1JQd-ovkUiKsiaqtTno98V9-LmZKP9dfeXEr0Gex_RqM_4128-1024x342.png\" alt=\"GAS - Custom functions for Google Sheets using Apps Script\" width=\"970\" height=\"324\" srcset=\"https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/04\/spreadsheetdev_1JQd-ovkUiKsiaqtTno98V9-LmZKP9dfeXEr0Gex_RqM_4128-1024x342.png 1024w, https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/04\/spreadsheetdev_1JQd-ovkUiKsiaqtTno98V9-LmZKP9dfeXEr0Gex_RqM_4128-300x100.png 300w, https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/04\/spreadsheetdev_1JQd-ovkUiKsiaqtTno98V9-LmZKP9dfeXEr0Gex_RqM_4128-150x50.png 150w, https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/04\/spreadsheetdev_1JQd-ovkUiKsiaqtTno98V9-LmZKP9dfeXEr0Gex_RqM_4128-768x256.png 768w, https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/04\/spreadsheetdev_1JQd-ovkUiKsiaqtTno98V9-LmZKP9dfeXEr0Gex_RqM_4128-1536x512.png 1536w, https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/04\/spreadsheetdev_1JQd-ovkUiKsiaqtTno98V9-LmZKP9dfeXEr0Gex_RqM_4128.png 2048w\" sizes=\"auto, (max-width: 970px) 100vw, 970px\" \/><figcaption id=\"caption-attachment-4157\" class=\"wp-caption-text\">GAS &#8211; Custom functions for Google Sheets using Apps Script<\/figcaption><\/figure>\n<p>You can now use this custom function in a Google Sheets formula just like any other function.<\/p>\n<figure id=\"attachment_4158\" aria-describedby=\"caption-attachment-4158\" style=\"width: 970px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-4158 size-large\" src=\"https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/04\/spreadsheetdev_1JQd-ovkUiKsiaqtTno98V9-LmZKP9dfeXEr0Gex_RqM_8741-1024x582.png\" alt=\"GAS - Custom functions for Google Sheets using Apps Script\" width=\"970\" height=\"551\" srcset=\"https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/04\/spreadsheetdev_1JQd-ovkUiKsiaqtTno98V9-LmZKP9dfeXEr0Gex_RqM_8741-1024x582.png 1024w, https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/04\/spreadsheetdev_1JQd-ovkUiKsiaqtTno98V9-LmZKP9dfeXEr0Gex_RqM_8741-300x171.png 300w, https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/04\/spreadsheetdev_1JQd-ovkUiKsiaqtTno98V9-LmZKP9dfeXEr0Gex_RqM_8741-150x85.png 150w, https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/04\/spreadsheetdev_1JQd-ovkUiKsiaqtTno98V9-LmZKP9dfeXEr0Gex_RqM_8741-768x437.png 768w, https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/04\/spreadsheetdev_1JQd-ovkUiKsiaqtTno98V9-LmZKP9dfeXEr0Gex_RqM_8741.png 1512w\" sizes=\"auto, (max-width: 970px) 100vw, 970px\" \/><figcaption id=\"caption-attachment-4158\" class=\"wp-caption-text\">GAS &#8211; Custom functions for Google Sheets using Apps Script<\/figcaption><\/figure>\n<p>You should see your lucky number displayed in the cell where you entered the formula.<\/p>\n<figure id=\"attachment_4159\" aria-describedby=\"caption-attachment-4159\" style=\"width: 970px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-4159 size-large\" src=\"https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/04\/spreadsheetdev_1JQd-ovkUiKsiaqtTno98V9-LmZKP9dfeXEr0Gex_RqM_7811-1024x557.png\" alt=\"GAS - Custom functions for Google Sheets using Apps Script\" width=\"970\" height=\"528\" srcset=\"https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/04\/spreadsheetdev_1JQd-ovkUiKsiaqtTno98V9-LmZKP9dfeXEr0Gex_RqM_7811-1024x557.png 1024w, https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/04\/spreadsheetdev_1JQd-ovkUiKsiaqtTno98V9-LmZKP9dfeXEr0Gex_RqM_7811-300x163.png 300w, https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/04\/spreadsheetdev_1JQd-ovkUiKsiaqtTno98V9-LmZKP9dfeXEr0Gex_RqM_7811-150x82.png 150w, https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/04\/spreadsheetdev_1JQd-ovkUiKsiaqtTno98V9-LmZKP9dfeXEr0Gex_RqM_7811-768x418.png 768w, https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/04\/spreadsheetdev_1JQd-ovkUiKsiaqtTno98V9-LmZKP9dfeXEr0Gex_RqM_7811-1536x835.png 1536w, https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/04\/spreadsheetdev_1JQd-ovkUiKsiaqtTno98V9-LmZKP9dfeXEr0Gex_RqM_7811.png 1596w\" sizes=\"auto, (max-width: 970px) 100vw, 970px\" \/><figcaption id=\"caption-attachment-4159\" class=\"wp-caption-text\">GAS &#8211; Custom functions for Google Sheets using Apps Script<\/figcaption><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>If you&#8217;re familiar with Google Sheets, you&#8217;ve probably used functions such as <a class=\"read-more\" href=\"https:\/\/rengga.dev\/blog\/custom-functions-for-google-sheets-using-apps-script\/\" title=\"GAS &#8211; Custom functions for Google Sheets using Apps Script\" itemprop=\"url\"><\/a><\/p>\n","protected":false},"author":1,"featured_media":4379,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[398,12],"tags":[503,399,400,402,401,403],"newstopic":[588],"class_list":{"0":"post-4156","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-google-apps-script","8":"category-web-development","9":"tag-custom-functions-for-google-sheets","10":"tag-gas","11":"tag-google-apps-script","12":"tag-google-apps-script-free-code","13":"tag-google-apps-script-sample","14":"tag-google-apps-script-source","15":"newstopic-google-app-script"},"_links":{"self":[{"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/posts\/4156","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/comments?post=4156"}],"version-history":[{"count":2,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/posts\/4156\/revisions"}],"predecessor-version":[{"id":4383,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/posts\/4156\/revisions\/4383"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/media\/4379"}],"wp:attachment":[{"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/media?parent=4156"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/categories?post=4156"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/tags?post=4156"},{"taxonomy":"newstopic","embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/newstopic?post=4156"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}