如何将2D数组传递到google图表html

如何将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

我有一个由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.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
    一起使用
对于这个,这个修改怎么样?在此修改中,使用了scriptlet。请修改HTML和Javascript的
ActivityChart
,如下所示。在本例中,我没有修改GoogleApps脚本端

发件人: 致:

让ar=JSON.parse();
ar.unshift([“header1”、“header2]”);
var data=google.visualization.arrayToDataTable(ar);
注:
  • 根据您的问题,我认为二维数组可能没有标题行。因此,我使用
    ar.unshift([“header1”、“header2”])
    添加了标题行。如果您的数据包含标题行,请将其删除
参考:

@Vladimir Melnikov感谢您的回复。我很高兴你的问题解决了。
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);