{"id":4107,"date":"2023-03-26T02:39:23","date_gmt":"2023-03-26T02:39:23","guid":{"rendered":"https:\/\/rengga.dev\/blog\/?p=4107"},"modified":"2023-06-10T11:31:31","modified_gmt":"2023-06-10T11:31:31","slug":"gas-creating-your-first-apps-script","status":"publish","type":"post","link":"https:\/\/rengga.dev\/blog\/gas-creating-your-first-apps-script\/","title":{"rendered":"GAS &#8211; Creating your first Apps Script"},"content":{"rendered":"<p class=\"para\">First, go ahead and create a new Google Sheet. I created one called\u00a0<b>First Apps Script<\/b>.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-4108 size-large\" src=\"https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/03\/spreadsheetdev_198RhWcPZvuhpjmcCnP4nuQhr2_Rt73wFit6tUONBaP4_8864-1024x330.png\" alt=\"GAS - Creating your first Apps Script\" width=\"970\" height=\"313\" srcset=\"https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/03\/spreadsheetdev_198RhWcPZvuhpjmcCnP4nuQhr2_Rt73wFit6tUONBaP4_8864-1024x330.png 1024w, https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/03\/spreadsheetdev_198RhWcPZvuhpjmcCnP4nuQhr2_Rt73wFit6tUONBaP4_8864-300x97.png 300w, https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/03\/spreadsheetdev_198RhWcPZvuhpjmcCnP4nuQhr2_Rt73wFit6tUONBaP4_8864-150x48.png 150w, https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/03\/spreadsheetdev_198RhWcPZvuhpjmcCnP4nuQhr2_Rt73wFit6tUONBaP4_8864-768x247.png 768w, https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/03\/spreadsheetdev_198RhWcPZvuhpjmcCnP4nuQhr2_Rt73wFit6tUONBaP4_8864-1536x494.png 1536w, https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/03\/spreadsheetdev_198RhWcPZvuhpjmcCnP4nuQhr2_Rt73wFit6tUONBaP4_8864.png 2048w\" sizes=\"auto, (max-width: 970px) 100vw, 970px\" \/><\/p>\n<p class=\"para\">Then, select\u00a0<b>Extensions &gt; Apps Script.<\/b><\/p>\n<p class=\"para\">This will open a new project in the Apps Script editor.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-4109 size-large\" src=\"https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/03\/spreadsheetdev_198RhWcPZvuhpjmcCnP4nuQhr2_Rt73wFit6tUONBaP4_1812-1024x472.png\" alt=\"GAS - Creating your first Apps Script\" width=\"970\" height=\"447\" srcset=\"https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/03\/spreadsheetdev_198RhWcPZvuhpjmcCnP4nuQhr2_Rt73wFit6tUONBaP4_1812-1024x472.png 1024w, https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/03\/spreadsheetdev_198RhWcPZvuhpjmcCnP4nuQhr2_Rt73wFit6tUONBaP4_1812-300x138.png 300w, https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/03\/spreadsheetdev_198RhWcPZvuhpjmcCnP4nuQhr2_Rt73wFit6tUONBaP4_1812-150x69.png 150w, https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/03\/spreadsheetdev_198RhWcPZvuhpjmcCnP4nuQhr2_Rt73wFit6tUONBaP4_1812-768x354.png 768w, https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/03\/spreadsheetdev_198RhWcPZvuhpjmcCnP4nuQhr2_Rt73wFit6tUONBaP4_1812-1536x708.png 1536w, https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/03\/spreadsheetdev_198RhWcPZvuhpjmcCnP4nuQhr2_Rt73wFit6tUONBaP4_1812.png 1676w\" sizes=\"auto, (max-width: 970px) 100vw, 970px\" \/><\/p>\n<p class=\"para\">Give your project a name by clicking on &#8220;Untitled project&#8221;.<\/p>\n<div style=\"width: 1140px;\" class=\"wp-video\"><video class=\"wp-video-shortcode\" id=\"video-4107-1\" width=\"1140\" height=\"647\" preload=\"metadata\" controls=\"controls\"><source type=\"video\/mp4\" src=\"https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/03\/create-first-apps-script-project-0.mp4?_=1\" \/><a href=\"https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/03\/create-first-apps-script-project-0.mp4\">https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/03\/create-first-apps-script-project-0.mp4<\/a><\/video><\/div>\n<p>&nbsp;<\/p>\n<p class=\"para\">There are three main sections in the Apps Script project UI.<\/p>\n<ul class=\"list\">\n<li class=\"list-item\">\n<p class=\"para\">The files in your project are displayed in a sidebar on the left.<\/p>\n<\/li>\n<li class=\"list-item\">\n<p class=\"para\">If you select a file, its contents will be displayed in a code editor that occupies most of the screen.<\/p>\n<\/li>\n<li class=\"list-item\">\n<p class=\"para\">There is a toolbar above the code editor.<\/p>\n<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-4113 size-large\" src=\"https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/03\/spreadsheetdev_198RhWcPZvuhpjmcCnP4nuQhr2_Rt73wFit6tUONBaP4_3514-1024x565.png\" alt=\"GAS - Creating your first Apps Script\" width=\"970\" height=\"535\" srcset=\"https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/03\/spreadsheetdev_198RhWcPZvuhpjmcCnP4nuQhr2_Rt73wFit6tUONBaP4_3514-1024x565.png 1024w, https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/03\/spreadsheetdev_198RhWcPZvuhpjmcCnP4nuQhr2_Rt73wFit6tUONBaP4_3514-300x166.png 300w, https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/03\/spreadsheetdev_198RhWcPZvuhpjmcCnP4nuQhr2_Rt73wFit6tUONBaP4_3514-150x83.png 150w, https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/03\/spreadsheetdev_198RhWcPZvuhpjmcCnP4nuQhr2_Rt73wFit6tUONBaP4_3514-768x424.png 768w, https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/03\/spreadsheetdev_198RhWcPZvuhpjmcCnP4nuQhr2_Rt73wFit6tUONBaP4_3514.png 1490w\" sizes=\"auto, (max-width: 970px) 100vw, 970px\" \/><\/p>\n<p class=\"para\">You&#8217;ll notice the following code in the editor:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">function myFunction() { \r\n\r\n}<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-4114 size-large\" src=\"https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/03\/spreadsheetdev_198RhWcPZvuhpjmcCnP4nuQhr2_Rt73wFit6tUONBaP4_867-1024x323.png\" alt=\"GAS - Creating your first Apps Script\" width=\"970\" height=\"306\" srcset=\"https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/03\/spreadsheetdev_198RhWcPZvuhpjmcCnP4nuQhr2_Rt73wFit6tUONBaP4_867-1024x323.png 1024w, https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/03\/spreadsheetdev_198RhWcPZvuhpjmcCnP4nuQhr2_Rt73wFit6tUONBaP4_867-300x95.png 300w, https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/03\/spreadsheetdev_198RhWcPZvuhpjmcCnP4nuQhr2_Rt73wFit6tUONBaP4_867-150x47.png 150w, https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/03\/spreadsheetdev_198RhWcPZvuhpjmcCnP4nuQhr2_Rt73wFit6tUONBaP4_867-768x242.png 768w, https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/03\/spreadsheetdev_198RhWcPZvuhpjmcCnP4nuQhr2_Rt73wFit6tUONBaP4_867-1536x484.png 1536w, https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/03\/spreadsheetdev_198RhWcPZvuhpjmcCnP4nuQhr2_Rt73wFit6tUONBaP4_867.png 2018w\" sizes=\"auto, (max-width: 970px) 100vw, 970px\" \/><\/p>\n<p class=\"para\">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. You can also run a function by selecting its name in the dropdown menu in the toolbar and clicking the\u00a0<b>Run<\/b>\u00a0button.<\/p>\n<p class=\"para\">Right now, running this function will not do anything. This is because the function\u00a0<code class=\"inline-code\">myFunction<\/code>\u00a0is empty. There is nothing for it to do when you run it. Let&#8217;s change that.<\/p>\n<p class=\"para\">Add the following code between the two curly braces\u00a0<code class=\"inline-code\">{<\/code>\u00a0and\u00a0<code class=\"inline-code\">}<\/code>.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">Logger.log(101);<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-4115 size-large\" src=\"https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/03\/spreadsheetdev_198RhWcPZvuhpjmcCnP4nuQhr2_Rt73wFit6tUONBaP4_434-1024x323.png\" alt=\"GAS - Creating your first Apps Script\" width=\"970\" height=\"306\" srcset=\"https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/03\/spreadsheetdev_198RhWcPZvuhpjmcCnP4nuQhr2_Rt73wFit6tUONBaP4_434-1024x323.png 1024w, https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/03\/spreadsheetdev_198RhWcPZvuhpjmcCnP4nuQhr2_Rt73wFit6tUONBaP4_434-300x95.png 300w, https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/03\/spreadsheetdev_198RhWcPZvuhpjmcCnP4nuQhr2_Rt73wFit6tUONBaP4_434-150x47.png 150w, https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/03\/spreadsheetdev_198RhWcPZvuhpjmcCnP4nuQhr2_Rt73wFit6tUONBaP4_434-768x242.png 768w, https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/03\/spreadsheetdev_198RhWcPZvuhpjmcCnP4nuQhr2_Rt73wFit6tUONBaP4_434-1536x484.png 1536w, https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/03\/spreadsheetdev_198RhWcPZvuhpjmcCnP4nuQhr2_Rt73wFit6tUONBaP4_434.png 2018w\" sizes=\"auto, (max-width: 970px) 100vw, 970px\" \/><\/p>\n<p>GAS &#8211; Creating your first Apps Script<\/p>\n<p class=\"para\">A log is like a diary for your code. It is used to record what your code did. We&#8217;re using it to record the number 101. That&#8217;s not super useful but that&#8217;s OK. You&#8217;ll eventually learn how to use it.<\/p>\n<div class=\"note\" role=\"alert\">\n<p class=\"para\"><b>Note<\/b><\/p>\n<ul class=\"list\">\n<li class=\"list-item\">\n<p class=\"para\">Ensure that the &#8220;L&#8221; in Logger is in uppercase. Names in JavaScript are case-sensitive. Logger is not the same as logger.<\/p>\n<\/li>\n<li class=\"list-item\">\n<p class=\"para\">Ensure that the line ends with a semicolon (;).<\/p>\n<\/li>\n<\/ul>\n<\/div>\n<p class=\"para\">The editor should have the following code in it.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">function myFunction() { \r\nLogger.log(101); \r\n}<\/pre>\n<p><b>Indent the code inside functions<\/b><\/p>\n<div class=\"note\" role=\"alert\">\n<p class=\"para\">Did you notice that I added two spaces to the left of [code]Logger.log(101);[\/code]? This makes it easier to read the code inside functions (especially when a function has multiple lines of code within it).<\/p>\n<\/div>\n<p class=\"para\">Now save your script and then try running\u00a0<code class=\"inline-code\">myFunction<\/code>\u00a0again by clicking the\u00a0<b>Run<\/b>\u00a0button.<\/p>\n<div style=\"width: 1140px;\" class=\"wp-video\"><video class=\"wp-video-shortcode\" id=\"video-4107-2\" width=\"1140\" height=\"647\" preload=\"metadata\" controls=\"controls\"><source type=\"video\/mp4\" src=\"https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/03\/run-first-apps-script-0.mp4?_=2\" \/><a href=\"https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/03\/run-first-apps-script-0.mp4\">https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/03\/run-first-apps-script-0.mp4<\/a><\/video><\/div>\n<p>&nbsp;<\/p>\n<p class=\"para\">Your script will run and will record\u00a0<code class=\"inline-code\">101.0<\/code>\u00a0to the execution log. You&#8217;ll also see the time at which the log entry was made. Click [X] to close the window.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-4117 size-large\" src=\"https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/03\/spreadsheetdev_198RhWcPZvuhpjmcCnP4nuQhr2_Rt73wFit6tUONBaP4_5709-1024x466.png\" alt=\"GAS - Creating your first Apps Script\" width=\"970\" height=\"441\" srcset=\"https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/03\/spreadsheetdev_198RhWcPZvuhpjmcCnP4nuQhr2_Rt73wFit6tUONBaP4_5709-1024x466.png 1024w, https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/03\/spreadsheetdev_198RhWcPZvuhpjmcCnP4nuQhr2_Rt73wFit6tUONBaP4_5709-300x137.png 300w, https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/03\/spreadsheetdev_198RhWcPZvuhpjmcCnP4nuQhr2_Rt73wFit6tUONBaP4_5709-150x68.png 150w, https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/03\/spreadsheetdev_198RhWcPZvuhpjmcCnP4nuQhr2_Rt73wFit6tUONBaP4_5709-768x350.png 768w, https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/03\/spreadsheetdev_198RhWcPZvuhpjmcCnP4nuQhr2_Rt73wFit6tUONBaP4_5709-1536x699.png 1536w, https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/03\/spreadsheetdev_198RhWcPZvuhpjmcCnP4nuQhr2_Rt73wFit6tUONBaP4_5709.png 2048w\" sizes=\"auto, (max-width: 970px) 100vw, 970px\" \/><\/p>\n<p>GAS &#8211; Creating your first Apps Script<\/p>\n<p class=\"para\">You can open or close the execution log by clicking on the\u00a0<b>Execution log<\/b>\u00a0button.<\/p>\n<div style=\"width: 1140px;\" class=\"wp-video\"><video class=\"wp-video-shortcode\" id=\"video-4107-3\" width=\"1140\" height=\"647\" preload=\"metadata\" controls=\"controls\"><source type=\"video\/mp4\" src=\"https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/03\/toggle-execution-log-apps-script-editor-0.mp4?_=3\" \/><a href=\"https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/03\/toggle-execution-log-apps-script-editor-0.mp4\">https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2023\/03\/toggle-execution-log-apps-script-editor-0.mp4<\/a><\/video><\/div>\n","protected":false},"excerpt":{"rendered":"<p>First, go ahead and create a new Google Sheet. I created one <a class=\"read-more\" href=\"https:\/\/rengga.dev\/blog\/gas-creating-your-first-apps-script\/\" title=\"GAS &#8211; Creating your first 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":[399,400,402,401,403],"newstopic":[588],"class_list":{"0":"post-4107","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-gas","10":"tag-google-apps-script","11":"tag-google-apps-script-free-code","12":"tag-google-apps-script-sample","13":"tag-google-apps-script-source","14":"newstopic-google-app-script"},"_links":{"self":[{"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/posts\/4107","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=4107"}],"version-history":[{"count":9,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/posts\/4107\/revisions"}],"predecessor-version":[{"id":4125,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/posts\/4107\/revisions\/4125"}],"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=4107"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/categories?post=4107"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/tags?post=4107"},{"taxonomy":"newstopic","embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/newstopic?post=4107"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}