[英]Ingest data from Google sheets to make a gantt chart on Google script and serve the HTML as web app
I'm attempting to create a Google Gantt Chart on Google Script, pulling data from a Google Sheets spreadsheet to serve the HTML as web app.我正在尝试在 Google Script 上创建 Google 甘特图,从 Google Sheets 电子表格中提取数据以将 HTML 作为网络应用程序提供。 I managed to do the trick with other types of charts, but I think there's some problem with the date format, as I keep getting erros like "Invalid data table format: column #3 must be of type 'date'"
我设法用其他类型的图表来解决这个问题,但我认为日期格式存在一些问题,因为我不断收到错误信息,例如“无效的数据表格式:第 3 列必须是‘日期’类型”
I'm not familiar with Javascript, so I'm really lost here.我不熟悉 Javascript,所以我真的迷路了。 Here is the code on my Code.gs.
这是我的 Code.gs 上的代码。 The file has to be private, it's on my work Gsuite, but is a simple test with data formatted as in the Google Gantt Charts documentation .
该文件必须是私有的,它在我的工作 Gsuite 上,但它是一个简单的测试,数据的格式与Google Gantt Charts 文档中的格式相同。
function doGet() {
return HtmlService
.createTemplateFromFile("Index")
.evaluate()
.setTitle("MKT Simple Cycle")
.setSandboxMode(HtmlService.SandboxMode.IFRAME);
}
function getCycle() {
var ssID = "1p4jEZtSk4GoVWvR9OrZj2ou1dpUbl6GHYUlJ1bSVY-s",
sheet = SpreadsheetApp.openById(ssID).getSheets()[0],
data = sheet.getDataRange().getValues();
return data
}
And here is the code on my Javascript.html:这是我的 Javascript.html 上的代码:
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
google.charts.load('current', {'packages':['gantt']});
google.charts.setOnLoadCallback(getCycle);
function getCycle() {
google.script.run.withSuccessHandler(drawChart).getCycle();
}
function drawChart(rows) {
var data = google.visualization.arrayToDataTable(rows, false);
var options = {
height: 400,
gantt: {
trackHeight: 30
}
};
var chart = new google.visualization.Gantt(document.getElementById('Cycle'));
chart.draw(data, options);
}
The problem here is that sheet.getDataRange().getValues();
这里的问题是
sheet.getDataRange().getValues();
returns strings, and the Gantt chart needs Start Date and End Date to be javascript Date objects.返回字符串,甘特图需要开始日期和结束日期作为 javascript 日期对象。
Cycle through the array you have in data
, converting the strings in cells 3 and 4 (if you have a Resource ID column in your sheet, or cells 2 and 3 if you don't) with Date objects.循环遍历
data
的数组,使用 Date 对象转换单元格 3 和 4 中的字符串(如果您的工作表中有资源 ID列,或者单元格 2 和 3,如果没有)。
row[2] = new Date(row[2]);
row[3] = new Date(row[3]);
That should fix the problem.那应该可以解决问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.