javascript - 时间线图表在 Google Apps 脚本中不起作用

标签 javascript google-apps-script google-visualization timeline

我正在尝试在 Apps 脚本中使用 Google Visualization API 绘制一些图表。 所有图表都可以正常工作(饼图、组合图...),但时间轴除外。

如果我将代码复制/粘贴到 HTML 文件中,效果很好,但是在 Apps 脚本项目中,当调用构建器时会抛出此错误 (new google.visualization.Timeline(container): "undefined is not a function"。

这是我的代码:

<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
    google.load('visualization', '1', {packages: ['corechart','timeline']});

    function pintarGraficaTimeline(){
     var chart = new google.visualization.Timeline(document.getElementById('divGrafica')); 
      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 style="font-family: Arial;border: 0 none;">
  <div id="divGraficaTimeline" style="float:right;display:block;">
   <div  style="float: right;"> <input type="button" value="Buscar" style="float:    right;margin-right: 4%;" onclick="pintarGraficaTimeline()" /></div>
</div>
<div id="divGrafica" > </div>

Timeline 包在 Apps 脚本中不起作用吗?

最佳答案

Timeline 包在 Apps 脚本中尚不可用,但这不应该成为问题,因为您没有使用 Apps 脚本来驱动可视化。这似乎可能是由于在 API 完成加载之前尝试绘制图表造成的。尝试在 Google 加载程序的回调中分配按钮点击事件:

function init () {
    var el = document.querySelector('#divGraficaTimeline input');
    if (document.addEventListener) {
        el.addEventListener('click', pintarGraficaTimeline);
    }
    else if (document.attachEvent) {
        el.attachEvent('onclick', pintarGraficaTimeline);
    }
    else {
        el.onclick = pintarGraficaTimeline;
    }
}
google.load('visualization', '1', {packages: ['corechart','timeline'], callback: init});

关于javascript - 时间线图表在 Google Apps 脚本中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23112432/

相关文章:

ios - 在iPhone和移动设备上显示图表

javascript - 从条形图谷歌图表中删除突出显示

google-apps-script - 自定义函数抛出 "You do not have the permission required to setValue"错误

javascript - 为什么 RxJS 函数 toPromise 没有取消订阅

javascript - 通过 javascript 渲染 html

Javascript 从 map 内的函数获取回调

google-apps-script - 在段落的特定点插入图像

google-apps-script - ScriptApp.getOAuthToken 未获得通过 url 获取应用程序进行驱动的正确权限?

php - 从异步 AJAX 响应加载 Google Chart

javascript - 如何将 hexlified 二进制数据转换为 unicode 字符串