Everything about Google Apps Script

What is Google Apps Script & how to use it?

Google Sheets is a popular online spreadsheet application that is used to organize and analyze data. It is widely used by individuals, small businesses, and large organizations. One of the most powerful features of Google Sheets is its ability to be customized with Google Apps Script, which is a scripting language based on JavaScript that allows you to automate tasks and create custom functions within Google Sheets.

Avatar Placeholder
Jil Kiun
March 10th 2023
guide of how to use apps script

Introduction to Google Apps Script

Google Apps Script is a scripting language that is used to extend the functionality of Google's suite of online productivity tools, including Google Sheets, Google Docs, and Google Slides. It is based on JavaScript and is used to automate tasks, create custom functions, and build custom user interfaces within these applications.

Google Apps Script is a powerful tool that allows you to automate repetitive tasks and streamline workflows. It can be used to extract data from Google Sheets, analyze it, and create custom reports. It can also be used to send emails, create custom menus, and interact with external APIs.

Benefits of using Google Apps Script in Google Sheets

There are many benefits to using Google Apps Script in Google Sheets. One of the most significant benefits is the ability to automate tasks and streamline workflows. This can save you a significant amount of time and increase productivity.

Another benefit of using Google Apps Script in Google Sheets is the ability to create custom functions. This can be used to perform complex calculations, manipulate data, and automate tasks.

Google Apps Script can also be used to interact with external APIs. This can be used to extract data from other sources and integrate it into Google Sheets.

How to access Google Apps Script in Google Sheets

Google Apps Script can be accessed from within Google Sheets. To access it, you need to click on the "Tools" menu and select "Script editor."

This will open the Google Apps Script editor, where you can write, edit, and execute their scripts. You can also create new scripts by clicking on "File" and selecting "New script."

Creating custom functions with Google Apps Script

One of the most powerful features of Google Apps Script is the ability to create custom functions. This allows you to perform complex calculations and manipulate data within Google Sheets.

To create a custom function, you need to open the Script editor and create a new function. The function should be defined with the keyword "function," followed by the function name, and any parameters that the function takes. The function body should contain the logic for the function.

Once the function is created, it can be used within Google Sheets by typing the function name into a cell and passing any required parameters.

Automating tasks with Google Apps Script

Google Apps Script can also be used to automate tasks within Google Sheets. This can include tasks such as formatting cells, importing data, and sending emails.

To automate a task, you need to write a script that performs the task and then schedule the script to run automatically. This can be done using the Triggers feature in Google Apps Script.

Triggers can be set to run scripts at specific times, such as every day

Use-cases of Google Apps Script in Google Sheets

Google Apps Script can be used for a wide range of tasks in Google Sheets. Here are a few examples of use-cases:

1. Extracting Data from Google Sheets

Google Apps Script can be used to extract data from Google Sheets and save it to external databases, such as MySQL or PostgreSQL. This can be useful for creating backups of data, or for integrating Google Sheets with other systems.

2. Automating Reports

Google Apps Script can be used to automate the creation of reports in Google Sheets. For example, a script can be written to extract data from multiple sheets and consolidate it into a single report, which can be emailed to a distribution list.

3. Customizing User Interfaces

Google Apps Script can be used to create custom user interfaces in Google Sheets. This can include custom menus, dialogs, and sidebars. Custom user interfaces can be used to streamline workflows and make it easier for you to interact with data.

4. Integrating with External APIs

Google Apps Script can be used to integrate with external APIs, such as the Hubspot API, Clearbit API, Similarweb API... This can be useful for analyzing data from external sources and integrating it into Google Sheets.

5. Sending Emails

Google Apps Script can be used to send emails from Google Sheets. This can be useful for sending automated notifications or reports to you.

How build a custom Apps Script in Google Sheets?

Suppose you have a Google Sheet that contains a list of sales transactions. Each row in the sheet represents a single transaction, and the columns contain information about the transaction, such as the date, the salesperson, the product sold, and the sales amount.

You want to create a custom function that calculates the total sales for a particular salesperson. The function should take the name of the salesperson as an argument and return the total sales amount.

Here's how you can create the custom function using Google Apps Script:

  1. Open the Google Sheet that contains the sales transactions.
  2. Click on the Tools menu, and then select Script editor. This will open the Google Apps Script editor.
  3. In the editor, create a new script file by clicking on the New script file button.
  4. In the new script file, enter the following code:

function totalSales(salesperson) {
  var sheet = SpreadsheetApp.getActiveSheet();
  var data = sheet.getDataRange().getValues();
  var total = 0;
  
  for (var i = 0; i < data.length; i++) {
    if (data[i][1] == salesperson) {
      total += data[i][3];
    }
  }
  
  return total;
}

  1. Save the script file.
  2. Go back to the Google Sheet and enter the following formula into a cell:

=totalSales("John Smith")

  1. Replace "John Smith" with the name of the salesperson you want to calculate the total sales for.
  2. Press Enter to execute the formula. The total sales amount for the specified salesperson will be calculated and displayed in the cell.

That's it! You've just created a custom function using Google Apps Script that can be used to calculate the total sales for a particular salesperson in your Google Sheet.

You can also discover our library of Google Apps Scripts here : Discover the library

build a custom apps script in google sheets
To create your first Apps Script in Google Sheets, go to -> Script Editor in "tools" menu.

Creating a Custom Menu with Google Apps Script

Google Apps Script can be used to create custom menus in Google Sheets. Custom menus can be used to streamline workflows and make it easier for you to interact with data.

To create a custom menu, you need to write a script that creates the menu and adds the desired menu items. The script can then be attached to the spreadsheet, and the menu will be available to all you who have access to the spreadsheet.

Using External APIs with Google Apps Script

Google Apps Script can be used to interact with external APIs, such as the Google Maps API or the Twitter API. This can be useful for analyzing data from external sources and integrating it into Google Sheets.

To use an external API, you need to write a script that sends requests to the API and receives responses. The responses can then be parsed and stored in Google Sheets.

Debugging Google Apps Script

Debugging Google Apps Script can be a challenging task. To help with debugging, Google Apps Script provides a built-in debugger that can be used to step through code and identify errors.

You can also use the Logger service to log messages and track the flow of their code. The log messages can be viewed in the Log window in the Script editor.

Best Practices for Using Google Apps Script in Google Sheets

Here are some best practices for using Google Apps Script in Google Sheets:

  • Use version control to track changes to your scripts
  • Use comments to document your code
  • Keep your code organized and easy to read
  • Use efficient code to minimize the execution time of your scripts
  • Test your scripts thoroughly before deploying them to production

Conclusion

Google Apps Script is a powerful tool that allows you to extend the functionality of Google Sheets. It can be used to automate tasks, create custom functions, and interact with external APIs. By using Google Apps Script, users can increase productivity and streamline workflows.