Introduction to Spreadsheet Apps Script

Image by pixabay.com

Introduction

Hi everyone! It’s been quite a while since I wrote the last article in Jan 21. Finally I’m back! This time I will share a topic, still related with technology which is about Google Spreadsheet! Maybe we usually use Spreadsheet to make a report, calculate data with formula, visualize the data through chart, and etc. However, with Apps Script tools we can improve the way we manage the data better!

Apps Script

With Apps Script tool from Spreadsheet, we can do some operation regarding how we manage the data inside the Sheet. Like writing some row and column. Basically we will write using JavaScript programming language.

In this article, we will try to create new menu which has simple application behind it to pull data from some web services and show it on Spreadsheet. We can find the menu from Extensions and choose App Script like the picture below.

We will redirected to Apps Script project. In this page, we can write some Javascript to write any data to the Sheet. The project name also can be renamed by clicking the Untitled project and just changed it.

Create Menu

To add new menu, we can write some script inside onOpen function like the picture below.

After save the project, go back to Sheet and refresh the page. We will find new menu that we defined on the Apps Script beside Help menu. In this example, we named it Example Menu with Run Function as submenu.

Read - Write Data

Let’s try to read and write a simple data into first row on A and B column. The data will be executed after we click the Run Function submenu. Then, we need to modify the myFunction like this:

After the script saved, go back to Sheet. Run the menu and see the first row on A and B column will be filled by date.

Based on the script above, we try to get the current date and write on first row A column. How about the read data? The first row B column handle it. As we can see, we will read the value of first row A column and add 1 day into it.

API Call (REST)

As mentioned on the introduction, we will pull data from web services (in this case we will use REST) and write the data into Sheet.

Because of currently (2021) we have pandemic situation, let’s try with Covid-19 API here https://covid19.mathdro.id/api and call this endpoint https://covid19.mathdro.id/api/confirmed

The return of the endpoint will be like :

Then will try to write simple table like this:

Next, let’s modify the script like this:

As we can see from the script above, we use Sheetfu library https://github.com/socialpoint-labs/sheetfu-apps-script to help manage the sheet easily. We can add some library by adding from the left menu and fill the Script ID .

After saved the script, try to re-run the Run Function. Probably we will asked for some permission, just allow it.

And after the script finished, the data from API successfully pulled into Sheet!!

Triggers

The interesting things we can have ability to automatically complete tasks with time-driven triggers. The function can run by specific time like by the minute, hour, day, week, month, and etc. I am not sure but I think the triggers also running on background! It’s mean if we close the Sheet, the trigger always running according to the schedule.

Speed

To pull and write the data into Sheet, here is two case and the result:

  • 3948 rows, 3 columns : 26 seconds => call confirmed endpoint (1.49MB-185ms)
  • 195 rows, 1 column : 3 seconds => call countries endpoint (8.75KB-39ms)

For the column, I think it’s not really matter because with same rows, how much column doesn’t really affected the time.

Conclusion

After try to build a simple application by Apps Script, some interesting point that we can conclude are:

  1. Easy to use.
  2. Support API call.
  3. Has time-driven triggers.
  4. Don’t need take care about server.
  5. Don’t need to build interface.

Maybe we are not fully yet exploring the entire feature and it could be another hidden gems for this Spreadsheet. But as usual, hopefully this article is useful for you, thank you!!