UI服务仅在2014年12月11日几天前被弃用。
以下是一步一步的说明:
将doGet()
函数中的代码替换为以下内容:
function doGet() {
return HtmlService.createHtmlOutputFromFile('index')
.setSandboxMode(HtmlService.SandboxMode.IFRAME);
}
保存它。 给项目命名。
在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>
保存所有更改。
您应该会得到类似以下内容的东西:
现在,您已经设置好了 .gs
代码和 HTML,您需要修改 HTML 文件的 SCRIPT 标签中的 JavaScript。
您需要运行一个 google.script.run
API,以触发 .gs
文件中的另一个函数,该函数将获取您的数据。
Google 文档 - Class google.script.run (客户端 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;
}