- Learning Google Apps Script
- Ramalingam Ganapathy
- 455字
- 2021-07-16 12:40:04
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 the createMenu
, addItem
, and addToUi
methods by method chaining. The second function should be familiar to you, as you created it in the previous task, but this time with the DocumentApp
class and associated methods.
Tip
Do not copy-paste these functions or codes; create/edit them yourself line by line. This will help you become familiar with the script editor's code hinting and completion features.
Now run the createMenu
function and flip to the document window/tab. You will see a new menu item called PACKT added next to the Help menu. You can see the custom menu PACKT with an item Greeting as shown in the following screenshot. The item label Greeting is associated with the function greeting
.

The menu item Greeting works the same way as the button created in the previous task. The drawback with this method of inserting the custom menu is that to get the custom menu to show up, you need to run createMenu
every time within the script editor. Consider how your user would be able to use this greeting
function if they didn't know about GAS and the script editor. Think about how your user may not be a programmer like you. To enable your users to execute selected GAS functions, you should create a custom menu and make it visible as soon as the document opens. To do so, rename the createMenu
function onOpen
, and that's all.
Tip
The onOpen
function is a special function name. Whenever a user opens a document, the GAS interpreter executes this function first. Other similar function names are onEdit
, onInstall
, doGet
, and doPost
. The first two are spreadsheet event-related functions and the next two are published script service's get
and post
callback functions. You should not use these function names other than for the intended purposes.
- C++ Primer習(xí)題集(第5版)
- 大學(xué)計(jì)算機(jī)基礎(chǔ)(第三版)
- C語(yǔ)言程序設(shè)計(jì)實(shí)踐教程(第2版)
- Java EE框架整合開(kāi)發(fā)入門(mén)到實(shí)戰(zhàn):Spring+Spring MVC+MyBatis(微課版)
- 深入淺出Prometheus:原理、應(yīng)用、源碼與拓展詳解
- 程序員面試算法寶典
- JMeter 性能測(cè)試實(shí)戰(zhàn)(第2版)
- Gradle for Android
- 小學(xué)生C++創(chuàng)意編程(視頻教學(xué)版)
- Learning Vaadin 7(Second Edition)
- Tableau Desktop可視化高級(jí)應(yīng)用
- 監(jiān)控的藝術(shù):云原生時(shí)代的監(jiān)控框架
- 深入理解Java虛擬機(jī):JVM高級(jí)特性與最佳實(shí)踐
- Java程序設(shè)計(jì)入門(mén)(第2版)
- Apache Solr for Indexing Data