You have written your first Apps Script and it runs perfectly from the script editor, good job so far. But did you know you can add custom menu items to your Spreadsheet? I briefly explained this in the post "Add / Delete Calendar event with Google Apps Script" and I will explain further here how the method responsible for adding menu works with an example script.
This guide will take approximately 5 minutes to go through and try out. It is divided into 3 sections
Section 1: Method addMenu from Class Spreadsheet
For everything Spreadsheet, the spreadsheet class is responsible for almost all; from adding collaborators to setting the timezone.
addMenu is the method responsible for creating new menu in the Spreadsheet UI. addMenu takes two arguments:
Section 2: Example script
The example script is taken from the post Add / Delete Calendar event with Google Apps Script (line 67 - 71 in the example code). Lets analyze the script below which will add our menu items to a Spreadsheet UI when run.
The Array menuEntries contains the entries we want to create. The element in the Array defines the text to be displayed in the menu entry and the function to run when a user clicks on the menu entry. To add the menu, you need to specify a name for the menu and a menuEntries Array.
Summary
Now that was very simple and straight forward. You can add and make your script do more complex things such as automatically executing the script to load the menu items every time the Spreadsheet is loaded by a user without having to open the Script Editor.
This guide will take approximately 5 minutes to go through and try out. It is divided into 3 sections
- Section 1: Method addMenu from Class Spreadsheet
- Section 2: Example script
- Summary
Section 1: Method addMenu from Class Spreadsheet
For everything Spreadsheet, the spreadsheet class is responsible for almost all; from adding collaborators to setting the timezone.
addMenu is the method responsible for creating new menu in the Spreadsheet UI. addMenu takes two arguments:
- menuName: This is the name you want to give to the menu and with the data type being a string
- menuEntries: This is an array containing items in the menu. The fields "name" and "functionName" represents the menu options
The example script is taken from the post Add / Delete Calendar event with Google Apps Script (line 67 - 71 in the example code). Lets analyze the script below which will add our menu items to a Spreadsheet UI when run.
The Array menuEntries contains the entries we want to create. The element in the Array defines the text to be displayed in the menu entry and the function to run when a user clicks on the menu entry. To add the menu, you need to specify a name for the menu and a menuEntries Array.
Summary
Now that was very simple and straight forward. You can add and make your script do more complex things such as automatically executing the script to load the menu items every time the Spreadsheet is loaded by a user without having to open the Script Editor.
Nice , on it for some automation work am working on :-)
ReplyDelete