简体   繁体   中英

timeline charts not working in Google Apps Script

I'm trying to draw some charts with Google Visualization API in Apps Script. All charts work fine (PieChart, ComboChart...) but timeline.

If I copy/paste my code in a HTML file works fine however in an Apps Script project throw this error when a call the builder (new google.visualization.Timeline(container): "undefined is not a function".

Here my code:

<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>

Doesn't Timeline package work in Apps Scripts??

The Timeline package is not yet available in Apps Script, though that shouldn't be an issue since you are not using Apps Script to drive the visualizations. This seems likely to be caused by trying to draw the chart before the API finishes loading. Try assigning your button click event in a callback from the google loader:

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});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM