[英]Add array to a html file from javascript (Google Apps Script)
I have an html file and a javascript file (Google Apps Script), and the html file renders a chart.我有一个 html 文件和一个 javascript 文件(Google Apps 脚本),并且 html 文件呈现一个图表。
I want to add an array into that chart from the javascript file but it won't work (so trying this alternative idea).我想从 javascript 文件向该图表中添加一个数组,但它不起作用(所以尝试这个替代想法)。
Here is the html file:这是html文件:
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
google.charts.load("current", {packages:["timeline"]});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var container = document.getElementById('example3.1');
var chart = new google.visualization.Timeline(container);
var dataTable = new google.visualization.DataTable();
dataTable.addColumn({ type: 'string', id: 'Position' });
dataTable.addColumn({ type: 'string', id: 'Name' });
dataTable.addColumn({ type: 'date', id: 'Start' });
dataTable.addColumn({ type: 'date', id: 'End' });
dataTable.addRows(
//HERES WHERE I WANT TO ADD IN THE ARRAY THAT FUNCTIONS PROPERLY
);
chart.draw(dataTable);
}
</script>
<div id="example3.1" style="height: 400px;">Graph D</div>
Here is my Code.gs:这是我的 Code.gs:
function showDialog() {//This shows the modal which renders the chart
//Firstly replace the placeholder in the html with the below array somehow. Then...
var html = HtmlService.createHtmlOutputFromFile('GraphD')
.setWidth(800)
.setHeight(700);
SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
.showModalDialog(html, 'My custom dialog');
}
This is the array I want to insert into the html file before rendering:这是我想在渲染前插入到 html 文件中的数组:
var MyArray = [[ 'President', 'George Washington', new Date(1789, 3, 30), new Date(1797, 2, 4) ],
[ 'President', 'John Adams', new Date(1797, 2, 4), new Date(1801, 2, 4) ],
[ 'President', 'Thomas Jefferson', new Date(1801, 2, 4), new Date(1809, 2, 4) ],
[ 'Vice President', 'John Adams', new Date(1789, 3, 21), new Date(1797, 2, 4)],
[ 'Vice President', 'Thomas Jefferson', new Date(1797, 2, 4), new Date(1801, 2, 4)],
[ 'Vice President', 'George Clinton', new Date(1805, 2, 4), new Date(1812, 3, 20)]];
So I want the above array inserted into the commented placeholder in the HTML before then rendering Any help would be great.所以我希望在渲染之前将上述数组插入到 HTML 中的注释占位符中任何帮助都会很棒。
You are looking to pass an Array
from Google Apps Script as the argument of DataTable
method of Google Charts API on the client-side.您希望从 Google Apps Script 传递一个Array
作为客户端 Google Charts API 的DataTable
方法的参数。
One way is to use google.script.run
to exectute a server-side function to read/generate the data to be visualized on the chart and pass it to the client-side code, but this API cannot pass JavaScript Date objects, so instead of passing these type of objects we should pass some represention of them.一种方法是使用google.script.run
执行服务器端函数来读取/生成要在图表上可视化的数据并将其传递给客户端代码,但此 API 无法传递 JavaScript 日期对象,因此改为为了传递这些类型的对象,我们应该传递它们的一些表示。
What if instead of passing a JavaScript Array containing Date objects we pass a JSON?如果我们传递一个 JSON 而不是传递包含 Date 对象的 JavaScript 数组呢? The DataTable
method could supports an Array but also a JSON. DataTable
方法可以支持数组,也可以支持 JSON。
Passing an JSON, allows us to instead of passing new Date(1789,3,30)
to pass "Date(1789,3,30)"
(drop new
and enclose the remaining between quotes characters).传递一个 JSON,允许我们而不是传递new Date(1789,3,30)
来传递"Date(1789,3,30)"
(删除new
并将剩余的括在引号字符之间)。
Example:例子:
function showDialog() {
SpreadsheetApp.getUi().showModalDialog(
HtmlService.createHtmlOutputFromFile("index"),
"My dialog")
}
function doSomething(){
var data = {
cols:[
{ type: 'string', id: 'Position' },
{ type: 'string', id: 'Name' },
{ type: 'date', id: 'Start' },
{ type: 'date', id: 'End' }
],
rows:[
{c:[ {v:"President"}, {v:"George Washington"}, {v:"Date(1789, 3, 30)"}, {v:"Date(1797, 2, 4)"}]},
{c:[ {v:"President"}, {v:"John Adams"}, {v:"Date(1797, 2, 4)"}, {v:"Date(1801, 2, 4)" }]},
{c:[ {v:"President"}, {v:"Thomas Jefferson"}, {v:"Date(1801, 2, 4)"}, {v:"Date(1809, 2, 4)" }]},
{c:[ {v:"Vice President"}, {v:"John Adams"}, {v:"Date(1789, 3, 21)"}, {v:"Date(1797, 2, 4)"}]},
{c:[ {v:"Vice President"}, {v:"Thomas Jefferson"}, {v:"Date(1797, 2, 4)"}, {v:"Date(1801, 2, 4)"}]},
{c:[ {v:"Vice President"}, {v:"George Clinton"}, {v:"Date(1805, 2, 4)"}, {v:"Date(1812, 3, 20)"}]}
]
}
return data;
}
index.html索引.html
<div id="example3.1" style="height: 400px;">Graph D</div>
<script src="https://www.gstatic.com/charts/loader.js"></script>
<script>
google.charts.load("current", {packages:["timeline"]});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
google.script.run.withSuccessHandler(function(data){
var container = document.getElementById('example3.1');
var chart = new google.visualization.Timeline(container);
var dataTable = new google.visualization.DataTable(data);
chart.draw(dataTable);
}).doSomething();
}
</script>
Resources资源
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.