官术网_书友最值得收藏!

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:

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.

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

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.getUi()
  .createAddonMenu()
  .addItem("Show Sidebar", "showSidebar")
  .addToUi();
}


function showSidebar() {
  DocumentApp.getUi()
  .showSidebar(
    HtmlService.createHtmlOutputFromFile('Index')
    .setTitle('Greetings')
  );
}

To test the code, run the onOpen function or reload the document. In the Add-ons menu, a new item, called Chapter 2 (the project name), will be added, as shown here:

Show Sidebar is the label for the showSidebar function; click on it to show your sidebar.

主站蜘蛛池模板: 柯坪县| 黑龙江省| 芦溪县| 尖扎县| 乌拉特后旗| 合山市| 盐边县| 鄂州市| 梧州市| 寻乌县| 黑山县| 清水河县| 社旗县| 通河县| 巴塘县| 济南市| 无锡市| 东城区| 泗洪县| 开阳县| 克东县| 台北市| 玛纳斯县| 临桂县| 镇坪县| 安平县| 沭阳县| 长丰县| 邵阳县| 北海市| 陆良县| 阿拉善右旗| 冕宁县| 通江县| 美姑县| 隆回县| 瑞安市| 宜宾县| 盈江县| 佛冈县| 翁源县|