简体   繁体   English

将数组从 javascript(Google Apps 脚本)添加到 html 文件

[英]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.

相关问题 从 google 应用程序脚本中的 html 文件调用 javascript 函数 - Call javascript function from html file in google apps script Google Apps脚本-将来自应用脚本功能的输出返回给html文件javascript函数 - Google Apps Script - return output from apps script function back to the html file javascript function 将数组值从Google表格添加到HTML边栏(Google Apps脚本) - Add array values to HTML sidebar from Google Sheet (Google Apps Script) 如何将变量从HTML脚本文件传递到Google Apps脚本中的javascript函数? - How can I pass a variable from an HTML script file to a javascript function in google apps script? Google Apps脚本,将两个(变量)2个“变量”或“数组”从code.gs传递给index.html / Javascript - Google apps script, pass (two) 2 “variables” or “array” from code.gs to index.html / Javascript 将内部 javascript 和样式文件添加到 Google Apps 脚本 - Add internal javascript and style file to a Google Apps Script 谷歌应用程序脚本,将 object 从服务器发送到 HTML 模板和 javascript - google apps script, send object from server to HTML template and javascript 在 html 文件中显示来自应用程序脚本文件的数组 - Show array from apps script file in html file 来自 Gsheet 的 html 文件中的 Google Apps 脚本动态项目符号列表 - Google Apps Script dynamically bulleted list in an html file from Gsheet 来自 Gsheet 的 html 文件中的 Google Apps 脚本动态表格行 - Google Apps Script dynamically table row in an html file from Gsheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM