{"id":50318,"date":"2024-08-13T22:23:06","date_gmt":"2024-08-13T20:23:06","guid":{"rendered":"https:\/\/blog.sheetgo.com\/?p=50318"},"modified":"2026-10-28T15:33:08","modified_gmt":"2026-10-28T14:33:08","slug":"utiliser-le-script-google-apps-dans-google-sheets","status":"publish","type":"post","link":"https:\/\/www.sheetgo.com\/fr\/blog\/google-sheets-features\/use-google-apps-script-in-google-sheets\/","title":{"rendered":"How to use Google Apps Script in Google Sheets? [2026 tutorial for beginners]"},"content":{"rendered":"<p>[et_pb_section fb_built=&#8221;1&#8243; admin_label=&#8221;section&#8221; _builder_version=&#8221;4.16&#8243; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221; da_is_popup=&#8221;off&#8221; da_exit_intent=&#8221;off&#8221; da_has_close=&#8221;on&#8221; da_alt_close=&#8221;off&#8221; da_dark_close=&#8221;off&#8221; da_not_modal=&#8221;on&#8221; da_is_singular=&#8221;off&#8221; da_with_loader=&#8221;off&#8221; da_has_shadow=&#8221;on&#8221; da_disable_devices=&#8221;off|off|off&#8221;][et_pb_row admin_label=&#8221;row&#8221; _builder_version=&#8221;4.16&#8243; background_size=&#8221;initial&#8221; background_position=&#8221;top_left&#8221; background_repeat=&#8221;repeat&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;][et_pb_column type=&#8221;4_4&#8243; _builder_version=&#8221;4.16&#8243; custom_padding=&#8221;|||&#8221; global_colors_info=&#8221;{}&#8221; custom_padding__hover=&#8221;|||&#8221; theme_builder_area=&#8221;post_content&#8221;][et_pb_text _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<p>This article will teach you how to use Google Apps Script in Google Sheets. We will share script examples and how they affect spreadsheets. We will also explain some key concepts like Custom Functions and Data Types.<\/p>\n<p>Let\u2019s get right into it!<\/p>\n<p><b>Key Takeaways:<\/b><\/p>\n<ul>\n<li>In Google Sheets, Apps Script can be used to automate data entry, generate reports, and clean and format data, among other things.<\/li>\n<li>To use Google Apps Script in Google Sheets, go to <b>Extension &gt; Apps Script<\/b> and type or paste your code.<\/li>\n<li>Key concepts in Apps Script include Custom functions, Triggers, Properties and Methods, Apps Script Services, Error Handling, and Libraries.<\/li>\n<\/ul>\n<p>[\/et_pb_text][et_pb_text module_id=&#8221;what&#8221; _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<h2><b>What is Google Apps Script?<\/b><\/h2>\n<p>Google Apps Script is a cloud-based platform that lets you create your own programs to work with your Google apps (like Gmail, Docs, and Sheets). These programs perform tasks automatically. They can send emails or organize data. They can also add new features to your apps, like custom menus or buttons.<b><\/b><\/p>\n<p>Google Apps Script uses a simple programming language called JavaScript. You don&#8217;t need to be an expert coder to write it, though.<b><\/b><\/p>\n<p>In simple terms, this is how Google Apps Scripts works:<b><\/b><\/p>\n<ol>\n<li>You write simple instructions (code) in the Apps Script editor.<\/li>\n<li>Google&#8217;s servers read your instructions and perform the tasks you want.<\/li>\n<li>The results appear in your Google apps. These results could be new menus, automated tasks, or custom apps.<\/li>\n<\/ol>\n<p>Now, how to use Apps Script in Google Sheets?<\/p>\n<p>[\/et_pb_text][et_pb_image src=&#8221;https:\/\/static.sheetgo.com\/wp-content\/uploads\/2024\/08\/google_apps_scripts_google_sheets_1.webp&#8221; alt=&#8221;Apps Script Google Sheets \u2014 new Untitled project editor with empty myFunction in Code.gs&#8221; title_text=&#8221;New Untitled project editor with empty myFunction in Code.gs&#8221; _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;][\/et_pb_image][et_pb_text module_id=&#8221;how&#8221; _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<h2><b>How to use Google Apps Script in Google Sheets?<\/b><\/h2>\n<ol>\n<li> Open your Google Sheet.<\/li>\n<li> Click on \u201c<b>Extensions<\/b>\u201c in the menu bar and select \u201c<b>Apps Script<\/b>\u201d. This will open a new tab with the Apps Script editor.<\/li>\n<\/ol>\n<p>[\/et_pb_text][et_pb_image src=&#8221;https:\/\/static.sheetgo.com\/wp-content\/uploads\/2024\/08\/google_apps_scripts_google_sheets_2.webp&#8221; alt=&#8221;Apps Script Google Sheets \u2014 Extensions menu open with Apps Script highlighted in a blank spreadsheet&#8221; title_text=&#8221;Extensions menu open with Apps Script highlighted in a blank spreadsheet&#8221; _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;][\/et_pb_image][et_pb_text _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<p>3. You will see Code.gs. This is the main file where you&#8217;ll write your JavaScript code. It\u2019s located in the \u201cEditor\u201d menu.<\/p>\n<p>[\/et_pb_text][et_pb_image src=&#8221;https:\/\/static.sheetgo.com\/wp-content\/uploads\/2024\/08\/google_apps_scripts_google_sheets_3.webp&#8221; alt=&#8221;Google Apps Script in Google Sheets \u2014 Code.gs file highlighted in the editor with an empty function&#8221; title_text=&#8221;Code.gs file highlighted in the editor with an empty function&#8221; _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;][\/et_pb_image][et_pb_text _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<p>4. Type in or paste your code. You might need to learn Javascript to know how to write code, or you <a href=\"https:\/\/www.sheetgo.com\/blog\/google-sheets-formulas\/how-to-generate-google-sheets-formulas-with-ai\/\">can generate code with Gemini or ChatGPT<\/a>. But here is the script we\u2019ll use for this tutorial:<\/p>\n<p>[\/et_pb_text][et_pb_text module_class=&#8221;spreadsheet-function&#8221; _builder_version=&#8221;4.27.4&#8243; _module_preset=&#8221;default&#8221; hover_enabled=&#8221;0&#8243; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221; sticky_enabled=&#8221;0&#8243;]function sendEmails() {<br \/>\nconst sheet = SpreadsheetApp.getActiveSheet();<br \/>\nconst data = sheet.getDataRange().getValues(); \/\/ Get all data<br \/>\nconst headers = data.shift(); \/\/ Remove and store headers<br \/>\nfor (let i = 1; i &lt; data.length; i++) {<br \/>\nconst row = data[i];<br \/>\nconst emailAddress = row[headers.indexOf(&#8220;Email&#8221;)]; \/\/ Get email from column<br \/>\nconst subject = &#8220;Automated Email&#8221;; \/\/ Customize subject<br \/>\nconst message = `Hello ${row[headers.indexOf(&#8220;Name&#8221;)]}, this is an automated email from Google Sheets.`; \/\/ Customize message<br \/>\nif (emailAddress) { \/\/ Check if email exists<br \/>\nMailApp.sendEmail({<br \/>\nto: emailAddress,<br \/>\nsubject: subject,<br \/>\nbody: message<br \/>\n});<br \/>\n}<br \/>\n}<br \/>\n}[\/et_pb_text][et_pb_image src=&#8221;https:\/\/static.sheetgo.com\/wp-content\/uploads\/2024\/08\/google_apps_scripts_google_sheets_4.webp&#8221; alt=&#8221;Google Apps Script in Google Sheets \u2014 sendEmails function pasted into Code.gs that emails each row&#8221; title_text=&#8221;SendEmails function pasted into Code.gs that emails each row&#8221; _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;][\/et_pb_image][et_pb_text _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<p>What\u2019s the scenario behind this script? Let\u2019s assume we have a contact list in a Google Sheet and want to use it for a basic email campaign. The script will fetch all the necessary data, such as email and name, to compose and send emails.<\/p>\n<p>[\/et_pb_text][et_pb_image src=&#8221;https:\/\/static.sheetgo.com\/wp-content\/uploads\/2024\/08\/google_apps_scripts_google_sheets_5.webp&#8221; alt=&#8221;Apps Script Google Sheets \u2014 Email Campaign sheet with Email, Name and Other Data columns&#8221; title_text=&#8221;Email Campaign sheet with Email, Name and Other Data columns&#8221; _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;][\/et_pb_image][et_pb_text _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<p>5. Click the \u201c<b>Save<\/b>\u201d icon to save the script. <\/p>\n<p>[\/et_pb_text][et_pb_image src=&#8221;https:\/\/static.sheetgo.com\/wp-content\/uploads\/2024\/08\/google_apps_scripts_google_sheets_6.webp&#8221; alt=&#8221;Apps Script Google Sheets \u2014 Save project icon highlighted above the sendEmails code&#8221; title_text=&#8221;Save project icon highlighted above the sendEmails code&#8221; _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;][\/et_pb_image][et_pb_text _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<p>6. In the top left corner, rename your project and click \u201c<b>Run<\/b>.\u201d The script will send emails to all addresses in your sheet.<\/p>\n<p>[\/et_pb_text][et_pb_image src=&#8221;https:\/\/static.sheetgo.com\/wp-content\/uploads\/2024\/08\/google_apps_scripts_google_sheets_7.webp&#8221; alt=&#8221;Apps Script Google Sheets \u2014 project renamed Email Campaign with the Run button highlighted&#8221; title_text=&#8221;Project renamed Email Campaign with the Run button highlighted&#8221; _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;][\/et_pb_image][et_pb_text _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<p>7. Apps Script scripts require authorization to access your Google Sheets data. You&#8217;ll be prompted to grant these permissions when you run a script.<\/p>\n<p>[\/et_pb_text][et_pb_image src=&#8221;https:\/\/static.sheetgo.com\/wp-content\/uploads\/2024\/08\/google_apps_scripts_google_sheets_8.webp&#8221; alt=&#8221;Apps Script permissions \u2014 Authorization required dialog with Review permissions button&#8221; title_text=&#8221;Authorization required dialog with Review permissions button&#8221; _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;][\/et_pb_image][et_pb_text _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<p>8. You can also set up a trigger to run the script automatically on a schedule (e.g., daily or weekly). To do that, hover over your mouse on the left icons and click \u201c<b>Triggers<\/b>.\u201d<\/p>\n<p>[\/et_pb_text][et_pb_image src=&#8221;https:\/\/static.sheetgo.com\/wp-content\/uploads\/2024\/08\/google_apps_scripts_google_sheets_9.webp&#8221; alt=&#8221;Apps Script trigger \u2014 Execution log shows run completed, Triggers menu item highlighted&#8221; title_text=&#8221;Execution log shows run completed, Triggers menu item highlighted&#8221; _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;][\/et_pb_image][et_pb_text _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<p>9. Click the \u201c<b>+ Add Trigger<\/b>\u201d button.<\/p>\n<p>[\/et_pb_text][et_pb_image src=&#8221;https:\/\/static.sheetgo.com\/wp-content\/uploads\/2024\/08\/google_apps_scripts_google_sheets_10.webp&#8221; alt=&#8221;Apps Script trigger \u2014 empty Triggers page with the Add Trigger button highlighted&#8221; title_text=&#8221;Empty Triggers page with the Add Trigger button highlighted&#8221; _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;][\/et_pb_image][et_pb_text _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<p>10. Go to event source and choose &#8220;<b>Time-driven<\/b>.&#8221;<\/p>\n<p>[\/et_pb_text][et_pb_image src=&#8221;https:\/\/static.sheetgo.com\/wp-content\/uploads\/2024\/08\/google_apps_scripts_google_sheets_11.webp&#8221; alt=&#8221;Apps Script trigger \u2014 Add Trigger dialog with Time-driven selected as event source&#8221; title_text=&#8221;Add Trigger dialog with Time-driven selected as event source&#8221; _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;][\/et_pb_image][et_pb_text _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<p>11. Select a time-based type.<\/p>\n<p>[\/et_pb_text][et_pb_image src=&#8221;https:\/\/static.sheetgo.com\/wp-content\/uploads\/2024\/08\/google_apps_scripts_google_sheets_12.webp&#8221; alt=&#8221;Apps Script trigger \u2014 time-based trigger type list with Minutes timer highlighted&#8221; title_text=&#8221;Time-based trigger type list with Minutes timer highlighted&#8221; _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;][\/et_pb_image][et_pb_text _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<ul>\n<li><b>Specific date and time:<\/b> For a one-time trigger.<\/li>\n<li><b>Minutes timer:<\/b> To run at a specified interval in minutes (e.g., every 15 minutes).<\/li>\n<li><b>Hour timer:<\/b> To run at a specified interval in hours (e.g., every 3 hours).<\/li>\n<li><b>Day timer:<\/b> To run daily at a specific time.<\/li>\n<li><b>Week timer:<\/b> To run on specific days of the week at a specific time.<\/li>\n<li><b>Month timer:<\/b> To run on specific days of the month at a specific time.<\/li>\n<\/ul>\n<p>12. Click &#8220;<b>Save<\/b>.&#8221; The trigger is now active.<\/p>\n<p>[\/et_pb_text][et_pb_image src=&#8221;https:\/\/static.sheetgo.com\/wp-content\/uploads\/2024\/08\/google_apps_scripts_google_sheets_13.webp&#8221; alt=&#8221;Apps Script trigger \u2014 sendEmails set to run every 15 minutes with Save button highlighted&#8221; title_text=&#8221;SendEmails set to run every 15 minutes with Save button highlighted&#8221; _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;][\/et_pb_image][et_pb_text _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<p>Here is the email I received after running the script.<\/p>\n<p>[\/et_pb_text][et_pb_image src=&#8221;https:\/\/static.sheetgo.com\/wp-content\/uploads\/2024\/08\/google_apps_scripts_google_sheets_14.webp&#8221; alt=&#8221;Google Apps Script in Google Sheets \u2014 Gmail inbox showing the Automated Email sent by the script&#8221; title_text=&#8221;Gmail inbox showing the Automated Email sent by the script&#8221; _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;][\/et_pb_image][et_pb_text _builder_version=&#8221;4.27.4&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<p>There you go! You now know how to use Google Apps Script in Google Sheets. But let\u2019s discuss more use cases and see how Apps Script enhances spreadsheets.<\/p>\n<\/p>\n<p>[\/et_pb_text][et_pb_text module_id=&#8221;practical&#8221; _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<h2><b>Practical examples of Apps Scripts in action<\/b><\/h2>\n<h3><b>1. Automate data entry<\/b><\/h3>\n<p>You can use Google Apps Scripts in Google Sheets to automate data entry.<\/p>\n<p>Manual data entry is prone to typos. With <a href=\"https:\/\/www.sheetgo.com\/blog\/how-to-solve-with-sheetgo\/google-sheets-automations\/\">automation<\/a>, you reduce the chances of human mistakes. Apps Script can pull data from other sources (like websites, forms, or emails) and automatically plug it into your spreadsheet.\u00a0<\/p>\n<p>Let&#8217;s say you get emailed receipts for business expenses. Apps Script can scan those emails, extract the relevant numbers (date, amount, category), and neatly organize them in your expense tracker spreadsheet.<\/p>\n<p>Let\u2019s write and explain a script:<\/p>\n<p>[\/et_pb_text][et_pb_text module_class=&#8221;spreadsheet-function&#8221; _builder_version=&#8221;4.27.4&#8243; _module_preset=&#8221;default&#8221; hover_enabled=&#8221;0&#8243; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221; sticky_enabled=&#8221;0&#8243;]function extractExpensesFromEmails() {<\/p>\n<p>\/\/ 1. Get Unread Emails with Receipts<br \/>\nvar label = GmailApp.getUserLabelByName(&#8220;Receipts&#8221;); \/\/ Change &#8220;Receipts&#8221; if your label is different<br \/>\nvar threads = label.getThreads(0, 5); \/\/ Get the first 5 unread threads<\/p>\n<p>\/\/ 2. Process Each Email<br \/>\nfor (var i = 0; i &lt; threads.length; i++) {<br \/>\nvar messages = threads[i].getMessages();<br \/>\nfor (var j = 0; j &lt; messages.length; j++) {<br \/>\nvar message = messages[j];<\/p>\n<p>\/\/ 3. Extract Expense Details (This is a simplified example)<br \/>\nvar body = message.getBody();<br \/>\nvar amountMatch = body.match(\/\\$\\d+\\.\\d{2}\/);\u00a0 \/\/ Look for $ amounts<br \/>\nvar dateMatch = body.match(\/\\d{2}\\\/\\d{2}\\\/\\d{4}\/); \/\/ Look for MM\/DD\/YYYY dates<\/p>\n<p>\/\/ 4. If found, add to Spreadsheet<br \/>\nif (amountMatch &amp;&amp; dateMatch) {<br \/>\nvar amount = amountMatch[0];<br \/>\nvar date = dateMatch[0];<br \/>\nvar sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();<br \/>\nsheet.appendRow([date, amount, message.getSubject()]); \/\/ Adjust columns as needed<br \/>\n}<br \/>\n}<\/p>\n<p>\/\/ 5. Mark Email as Read<br \/>\nthreads[i].markRead();<br \/>\n}<br \/>\n}[\/et_pb_text][et_pb_image src=&#8221;https:\/\/static.sheetgo.com\/wp-content\/uploads\/2024\/08\/google_apps_scripts_google_sheets_15.webp&#8221; alt=&#8221;Google Apps Script in Google Sheets \u2014 extractExpensesFromEmails script reading Gmail receipts&#8221; title_text=&#8221;ExtractExpensesFromEmails script reading Gmail receipts&#8221; _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;][\/et_pb_image][et_pb_text _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<h4><b>What does this script mean?<\/b><\/h4>\n<p>We assume you have a Gmail label called &#8220;Receipts&#8221; where you store expense emails. The script fetches the first 5 unread threads (conversations) from that label. It loops through each thread and its individual messages (emails).<\/p>\n<p>It then extracts expense details. Keep in mind that we&#8217;ve highly simplified this part. The script assumes a specific format for expenses in the email body (e.g., &#8220;$15.99&#8221; and &#8220;07\/25\/2024&#8221;).<\/p>\n<p>Then, if the script finds both the amount and date, it gets the active spreadsheet and adds a new row with the extracted data (and the email subject for reference).<\/p>\n<p>[\/et_pb_text][et_pb_image src=&#8221;https:\/\/static.sheetgo.com\/wp-content\/uploads\/2024\/08\/google_apps_scripts_google_sheets_16.webp&#8221; alt=&#8221;Google Apps Script in Google Sheets \u2014 Business Expenses sheet filled with dates, amounts and vendors&#8221; title_text=&#8221;Business Expenses sheet filled with dates, amounts and vendors&#8221; _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;][\/et_pb_image][et_pb_text _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<p>Finally, after processing, the script marks the email thread as read to avoid duplicates.<\/p>\n<p><b>Note:<\/b> Our script is simplified. The accuracy of the data depends entirely on the email format and the script&#8217;s ability to parse it correctly. Moreover, you might have additional columns in your actual expense tracker (e.g., category, payment method).<\/p>\n<p>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<h3><b>2. Generate Reports<\/b><\/h3>\n<p>You can also use Google Apps Script in Google Sheets to generate reports.<\/p>\n<p>Think about those tedious reports you have to make\u2014maybe sales summaries, project updates, or student grades. Apps Script can take the pain out of this process by pulling that data from a Google Sheet or other places.\u00a0<\/p>\n<p>Let&#8217;s say you track your weekly sales in a Google Sheet. Apps Script can:<\/p>\n<ol>\n<li>Grab the latest sales numbers from your sheet.<\/li>\n<li>Calculate total sales, sales by product, and maybe even growth compared to last week.<\/li>\n<li>Make a <a href=\"https:\/\/www.sheetgo.com\/blog\/google-sheets-features\/how-to-make-a-pie-chart-in-google-sheets\/\">pie chart<\/a> or <a href=\"https:\/\/www.sheetgo.com\/blog\/google-sheets-features\/how-to-make-a-line-graph-in-google-sheets\/\">line graph<\/a> report and a summary.<\/li>\n<li>Email the report to your manager every Monday morning.<\/li>\n<\/ol>\n<p>You set it up once, and then Google Apps Script does the rest \u2013 week after week!<\/p>\n<p>Here is a script example, and we will explain it below.<\/p>\n<p>[\/et_pb_text][et_pb_text module_class=&#8221;spreadsheet-function&#8221; _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<p>function generateSalesReport() {<\/p>\n<p>\u00a0\u00a0\/\/ Get the sales data<\/p>\n<p>\u00a0\u00a0var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(&#8220;WeeklySales&#8221;);<\/p>\n<p>\u00a0\u00a0var data = sheet.getDataRange().getValues();<\/p>\n<p>\u00a0\u00a0\/\/ Calculate total sales and sales by product<\/p>\n<p>\u00a0\u00a0var totalSales = 0;<\/p>\n<p>\u00a0\u00a0var salesByProduct = {};<\/p>\n<p>\u00a0\u00a0for (var i = 1; i &lt; data.length; i++) { \/\/ Skip header row<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0var product = data[i][1];<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0var sales = data[i][2];<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0totalSales += sales;<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0salesByProduct[product] = (salesByProduct[product] || 0) + sales;<\/p>\n<p>\u00a0\u00a0}<\/p>\n<p>\u00a0\u00a0\/\/ Create the report<\/p>\n<p>\u00a0\u00a0var report = &#8220;&lt;h2&gt;Weekly Sales Report&lt;\/h2&gt;\\n\\n&#8221;;<\/p>\n<p>\u00a0\u00a0report += &#8220;&lt;p&gt;Total Sales: $&#8221; + totalSales.toFixed(2) + &#8220;&lt;\/p&gt;\\n\\n&#8221;;<\/p>\n<p>\u00a0\u00a0report += &#8220;&lt;h3&gt;Sales by Product:&lt;\/h3&gt;\\n\\n&#8221;;<\/p>\n<p>\u00a0\u00a0for (var product in salesByProduct) {<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0report += &#8220;&lt;p&gt;&#8221; + product + &#8220;: $&#8221; + salesByProduct[product].toFixed(2) + &#8220;&lt;\/p&gt;\\n\\n&#8221;;<\/p>\n<p>\u00a0\u00a0}<\/p>\n<p>\u00a0\u00a0\/\/ Send the email<\/p>\n<p>\u00a0\u00a0var emailSubject = &#8220;Weekly Sales Report&#8221;;<\/p>\n<p>\u00a0\u00a0var emailBody = report;<\/p>\n<p>\u00a0\u00a0MailApp.sendEmail(&#8220;YOUR_EMAIL_ADDRESS&#8221;, emailSubject, &#8220;&#8221;, {htmlBody: emailBody}); \/\/ Include your email address<\/p>\n<p>}<\/p>\n<p>[\/et_pb_text][et_pb_image src=&#8221;https:\/\/static.sheetgo.com\/wp-content\/uploads\/2024\/08\/google_apps_scripts_google_sheets_17.webp&#8221; alt=&#8221;Google Apps Script in Google Sheets \u2014 generateSalesReport function in the Apps Script editor&#8221; title_text=&#8221;GenerateSalesReport function in the Apps Script editor&#8221; _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;][\/et_pb_image][et_pb_text _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<p><b>Recommended reading:<\/b> <a href=\"https:\/\/www.sheetgo.com\/blog\/sales-processes\/google-sheets-sales-templates\/\">Top 5 Google Sheets sales templates<\/a><\/p>\n<h4><b>What does this script mean?<\/b><\/h4>\n<p>The script fetches all the values from the &#8220;WeeklySales&#8221; sheet. It loops through the data and calculates total sales and sales per product. It then creates an HTML report with the calculated data. Finally, the script uses MailApp.sendEmail to send the report as an email.<\/p>\n<p>[\/et_pb_text][et_pb_text module_class=&#8221;spreadsheet-function&#8221; _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<p>MailApp.sendEmail<\/p>\n<p>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<p>Here is a Google Sheet grouping all sales:<\/p>\n<p>[\/et_pb_text][et_pb_image src=&#8221;https:\/\/static.sheetgo.com\/wp-content\/uploads\/2024\/08\/google_apps_scripts_google_sheets_18.webp&#8221; alt=&#8221;Apps Script Google Sheets \u2014 WeeklySales sheet with Week, Product and Sales columns&#8221; title_text=&#8221;WeeklySales sheet with Week, Product and Sales columns&#8221; _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;][\/et_pb_image][et_pb_text _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<p>Here is what the total weekly sales report looks like in the email:<\/p>\n<p>[\/et_pb_text][et_pb_image src=&#8221;https:\/\/static.sheetgo.com\/wp-content\/uploads\/2024\/08\/google_apps_scripts_google_sheets_19.webp&#8221; alt=&#8221;Google Apps Script in Google Sheets \u2014 Gmail showing the Weekly Sales Report with totals by product&#8221; title_text=&#8221;Gmail showing the Weekly Sales Report with totals by product&#8221; _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;][\/et_pb_image][et_pb_text _builder_version=&#8221;4.27.4&#8243; _module_preset=&#8221;default&#8221; hover_enabled=&#8221;0&#8243; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221; sticky_enabled=&#8221;0&#8243;]<\/p>\n<p><b>Note:<\/b> This is a simplified script. Change &#8220;WeeklySales&#8221; if your sheet has a different name. Replace &#8220;YOUR_EMAIL_ADDRESS&#8221; with the actual recipient\u2019s email address.<\/p>\n<p>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<h3><b>3. Clean and format data<\/b><\/h3>\n<p>Cleaning and formatting data is another way to use Apps Scripts in Google Sheets.<\/p>\n<p>Imagine you have a messy spreadsheet filled with customer information. Maybe some names have extra spaces, dates are in different formats, or some cells have typos. Cleaning this up manually would be a real headache. This is where Apps Scripts come in to save the day.<\/p>\n<p>You can use Google Apps Scripts to scan your data and find and fix specific issues, such as:<\/p>\n<ul>\n<li><b>Extra spaces:<\/b> Automatically remove extra spaces before and after names.<\/li>\n<li><b>Incorrect formats:<\/b> Change dates from &#8220;29\/07\/2024&#8221; to &#8220;July 29, 2024&#8221; so everything matches.<\/li>\n<li><b>Typos:<\/b> If a product name is misspelled, the script can fix it.<\/li>\n<\/ul>\n<p>You can also use Google Apps Scripts to organize information and combine or split data.<\/p>\n<p>Let\u2019s create a Javascript for this example.<\/p>\n<p>Imagine you have a Google Sheet with customer orders. Each row has information like Order ID, Customer Name, Product, Quantity, and Order Date. You want to organize the orders by date, with the most recent orders at the top.<\/p>\n<p>Our script could be something like this:<\/p>\n<p>[\/et_pb_text][et_pb_text module_class=&#8221;spreadsheet-function&#8221; _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<p>function sortSheetByDate() {<\/p>\n<p>\u00a0\u00a0var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();<\/p>\n<p>\u00a0\u00a0var range = sheet.getRange(&#8220;A2:E26&#8221;); \/\/ Adjust the range to cover your data<\/p>\n<p>\u00a0\u00a0\/\/ Get the data<\/p>\n<p>\u00a0\u00a0var data = range.getValues();<\/p>\n<p>\u00a0\u00a0\/\/ Check if dates are in Date object format and convert them to strings<\/p>\n<p>\u00a0\u00a0data.forEach(function(row) {<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0if (Object.prototype.toString.call(row[4]) === &#8216;[object Date]&#8217;) {<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0row[4] = Utilities.formatDate(row[4], SpreadsheetApp.getActiveSpreadsheet().getSpreadsheetTimeZone(), &#8216;dd\/MM\/yyyy&#8217;);<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0}<\/p>\n<p>\u00a0\u00a0});<\/p>\n<p>\u00a0\u00a0\/\/ Parse the dates correctly and sort<\/p>\n<p>\u00a0\u00a0data.sort(function(a, b) {<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0var dateA = new Date(a[4].split(&#8216;\/&#8217;).reverse().join(&#8216;-&#8216;));<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0var dateB = new Date(b[4].split(&#8216;\/&#8217;).reverse().join(&#8216;-&#8216;));<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0return dateB &#8211; dateA; \/\/ Sort from newest to oldest<\/p>\n<p>\u00a0\u00a0});<\/p>\n<p>\u00a0\u00a0\/\/ Set the sorted data back to the sheet<\/p>\n<p>\u00a0\u00a0range.setValues(data);<\/p>\n<p>}<\/p>\n<p>[\/et_pb_text][et_pb_image src=&#8221;https:\/\/static.sheetgo.com\/wp-content\/uploads\/2024\/08\/google_apps_scripts_google_sheets_20.webp&#8221; alt=&#8221;Apps Script Google Sheets \u2014 sortSheetByDate function in Code.gs that sorts rows by date&#8221; title_text=&#8221;SortSheetByDate function in Code.gs that sorts rows by date&#8221; _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;][\/et_pb_image][et_pb_text _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<h4><b>What does this script mean?<\/b><\/h4>\n<p>The script finds the order of information you want to sort. It then carefully takes all the information from each order form \u2013 customer name, order date, etc. \u2013 and puts it into separate cells. This is where the magic happens! The script looks at each order date and finds which orders are newer and which are older.<\/p>\n<p>Data with dates not in chronological order:<\/p>\n<p>[\/et_pb_text][et_pb_image src=&#8221;https:\/\/static.sheetgo.com\/wp-content\/uploads\/2024\/08\/google_apps_scripts_google_sheets_21.webp&#8221; alt=&#8221;Apps Script Google Sheets \u2014 Customer Orders sheet with unsorted order dates&#8221; title_text=&#8221;Customer Orders sheet with unsorted order dates&#8221; _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;][\/et_pb_image][et_pb_text _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<p>Data with dates in chronological order after using the script:<\/p>\n<p>[\/et_pb_text][et_pb_image src=&#8221;https:\/\/static.sheetgo.com\/wp-content\/uploads\/2024\/08\/google_apps_scripts_google_sheets_22.webp&#8221; alt=&#8221;Apps Script Google Sheets \u2014 Customer Orders sheet after the script sorted by newest date first&#8221; title_text=&#8221;Customer Orders sheet after the script sorted by newest date first&#8221; _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;][\/et_pb_image][et_pb_text module_id=&#8221;essential&#8221; _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<h2><b>Essential Apps Script concepts to know and use in Google Sheets<\/b><\/h2>\n<h3><b>1. Custom functions<\/b><\/h3>\n<p>Custom functions allow you to create your own functions that you can use directly in Google Sheets, just like the built-in functions (<a href=\"https:\/\/www.sheetgo.com\/blog\/google-sheets-formulas\/sumif-google-sheets\/\">SUM<\/a>, <a href=\"https:\/\/www.sheetgo.com\/blog\/google-sheets-formulas\/averageifs-formula-google-sheets\/\">AVERAGE<\/a>, etc.). These <a href=\"https:\/\/www.sheetgo.com\/blog\/excel-features\/100-functions-formulas-for-google-sheets-excel-basics\/\">functions<\/a> are written in JavaScript within Apps Script and can perform various tasks, from simple calculations to complex data manipulations.<\/p>\n<p>Here is an example of a custom function:<\/p>\n<p>[\/et_pb_text][et_pb_text module_class=&#8221;spreadsheet-function&#8221; _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<p>function DOUBLE(input) {<\/p>\n<p>\u00a0\u00a0return input * 2;<\/p>\n<p>}<\/p>\n<p>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<p>This custom function doubles numbers in a cell. After adding the custom function to the Script Editor, go back to your spreadsheet.<\/p>\n<p>Let&#8217;s say cell A1 contains the number 580.<\/p>\n<ul>\n<li>Type =DOUBLE(A1) in cell B1.<\/li>\n<\/ul>\n<p>[\/et_pb_text][et_pb_text module_class=&#8221;spreadsheet-function&#8221; _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<p>=DOUBLE(A1)<\/p>\n<p>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<ul>\n<li>Google Sheets runs your function, giving it the value 1160 in cell B1.<\/li>\n<li>The function calculates 580 * 2 = 1160.<\/li>\n<li>Cell B1 displays the result: 1160.<\/li>\n<\/ul>\n<p>[\/et_pb_text][et_pb_image src=&#8221;https:\/\/static.sheetgo.com\/wp-content\/uploads\/2024\/08\/google_apps_scripts_google_sheets_23.webp&#8221; alt=&#8221;Apps Script custom function \u2014 spreadsheet cells A1 and B1 showing 580 and 1160 highlighted&#8221; title_text=&#8221;Spreadsheet cells A1 and B1 showing 580 and 1160 highlighted&#8221; _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;][\/et_pb_image][et_pb_text _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<h4><b>Difference between a custom function and a general code you write in the Google Apps Script<\/b><\/h4>\n<p>The main difference between a custom function and any other code lies in how you use and call them within Google Sheets.<\/p>\n<p>A custom function code <b>must return a value<\/b>, like:<\/p>\n<p>[\/et_pb_text][et_pb_text module_class=&#8221;spreadsheet-function&#8221; _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<p>return input * 2;.<\/p>\n<p>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<p>A function cannot modify any part of the spreadsheet or trigger any other actions. It can only compute and return a result based on its inputs.<\/p>\n<p>Moreover, to use a custom function, you must enter the =functionName() code <b>into a cell<\/b> on the spreadsheet, like:<\/p>\n<p>[\/et_pb_text][et_pb_text module_class=&#8221;spreadsheet-function&#8221; _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<p>\u00a0=DOUBLE(A1) <\/p>\n<p>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<h3><b>2. Triggers<\/b><\/h3>\n<p>You also need to learn about triggers if you want to use Google Apps Script in Google Sheets.<\/p>\n<p>Imagine you have a task in Google Sheets that should happen automatically, without you having to click any buttons. That&#8217;s where triggers come in!\u00a0 A trigger is like a little alarm clock you set up within your Apps Script code. It tells your script to wake up and run a specific part of your code when a particular event happens in your spreadsheet.<\/p>\n<p>You can customize triggers to run your script when a sheet is opened, a cell is edited, a form is submitted, or even on a specific date and time.<\/p>\n<p>[\/et_pb_text][et_pb_image src=&#8221;https:\/\/static.sheetgo.com\/wp-content\/uploads\/2024\/08\/google_apps_scripts_google_sheets_24.webp&#8221; alt=&#8221;Apps Script trigger \u2014 From spreadsheet event type list with On open, On edit, On change, On form submit&#8221; title_text=&#8221;From spreadsheet event type list with On open, On edit, On change, On form submit&#8221; _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;][\/et_pb_image][et_pb_text _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<p>You can also use triggers to keep your data fresh. For example, a trigger could automatically pull in the latest stock prices or weather information.\u00a0<\/p>\n<p>If you&#8217;d rather start a script yourself with one click, you can <a href=\"https:\/\/www.sheetgo.com\/blog\/google-sheets-features\/google-sheets-button\/\">assign it to a button in Google Sheets<\/a>.<\/p>\n<p>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<h3><b>3. Properties and Methods<\/b><\/h3>\n<p>Properties and methods let you change how Google Sheets works. You can write scripts that automatically format cells or move data around. For example, instead of manually changing the color of hundreds of cells, your script can do it instantly.<\/p>\n<p>Think of properties like the traits or attributes of an object. In Google Sheets, this could be:<\/p>\n<ul>\n<li>The background color of a cell:<\/li>\n<\/ul>\n<p>[\/et_pb_text][et_pb_text module_class=&#8221;spreadsheet-function&#8221; _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<p>cell.getBackground()<\/p>\n<p>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<ul>\n<li>The value inside a cell:<\/li>\n<\/ul>\n<p>[\/et_pb_text][et_pb_text module_class=&#8221;spreadsheet-function&#8221; _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<p>cell.getValue()<\/p>\n<p>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<ul>\n<li>The font size of the text:<\/li>\n<\/ul>\n<p>[\/et_pb_text][et_pb_text module_class=&#8221;spreadsheet-function&#8221; _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<p>\u00a0cell.getFontSize()<\/p>\n<p>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<p>You can find out a property&#8217;s current value or set it to change its appearance or behavior.<\/p>\n<p>Methods, however, are like the verbs or actions that an object can perform. For example, a cell in Google Sheets can:<\/p>\n<ul>\n<li>Change its contents:<\/li>\n<\/ul>\n<p>[\/et_pb_text][et_pb_text module_class=&#8221;spreadsheet-function&#8221; _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<p>cell.setValue(&#8220;Hello!&#8221;)<\/p>\n<p>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<ul>\n<li>Clear itself completely:<\/li>\n<\/ul>\n<p>[\/et_pb_text][et_pb_text module_class=&#8221;spreadsheet-function&#8221; _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<p>cell.clearContent()<\/p>\n<p>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<ul>\n<li>Merge with neighboring cells:<\/li>\n<\/ul>\n<p>[\/et_pb_text][et_pb_text module_class=&#8221;spreadsheet-function&#8221; _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<p>cell.mergeAcross()<\/p>\n<p>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<p>You &#8220;call&#8221; a method to make it happen. It&#8217;s like giving an instruction.<\/p>\n<p>Here is a simple script example:<\/p>\n<p>[\/et_pb_text][et_pb_text module_class=&#8221;spreadsheet-function&#8221; _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<p>function formatImportantCells() {<\/p>\n<p>\u00a0\u00a0var sheet = SpreadsheetApp.getActiveSheet();<\/p>\n<p>\u00a0\u00a0var range = sheet.getRange(&#8220;A1:B10&#8221;); \/\/ Get a group of cells<\/p>\n<p>\u00a0\u00a0var cells = range.getCells(); \/\/ Get individual cells<\/p>\n<p>\u00a0\u00a0for (var i = 0; i &lt; cells.length; i++) {<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0if (cells[i].getValue() &gt; 100) { \/\/ Check the value<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0cells[i].setBackground(&#8220;yellow&#8221;); \/\/ Set a property (color)<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0}<\/p>\n<p>\u00a0\u00a0}<\/p>\n<p>}<\/p>\n<p>[\/et_pb_text][et_pb_image src=&#8221;https:\/\/static.sheetgo.com\/wp-content\/uploads\/2024\/08\/google_apps_scripts_google_sheets_25.webp&#8221; alt=&#8221;Google Apps Script in Google Sheets \u2014 formatImportantCells function that colors cells above 100 yellow&#8221; title_text=&#8221;FormatImportantCells function that colors cells above 100 yellow&#8221; _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;][\/et_pb_image][et_pb_text _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<p>This script checks the values in cells A1 through B10. If a value is greater than 100, the script turns the cell&#8217;s background yellow.<\/p>\n<p>In short:<\/p>\n<ul>\n<li><b>Properties describe:<\/b> They tell you about the current state of things.<\/li>\n<li><b>Methods do:<\/b> They make things happen.<\/li>\n<li><b>Dot notation:<\/b> You use a dot to access properties and methods (e.g., cell.getValue()).<\/li>\n<\/ul>\n<p>[\/et_pb_text][et_pb_text module_class=&#8221;spreadsheet-function&#8221; _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<p>cell.getValue()<\/p>\n<p>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<h3><b>4. Data types<\/b><\/h3>\n<p>Data types are another concept you need to learn to use Google Apps Script in Google Sheets.<\/p>\n<p>Data types are labels that tell Apps Script what kind of information it&#8217;s dealing with. Just like you&#8217;d distinguish between apples and oranges, Apps Script needs to know if it&#8217;s working with numbers, text, dates, or other types.<\/p>\n<p>But why do data types matter in Google Sheets?<\/p>\n<p>There are at least three reasons:<\/p>\n<ol>\n<li><b>Correct Calculations:<\/b> Imagine you&#8217;re adding up expenses. If Apps Script thinks your numbers are actually text, you&#8217;ll get weird results. Data types guarantee calculations happen accurately.<\/li>\n<li><b>Proper Formatting:<\/b> You want your dates to look like dates and your phone numbers to behave like phone numbers. Data types help you control how things are displayed and used in your spreadsheet.<\/li>\n<li><b>Error Prevention:<\/b> If you try to use a date as if it were a number, Apps Script might flag an error. Knowing the right data types helps you avoid these hiccups and write code that runs smoothly.<\/li>\n<\/ol>\n<p>Let&#8217;s say you&#8217;re tracking sales and have a column in Google Sheets with the number of items sold. When you use Apps Script to calculate total sales, it needs to understand this column contains numbers, not just random characters.<\/p>\n<p>For instance, the script below mentions the data type:<\/p>\n<p>[\/et_pb_text][et_pb_text module_class=&#8221;spreadsheet-function&#8221; _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<p>if (valueType === &#8220;number&#8221;) {<\/p>\n<p>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<p>Here&#8217;s the breakdown:<\/p>\n<ol>\n<li>valueType: This variable stores the result of typeof value. The typeof operator is a JavaScript feature that directly tells you the data type of a variable.<\/li>\n<li>=== &#8220;number&#8221;: This comparison checks if the valueType is strictly equal to the string &#8220;number&#8221;. If it is, the script knows it&#8217;s dealing with a numerical value and proceeds to add it to the totalSales.<\/li>\n<\/ol>\n<p>We\u2019ve just covered a few Apps Scripts concepts or terms. There are more, though, such as Apps Script Services, Error Handling, and Libraries.<\/p>\n<p>[\/et_pb_text][et_pb_text module_id=&#8221;tips&#8221; _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<h2><b>Tips and best practices for Apps Script<\/b><\/h2>\n<h3><b>1. Learn Javascript<\/b><\/h3>\n<p>Learning JavaScript gives you a solid foundation for understanding how Google Apps Script works. It becomes way easier to figure out code you find online, troubleshoot your own projects, and write more advanced scripts.<\/p>\n<p>Once you&#8217;ve grasped the basics of JavaScript, you&#8217;ll manipulate data in really clever ways and interact with other services on the web.<\/p>\n<p>Moreover, JavaScript is a widely used language, so your new skills won&#8217;t go to waste. You can apply them to other projects beyond Google Apps Script. For instance, you can build simple websites or even work on complex web development projects.<\/p>\n<h3><b>2. Write efficient and maintainable code<\/b><\/h3>\n<p>Your code is like a recipe, and a good recipe is:<\/p>\n<ul>\n<li><b>Efficient:<\/b> It doesn&#8217;t waste ingredients or time. Efficient code runs faster and uses less of your Apps Script quota (the limit on how much your scripts can do).<\/li>\n<li><b>Maintainable:<\/b> It&#8217;s easy to understand and update later. Maintainable code saves you headaches when you need to make changes down the road.<\/li>\n<\/ul>\n<p>Here are a few additional tips for Google Apps Script:<\/p>\n<ol>\n<li>Don&#8217;t name your variables &#8220;x&#8221; or &#8220;temp.&#8221; Choose names that describe what they store. For example, instead of &#8220;data,&#8221; use &#8220;customerNames&#8221; or &#8220;orderDates.&#8221;<\/li>\n<li>Break your code into smaller, reusable functions. Each function should ideally perform one specific task. This will make your code more organized and easier to test.<\/li>\n<li>Things don&#8217;t always go as planned. Use \u201ctry&#8230;catch\u201d blocks to gracefully catch errors, which prevents your script from crashing unexpectedly.\u00a0<\/li>\n<li>If you find yourself writing the same code multiple times, create a function for it. This principle is called &#8220;Don&#8217;t Repeat Yourself&#8221; (DRY).<\/li>\n<\/ol>\n<h3><b>3. Debug smartly<\/b><\/h3>\n<p>In Google Apps Script, bugs are errors or unexpected behavior in your code. Debugging is the process of finding and correcting these issues.<\/p>\n<p>When Apps Script encounters a problem, it usually displays an error message. This message is your first clue. It often tells you what went wrong. You\u2019ll know if you used a variable that doesn&#8217;t exist, divided by zero, or something else. You\u2019ll also know where it went wrong, like the line of code causing trouble.<\/p>\n<p>[\/et_pb_text][et_pb_image src=&#8221;https:\/\/static.sheetgo.com\/wp-content\/uploads\/2024\/08\/google_apps_scripts_google_sheets_26.webp&#8221; alt=&#8221;Apps Script Google Sheets \u2014 Execution log TypeError error highlighted for generateSalesReport&#8221; title_text=&#8221;Execution log TypeError error highlighted for generateSalesReport&#8221; _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;][\/et_pb_image][et_pb_text _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<p>Apps Script has a built-in debugger. It&#8217;s like a more powerful version of \u201cconsole.log().\u201d You can pause your code, step through it line by line, and inspect variables in real time.<\/p>\n<p>Let&#8217;s say you have this code:<\/p>\n<p>[\/et_pb_text][et_pb_text module_class=&#8221;spreadsheet-function&#8221; _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<p>function calculateArea(length, width) {<\/p>\n<p>\u00a0\u00a0var area = length * width;<\/p>\n<p>\u00a0\u00a0console.log(&#8220;The area is: &#8221; + area);\u00a0<\/p>\n<p>}<\/p>\n<p>calculateArea(10,); \/\/ Notice the missing argument for width<\/p>\n<p>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<p>You run it, and it crashes with an error: &#8220;TypeError: Cannot read property &#8216;*&#8217; of undefined.&#8221; What&#8217;s happening?<\/p>\n<ol>\n<li><b>Read the error:<\/b> It says you&#8217;re trying to use the &#8216;*&#8217; (multiplication) operator on something undefined.<\/li>\n<li><b>Check the code:<\/b> You realize you forgot to pass a value for the \u201cwidth\u201d argument.<\/li>\n<\/ol>\n<p><b>Fix the error:<\/b> Change the last line to:<\/p>\n<p>[\/et_pb_text][et_pb_text module_class=&#8221;spreadsheet-function&#8221; _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<p>calculateArea(10, 5);<\/p>\n<p>[\/et_pb_text][et_pb_text module_id=&#8221;use&#8221; _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<h2><b>Use Google Apps Scripts to enhance your Google Sheets<\/b><\/h2>\n<p>Congratulations! You&#8217;ve just unlocked the power of Apps Script within Google Sheets. In this article, we saw some practical use cases and shared script examples. You are now fully equipped to use Google Apps Script in Google Sheets.<\/p>\n<p>By automating tasks, customizing functions, and even building interactive web applications, you&#8217;re well on your way to becoming a spreadsheet ninja. Keep learning, keep scripting, and keep pushing the boundaries of what&#8217;s possible in Google Sheets.<\/p>\n<p>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.22.2&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<h2>Where to go from here<\/h2>\n<p>Once your first script runs, these are good next steps, roughly in this order:<\/p>\n<ul>\n<li>Triggers that run a script on a schedule, when someone edits the sheet, or when a form response arrives.<\/li>\n<li>Custom menus, so teammates can run a script from the Google Sheets menu bar without opening the editor.<\/li>\n<li>Custom functions such as =DOUBLE(A1) that work like built-in formulas.<\/li>\n<li>UrlFetchApp, which pulls data from other services and APIs into your sheet.<\/li>\n<\/ul>\n<p>Google&#8217;s <a href=\"https:\/\/developers.google.com\/apps-script\/reference\" target=\"_blank\" rel=\"noopener\">Apps Script reference<\/a> documents every service and method, and its <a href=\"https:\/\/developers.google.com\/apps-script\/guides\/sheets\" target=\"_blank\" rel=\"noopener\">guide to extending Google Sheets<\/a> has samples you can copy. If you&#8217;d rather not write code for a simple task, you can <a href=\"https:\/\/www.sheetgo.com\/blog\/google-sheets-features\/create-macros-in-google-sheets\/\">record a macro in Google Sheets<\/a> instead, and Apps Script saves it as a script you can edit later.<\/p>\n<p>[\/et_pb_text][et_pb_text module_id=&#8221;faq&#8221; _builder_version=&#8221;4.24.1&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<h2><b>FAQ<\/b><\/h2>\n<p><b>How to add Script Editor to Google Sheets?<\/b><\/p>\n<p>The Script Editor is built into Google Sheets. You don&#8217;t need to add it separately.<\/p>\n<p><b>How to open the Script Editor in Google Sheets?<\/b><\/p>\n<p>Go to Extensions &gt; Apps Script. This will open the Script Editor in a new tab.<\/p>\n<p><b>Where is the Script Editor in Google Sheets?<\/b><\/p>\n<p>The Script Editor is not physically located within your Google Sheet. It opens in a separate tab or window when accessed through the Extensions menu. Go to Extensions &gt; Apps Script.<\/p>\n<p><b>Is Google Apps Script free?<\/b><\/p>\n<p>Yes. Apps Script is free with any Google account, and you don&#8217;t need a paid plan to script Google Sheets. The limits are quotas: a script can run for up to 6 minutes at a time, and triggers on a free Google account get 90 minutes of total runtime a day. Google Workspace accounts get higher quotas.<\/p>\n<p>[\/et_pb_text][\/et_pb_column][\/et_pb_row][\/et_pb_section]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This article will teach you how to use Google Apps Script in Google Sheets. We will share script examples and how they affect spreadsheets. We will also explain some key concepts like Custom Functions and Data Types. Let\u2019s get right into it! Key Takeaways: In Google Sheets, Apps Script can be used to automate data [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":44470,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_et_pb_use_builder":"on","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[37],"tags":[39,28],"class_list":["post-50318","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-google-sheets-features","tag-connections-t","tag-spreadsheets"],"acf":[],"permalink_manager":null,"_links":{"self":[{"href":"https:\/\/www.sheetgo.com\/fr\/wp-json\/wp\/v2\/posts\/50318","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sheetgo.com\/fr\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sheetgo.com\/fr\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sheetgo.com\/fr\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sheetgo.com\/fr\/wp-json\/wp\/v2\/comments?post=50318"}],"version-history":[{"count":0,"href":"https:\/\/www.sheetgo.com\/fr\/wp-json\/wp\/v2\/posts\/50318\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.sheetgo.com\/fr\/wp-json\/wp\/v2\/media\/44470"}],"wp:attachment":[{"href":"https:\/\/www.sheetgo.com\/fr\/wp-json\/wp\/v2\/media?parent=50318"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sheetgo.com\/fr\/wp-json\/wp\/v2\/categories?post=50318"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sheetgo.com\/fr\/wp-json\/wp\/v2\/tags?post=50318"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}