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.
You're reading from Learning Google Apps Script
To create a modal dialog, which prevents the user from updating anything in the spreadsheet or document, update the code in the Code.gs
file as shown here:
function onOpen(){ DocumentApp.getUi() .createAddonMenu() .addItem("Show Dialog", "showDialog") .addToUi(); } function showDialog() { var html = HtmlService .createHtmlOutputFromFile('Index'); DocumentApp.getUi() .showModalDialog(html, 'Greeting'); }
Go to Add-ons | Chapter 2 | Show Dialog and a modal dialog will pop up:
Now we will create a modeless dialog and see the difference between modal and modeless dialogs. Update the showDialog
function as shown here:
function showDialog() { var html = HtmlService.createHtmlOutputFromFile('Index'); DocumentApp.getUi() .showModelessDialog(html, 'Greeting'); }
Note that the showModalDialog
method has been changed to showModelessDialog
.
Modeless dialogs do not prevent you from doing other things, such as editing the document, and you can drag the dialog around as shown here:
Logging the values of variables at a few points is essential when testing and debugging your code. The Logger
class is a helpful tool to do this and has a few methods that are essential to debug your code.
Update the showDialog
function as shown here:
function showDialog() { var ui = DocumentApp.getUi(); var response = ui.prompt( 'Greeting', 'Will you enter your name below?', ui.ButtonSet.YES_NO ); if (response.getSelectedButton() == ui.Button.YES) { Logger.log('Your name is %s.', response.getResponseText()); } else if (response.getSelectedButton() == ui.Button.NO) { Logger.log('You clicked \'NO\' button'); } else { Logger.log('You closed the dialog.'); } }
Run the showDialog
function as usual from the Add-ons menu. Do anything, for example, enter your name and click on Yes or No or close the dialog.
Now within the script editor, press Ctrl + Enter (Windows) or Command + Enter (Mac) or from the View menu, select Logs, then you can see the...
In this chapter, you learned about many type of dialog and how to create and display them, you found out how to use the Logger
class to log values, and you also saw how to debug your script. In the next chapter, you will learn about Gmail and Contacts.
Tip
Downloading the example code
You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
You can download the code files by following these steps:
Log in or register to our website using your e-mail address and password.
Hover the mouse pointer on the SUPPORT tab at the top.
Click on Code Downloads & Errata.
Enter the name of the book in the Search box.
Select the book for which you're looking to download the code files.
Choose from the drop-down menu where you purchased this book from.
Click on Code Download.
Once the file is downloaded, please make sure...