如何将2D数组传递到google图表html
我有一个由google sheet脚本生成的2D数组: [Steve,2],[Bob,101],[Anna,78]等如何将2D数组传递到google图表html,html,google-apps-script,Html,Google Apps Script,我有一个由google sheet脚本生成的2D数组: [Steve,2],[Bob,101],[Anna,78]等 var t = HtmlService.createTemplateFromFile('ActivityChart'); // Modified t.DataForChart = (newdata_03); html = t.evaluate().setWidth(950).setHeight(600); // Added SpreadsheetApp.ge
var t = HtmlService.createTemplateFromFile('ActivityChart'); // Modified
t.DataForChart = (newdata_03);
html = t.evaluate().setWidth(950).setHeight(600); // Added
SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
.showModalDialog(html,' ');
我需要将其传递给:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
google.charts.load("current", {packages:["corechart"]});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Task', 'Hours per Day'], //need to replace it with my array
['Work', 11], //need to replace it with my array
['Eat', 2],
['Commute', 2],
['Watch TV', 2],
['Sleep', 7]
]);
var options = {
title: 'My Daily Activities',
is3D: true,
};
var chart = new google.visualization.PieChart(document.getElementById('piechart_3d'));
chart.draw(data, options);
}
</script>
</head>
<body>
<div id="piechart_3d" style="width: 900px; height: 500px;"></div>
</body>
</html>
load(“当前”{packages:[“corechart”]});
google.charts.setOnLoadCallback(drawChart);
函数绘图图(){
var data=google.visualization.arrayToDataTable([
['Task','Hours per Day'],//需要用我的数组替换它
['Work',11],//需要用我的数组替换它
[Eat',2],
[‘通勤’,2],
[“看电视”,2],
[Sleep',7]
]);
变量选项={
标题:“我的日常活动”,
is3D:是的,
};
var chart=new google.visualization.PieChart(document.getElementById('PieChart_3d');
图表绘制(数据、选项);
}
如果您能帮助我,我将不胜感激,因为我在html方面很弱。我相信您的情况和目标如下
- 问题中的HTML和Javascript是
的文件ActivityChart
- 在脚本中,
t.DataForChart=(newdata\u 03)的
代码>是newdata\u 03
[[“Steve”,2],“Bob”,101],“Anna”,78],,]
- 您希望将此二维数组发送到HTML端,并将其与
一起使用google.visualization.arrayToDataTable
ActivityChart
,如下所示。在本例中,我没有修改GoogleApps脚本端
发件人:
致:
让ar=JSON.parse();
ar.unshift([“header1”、“header2]”);
var data=google.visualization.arrayToDataTable(ar);
注:
- 根据您的问题,我认为二维数组可能没有标题行。因此,我使用
添加了标题行。如果您的数据包含标题行,请将其删除ar.unshift([“header1”、“header2”])
var data = google.visualization.arrayToDataTable([
['Task', 'Hours per Day'], //need to replace it with my array
['Work', 11], //need to replace it with my array
['Eat', 2],
['Commute', 2],
['Watch TV', 2],
['Sleep', 7]
]);
<? const str = JSON.stringify(DataForChart); ?>
let ar = JSON.parse(<?= str ?>);
ar.unshift(["header1", "header2"]);
var data = google.visualization.arrayToDataTable(ar);