This tutorial is about how to add a custom into your google document using the google app script.
First, you need to have one open document and then click into the menu : Tools – Script editor …
You will see a new document into your browser with a link like this: https://script.google.com …
This will allow you to create New – Project, Script file, Html file, and Google documents.
Use the Code.gs like the base of the script and add this source of code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | function onOpen() { // get to use the DocumentApp or FormApp. var ui = SpreadsheetApp.getUi(); ui.createMenu('Custom Menu') .addItem('First item', 'menuItem1') .addSeparator() .addSubMenu(ui.createMenu('Sub-menu') .addItem('Second item', 'menuItem2')) .addItem('Show message', 'showMessageBox') .addItem('Show dialog', 'showDialog') .addToUi(); } function showDialog() { var html = HtmlService.createHtmlOutputFromFile('Page') .setWidth(400) .setHeight(300); SpreadsheetApp.getUi() // Or DocumentApp or FormApp. .showModalDialog(html, 'My custom dialog'); } function menuItem1() { SpreadsheetApp.getUi() // Or DocumentApp or FormApp. .alert('You clicked the first menu item!'); } function menuItem2() { SpreadsheetApp.getUi() // Or DocumentApp or FormApp. .alert('You clicked the second menu item!'); } function showMessageBox() { Browser.msgBox('You clicked it!'); } |
Now add this new Html page named Page.html with this source code:
1 2 3 4 5 6 7 8 9 10 11 | <!DOCTYPE html> <html> <head> <base target="_top"> </head> <body> Hello, World! <hr> <input type="button" value="Close" onclick="google.script.host.close()" /> </body> </html> |
The result is this:
The base of the drawing menu is the onOpen function to create and draw the menu by getting the document and create and add menu and submenu with separators. This comes with the text of the menu and the functions for each click on the menu.
In this example, I used one HTML page file. That HTML page shows us by using the showDialog function.
The documentation of Google Apps Script tells us:
The UI service in Google Apps Script provides the ability to build a user interface for displaying or capturing information using user interface elements called widgets.
Overview
Your scripts can create a wide range of user interface elements, including, but not limited to, the following:
Push buttons
Radio buttons
Toggle buttons
Checkboxes
Text fields
Labels
Titles
List boxes
Dialog boxes
Panels of many types
And far more elements than it’s possible to list here.
You can also test one edit box example by adding this function and change the onOpen function to show it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | function readText() { var ui = SpreadsheetApp.getUi(); // Same variations. var result = ui.prompt( 'Let\'s get to know each other!', 'Please enter your name:', ui.ButtonSet.OK_CANCEL); // Process the user's response. var button = result.getSelectedButton(); var text = result.getResponseText(); if (button == ui.Button.OK) { // User clicked "OK". ui.alert('Your name is ' + text + '.'); } else if (button == ui.Button.CANCEL) { // User clicked "Cancel". ui.alert('I didn\'t get your name.'); } else if (button == ui.Button.CLOSE) { // User clicked X in the title bar. ui.alert('You closed the dialog.'); } } |