UI 服务仅在几天前于 2014 年 12 月 11 日被弃用
Google Documentation - UI Service
以下是您需要执行的操作的分步说明:
创建附加到您网站的 Apps 脚本
- 在您的 Google 网站中 - 点击带有齿轮图标的按钮
- 选择管理网站
- 点击应用脚本选择
- 点击添加新脚本按钮
- 选择 SCRIPT 作为 WEB APP
替换doGet()函数中的代码,如下所示:
function doGet() {
return HtmlService.createHtmlOutputFromFile('index')
.setSandboxMode(HtmlService.SandboxMode.IFRAME);
}
保存。
为项目命名。
创建 index.html 文件
在 HTML 文件中输入以下内容:
<html>
<head>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["timeline"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var container = document.getElementById('timeline');
var chart = new google.visualization.Timeline(container);
var dataTable = new google.visualization.DataTable();
dataTable.addColumn({ type: 'string', id: 'President' });
dataTable.addColumn({ type: 'date', id: 'Start' });
dataTable.addColumn({ type: 'date', id: 'End' });
dataTable.addRows([
[ 'Washington', new Date(1789, 3, 29), new Date(1797, 2, 3) ],
[ 'Adams', new Date(1797, 2, 3), new Date(1801, 2, 3) ],
[ 'Jefferson', new Date(1801, 2, 3), new Date(1809, 2, 3) ]]);
chart.draw(dataTable);
}
</script>
</head>
<body>
<div id="timeline" style="width: 900px; height: 180px;"></div>
</body>
</html>
发布您的 Apps 脚本
- 点击发布
- 单击部署为 Web 应用程序
- 如果没有版本,请保存新版本
- 点击底部的更新
将 Apps 脚本小工具添加到您的站点页面
- 返回您的网站页面。
- 点击带有铅笔图标的编辑页面按钮
- 点击插入菜单
- 选择应用程序脚本
- 选择脚本
- 点击选择按钮
保存所有更改。
你应该得到如下所示的东西:
现在您已经设置了.gs 代码和 HTML,您需要修改 HTML 文件的 SCRIPT 标记中的 JavaScript。
您需要运行google.script.run API 以触发.gs 文件中的另一个函数,该函数将发出并获取您的数据。
Google Documentation - Class google.script.run (Client-side API)
查看该文档,了解它的作用。
index.html 文件中的新代码如下所示:
<script>
function onSuccess(importedData) {
dataTable.addRows([importedData]);
chart.draw(dataTable);
alert('Your data has been loaded');
}
google.script.run.withSuccessHandler(onSuccess)
.retrieveChartData();
</script>
硬编码的数据将被替换为代码。这是必须删除并更改为其他内容的硬编码数据:
dataTable.addRows([
[ 'Washington', new Date(1789, 3, 29), new Date(1797, 2, 3) ],
[ 'Adams', new Date(1797, 2, 3), new Date(1801, 2, 3) ],
[ 'Jefferson', new Date(1801, 2, 3), new Date(1809, 2, 3) ]]);
您需要在.gs 脚本文件中添加另一个函数。像这样的:
function retrieveChartData() {
Logger.log('I was called!');
//Get the data from your data source
Code here
var tableData = code here;
//return the data
return tableData;
}