Google 可视化 Table 不会 Return 来自函数

Google Visualiation Table Won't Return From Function

我在为函数内部的 google 时间线制作数据table 时遇到了一些问题,然后 return 将数据 table 放在函数的外部功能。我的意图是最终在循环内调用此函数,以从电子表格中获取多个数据table,目前它是静态的。

为什么这里的函数 "makeTable" 不是 return 我的 table 汇编的结果?

感谢您的帮助。

<script type="text/javascript" src="https://www.google.com/jsapi?autoload={'modules':[{'name':'visualization','version':'1','packages':['timeline']}]}"></script>
<script type="text/javascript">
var container = document.getElementById('example5.4');
var chart = new google.visualization.Timeline(container); 
var firsttable = new google.visualization.DataTable();

//initiate and return table, with contents
firsttable = makeTable();

//determine height of chart based on row count in tables
var height = firsttable.getNumberOfRows() * 41 + 60;

var options = {'colors': ['#cbb69d'],'width': 1500, 'height': height,'chartArea': {'width': '100%', 'height': '50%'}};

//make chart?
google.setOnLoadCallback(chart.draw(firsttable, options));

//insert div code for new chart.
document.write("<div id='example5.4' style='width: 900px, height: 300px'></div><br>");

function makeTable() 
{

var dataTable = new google.visualization.DataTable();
dataTable.addColumn({ type: 'string', id: 'Project Name' });
dataTable.addColumn({ type: 'string', id: 'Stage' });
dataTable.addColumn({ type: 'date', id: 'Start' });
dataTable.addColumn({ type: 'date', id: 'End' });
dataTable.addRows([['Project 1', 'LNTP', new Date(2015,12, 31), new Date(2016,3, 31)],['Project 2', 'LNTP', new Date(2015,12, 31), new Date(2016,3, 31)],['Project 3', 'LNTP', new Date(2015,12, 31), new Date(2016,3, 31)],['Project 4', 'LNTP', new Date(2015,12, 31), new Date(2016,3, 31)],['Project 5','LNTP', new Date(2015,12, 31), new Date(2016,3, 31)],['Project 6', 'LNTP', new Date(2015,5, 31), new Date(2015,7, 31)],['Project 7', 'LNTP', new Date(2015,5, 31), new Date(2015,7, 31)],['Project 8', 'LNTP', new Date(2015,9, 15), new Date(2015,10, 31)],['Project 9', 'LNTP', new Date(2014,11, 26), new Date(2015,3, 12)]]);

return dataTable;


 }


</script>
<!-- <div id="example5.4" style="width: 900px; height: 150px;"></div> -->
<style type="text/css">
.tg  {border-collapse:collapse;border-spacing:0;}
.tg td{font-family:Arial, sans-serif;font-size:14px;padding:5px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;}
.tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:bold;padding:5px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;}
.tg .tg-ohfc{ background-image: url("bgnoise_lg.png");background-size: 10px 10px;background-color:#38fff8;text-align:Left}
.tg .tg-med2{background-image: url("bgnoise_lg.png");background-size: 10px 10px;background-color:#38fff8;font-weight: bold;}
.tg .tg-v88f{background-image: url("light_grey.png");background-size: 10px 10px;background-color:#f8ff00}
.tg .tg-v9m3{background-image: url("light_grey.png");background-size: 10px 10px;background-color:#f8ff00;color:#333333}
.tg .tg-031e{padding:0px 0px}


</style>
<html>
<div id="testdiv"></div>
</html>

访问时container不可用,访问元素前必须先插入节点。

<script type="text/javascript" src="https://www.google.com/jsapi?autoload={'modules':[{'name':'visualization','version':'1','packages':['timeline']}]}"></script>
<script type="text/javascript">
//first insert div code for new chart.
document.write("<div id='example5.4' style='width: 900px, height: 300px'></div><br>");
//then access the element
var container = document.getElementById('example5.4');
var chart = new google.visualization.Timeline(container); 
var firsttable = new google.visualization.DataTable();

//initiate and return table, with contents
firsttable = makeTable();

//determine height of chart based on row count in tables
var height = firsttable.getNumberOfRows() * 41 + 60;

var options = {'colors': ['#cbb69d'],'width': 1500, 'height': height,'chartArea': {'width': '100%', 'height': '50%'}};

//make chart?
google.setOnLoadCallback(chart.draw(firsttable, options));



function makeTable() 
{

var dataTable = new google.visualization.DataTable();
dataTable.addColumn({ type: 'string', id: 'Project Name' });
dataTable.addColumn({ type: 'string', id: 'Stage' });
dataTable.addColumn({ type: 'date', id: 'Start' });
dataTable.addColumn({ type: 'date', id: 'End' });
dataTable.addRows([['Project 1', 'LNTP', new Date(2015,12, 31), new Date(2016,3, 31)],['Project 2', 'LNTP', new Date(2015,12, 31), new Date(2016,3, 31)],['Project 3', 'LNTP', new Date(2015,12, 31), new Date(2016,3, 31)],['Project 4', 'LNTP', new Date(2015,12, 31), new Date(2016,3, 31)],['Project 5','LNTP', new Date(2015,12, 31), new Date(2016,3, 31)],['Project 6', 'LNTP', new Date(2015,5, 31), new Date(2015,7, 31)],['Project 7', 'LNTP', new Date(2015,5, 31), new Date(2015,7, 31)],['Project 8', 'LNTP', new Date(2015,9, 15), new Date(2015,10, 31)],['Project 9', 'LNTP', new Date(2014,11, 26), new Date(2015,3, 12)]]);

return dataTable;


 }


</script>