Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Learning Google Apps Script
Learning Google Apps Script

Learning Google Apps Script: Customize and automate Google Applications using Apps Script

eBook
₱1256.99 ₱1796.99
Paperback
₱2245.99
Subscription
Free Trial

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
Table of content icon View table of contents Preview book icon Preview Book

Learning Google Apps Script

Chapter 2. Creating Basic Elements

In the previous chapter, you learned about Google Apps Script (GAS) and how to create a script project. In this chapter, you will learn how to create a clickable button, a custom menu, a message box, a sidebar and dialogs, as well as how to debug your script. We will use Sheets for the first two tasks and Docs for all the other tasks.

Creating a clickable button

In the previous chapter, you learned how to open the script editor in Google Sheets. For this task, open the script editor in a newly created or any existing Google Sheet and follow these steps:

  1. Select cell B3 or any other cell. Click on Insert and select Drawing..., as shown in the following screenshot:
    Creating a clickable button
  2. A drawing editor window will open. Click on the Text box icon and then click anywhere within the canvas area. Type Click Me. Resize the object to enclose the text only, as shown in the following screenshot:
    Creating a clickable button
  3. Click on Save & Close to exit the drawing editor. Now, the Click Me image will be inserted into the active cell (B3), as shown in the following screenshot:
    Creating a clickable button

    You can drag this image anywhere around the spreadsheet except in the menu bar.

    Tip

    In Google Sheets, images are not anchored to a particular cell, and they can be dragged or moved around.

    If you click on the image, a drop-down arrow on the top-right corner will be visible:

    Creating a clickable button
  4. Click on the Assign script…...

Showing toast when a button is clicked

Toast appears as a popup window in the lower-right corner of the active spreadsheet with a title and message. To create a toast dialog, edit or replace the greeting function as follows:

function greeting() {
  SpreadsheetApp.getActiveSpreadsheet()
   .toast("Hello World!", "Greeting");
}

Now if you click the button, then a toast dialog will appear as shown in the following screenshot, and it disappears within 5 seconds (the default):

Showing toast when a button is clicked

You can include a third argument, that is, timeout seconds, in the toast method. This means how long the toast will be visible for. Put a negative number if you want it to show up forever. For example, toast("Hello World!", "Greeting", -1).

Tip

Toast only works in Sheets.

Creating a custom menu

You might be wondering whether you can execute the greeting function without the help of the button. The answer is yes. In the script editor, there is a Run menu. If you click on Run | greeting, then the greeting function will be executed and the message box will open.

Creating a button for every function may not be feasible. Although you cannot alter or add items to the application's standard menu (except the Add-ons menu) such as File, Edit, View, and so on, you can add custom menus and menu items.

For this task, create a new Google Docs document or open an existing document. Open the script editor and type these two functions:

function createMenu() {
  DocumentApp.getUi()
   .createMenu("PACKT")
   .addItem("Greeting","greeting")
   .addToUi();
}

function greeting() {
  var ui = DocumentApp.getUi();
  ui.alert("Greeting", "Hello World!", ui.ButtonSet.OK);
}

In the first function, you are using the DocumentApp...

Creating a sidebar

A sidebar is a static dialog box and is included on the right-hand side of the document editor window. To create a sidebar, type the following code in the editor:

function onOpen() {
  var htmlOutput = HtmlService
  .createHtmlOutput('<button onclick="alert(\'Hello World!\');">Click Me</button>')
  .setTitle('My Sidebar');

  DocumentApp.getUi().showSidebar(htmlOutput);
}

In the preceding code, you are using HtmlService and invoking its method createHtmlOutput then consecutively invoking the setTitle method. To test this code, run the onOpen function or reload the document. The sidebar will open in the right-hand side of the document window as shown in the following screenshot. The sidebar layout size is a fixed one, which means you cannot change, alter, or resize it.

Creating a sidebar

The button in the sidebar is an HTML element, not a GAS element, and if clicked, it opens the browser interface's alert box.

Creating an Add-ons menu

In the previous task, you included the HTML code inline as a string argument to the createHtmlOutput method. Alternatively, you can put this HTML snippet in a separate HTML file.

To create a new HTML file, in the script editor, go to File | New | Html file, as shown in the following screenshot:

Creating an Add-ons menu

Then in the Create File box, enter your preferred name for the new HTML file. For this task, enter Index and click on the OK button. The .html extension will be added automatically.

Creating an Add-ons menu

A new Index.html file will be created with a few lines of default HTML code, as shown in the following screenshot:

Creating an Add-ons menu

Insert your button tags between the body tags as shown here:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <button onclick="alert('Hello World!');">Click Me</button>
  </body>
</html>

Insert the code shown here in the Code.gs file:

function onOpen(){
  DocumentApp...

Creating a clickable button


In the previous chapter, you learned how to open the script editor in Google Sheets. For this task, open the script editor in a newly created or any existing Google Sheet and follow these steps:

  1. Select cell B3 or any other cell. Click on Insert and select Drawing..., as shown in the following screenshot:

  2. A drawing editor window will open. Click on the Text box icon and then click anywhere within the canvas area. Type Click Me. Resize the object to enclose the text only, as shown in the following screenshot:

  3. Click on Save & Close to exit the drawing editor. Now, the Click Me image will be inserted into the active cell (B3), as shown in the following screenshot:

    You can drag this image anywhere around the spreadsheet except in the menu bar.

    Tip

    In Google Sheets, images are not anchored to a particular cell, and they can be dragged or moved around.

    If you click on the image, a drop-down arrow on the top-right corner will be visible:

  4. Click on the Assign script… menu item...

Showing toast when a button is clicked


Toast appears as a popup window in the lower-right corner of the active spreadsheet with a title and message. To create a toast dialog, edit or replace the greeting function as follows:

function greeting() {
  SpreadsheetApp.getActiveSpreadsheet()
   .toast("Hello World!", "Greeting");
}

Now if you click the button, then a toast dialog will appear as shown in the following screenshot, and it disappears within 5 seconds (the default):

You can include a third argument, that is, timeout seconds, in the toast method. This means how long the toast will be visible for. Put a negative number if you want it to show up forever. For example, toast("Hello World!", "Greeting", -1).

Tip

Toast only works in Sheets.

Creating a custom menu


You might be wondering whether you can execute the greeting function without the help of the button. The answer is yes. In the script editor, there is a Run menu. If you click on Run | greeting, then the greeting function will be executed and the message box will open.

Creating a button for every function may not be feasible. Although you cannot alter or add items to the application's standard menu (except the Add-ons menu) such as File, Edit, View, and so on, you can add custom menus and menu items.

For this task, create a new Google Docs document or open an existing document. Open the script editor and type these two functions:

function createMenu() {
  DocumentApp.getUi()
   .createMenu("PACKT")
   .addItem("Greeting","greeting")
   .addToUi();
}

function greeting() {
  var ui = DocumentApp.getUi();
  ui.alert("Greeting", "Hello World!", ui.ButtonSet.OK);
}

In the first function, you are using the DocumentApp class, invoking the getUi method, and consecutively invoking...

Creating a sidebar


A sidebar is a static dialog box and is included on the right-hand side of the document editor window. To create a sidebar, type the following code in the editor:

function onOpen() {
  var htmlOutput = HtmlService
  .createHtmlOutput('<button onclick="alert(\'Hello World!\');">Click Me</button>')
  .setTitle('My Sidebar');

  DocumentApp.getUi().showSidebar(htmlOutput);
}

In the preceding code, you are using HtmlService and invoking its method createHtmlOutput then consecutively invoking the setTitle method. To test this code, run the onOpen function or reload the document. The sidebar will open in the right-hand side of the document window as shown in the following screenshot. The sidebar layout size is a fixed one, which means you cannot change, alter, or resize it.

The button in the sidebar is an HTML element, not a GAS element, and if clicked, it opens the browser interface's alert box.

Left arrow icon Right arrow icon

Key benefits

  • Gain insight into customizing and automating Google applications with JavaScript
  • Create add-ons for Google Sheets, Docs, or Forms; automate your workflow; integrate with external APIs; and more.
  • A step-by-step guide to building real-world solutions

Description

Google Apps Script is a cloud-based scripting language based on JavaScript to customize and automate Google applications. Apps Script makes it easy to create and publish add-ons in an online store for Google Sheets, Docs, and Forms. It serves as one single platform to build, code, and ultimately share your App on the Web store. This book begins by covering the basics of the Google application platform and goes on to empower you to automate most of the Google applications. You will learn the concepts of creating a menu, sending mails, building interactive web pages, and implementing all these techniques to develop an interactive Web page as a form to submit sheets You will be guided through all these tasks with plenty of screenshots and code snippets that will ensure your success in customizing and automating various Google applications This guide is an invaluable tutorial for beginners who intend to develop the skills to automate and customize Google applications

Who is this book for?

Newbies to google apps script but having practical experience in Javascript.

What you will learn

  • Learn about the Google Apps script platform and work with scripts to develop Google apps
  • Create custom menus and dialogs
  • Parse and send emails
  • Generate Google calendar events
  • Build Translator and RSS reader applications
  • Develop interactive web pages
  • Design interactive web-forms
  • Form a workflow application

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Mar 18, 2016
Length: 232 pages
Edition : 1st
Language : English
ISBN-13 : 9781785882517

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : Mar 18, 2016
Length: 232 pages
Edition : 1st
Language : English
ISBN-13 : 9781785882517

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just ₱260 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just ₱260 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 6,430.97
Google Apps Script for Beginners
₱1683.99
Mastering Google App Engine
₱2500.99
Learning Google Apps Script
₱2245.99
Total 6,430.97 Stars icon

Table of Contents

10 Chapters
1. Introducing Google Apps Scripts Chevron down icon Chevron up icon
2. Creating Basic Elements Chevron down icon Chevron up icon
3. Parsing and Sending E-mails Chevron down icon Chevron up icon
4. Creating Interactive Forms Chevron down icon Chevron up icon
5. Creating Google Calendar and Drive Applications Chevron down icon Chevron up icon
6. Creating Feed Reader and Translator Applications Chevron down icon Chevron up icon
7. Creating Interactive Webpages Chevron down icon Chevron up icon
8. Building a Workflow Application Chevron down icon Chevron up icon
9. More Tips and Tricks and Creating an Add-on Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Empty star icon Empty star icon 3
(6 Ratings)
5 star 33.3%
4 star 16.7%
3 star 0%
2 star 16.7%
1 star 33.3%
Filter icon Filter
Top Reviews

Filter reviews by




markr Oct 20, 2016
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I noticed a review that claimed that the Kindle version of this book was "unreadable" not only is that claim incorrect but the book is actually excellent. For someone to be allowed to give a 1 star rating because of a problem with their reader is disgraceful. I hope someone removes it.
Amazon Verified review Amazon
Daniel Dec 05, 2016
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I am an "enthusiast" programmer so I kind of drift from project to project and language to language but I always come back to this book to get started up in Google App Script.I find it very useful. It is a great primer and its examples are short and to the point. A similar book I had tried to use was doing a working example throughout and it gets hard when there is a problem because you reach a dead end. That was not the case in the book. I can look at an example of a couple of pages and get going. Between that and looking at the online documentation from Google, you are good to go. Even his Utilities.sleep(1000) was very good practice as I had to insert over 200 calendar events without getting over the limit.It is even better to use on Kindle because you can bookmark and jump from place to place getting to the "good" stuff when you want to make something.It was a very good reference as well as introduction for me. I very highly recommend it for that.
Amazon Verified review Amazon
Amazon Customer Mar 22, 2017
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
Once fleshed out more, this is a very useful book. It has helped me with several problems already, but GAS is so extensive and the book so short, that many things still are not covered (OK -- this will always be the case, but more is better).
Amazon Verified review Amazon
Praveen Jain Jul 27, 2023
Full star icon Full star icon Empty star icon Empty star icon Empty star icon 2
Overpriced, no explanation in the book, only code
Amazon Verified review Amazon
Cliente de Amazon Jun 01, 2016
Full star icon Empty star icon Empty star icon Empty star icon Empty star icon 1
The free kindle sample is pretty much worthless .... Only the table of Contents, the Disclaimer and the credits can be "read".This does not five any real insight about the usefulness or not of this book. And I am not willing to pay 31.99 for something am am not sure will be of use. Sad.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.