简体   繁体   English

从 Google 表格中提取数据以在 Google 脚本上制作甘特图并将 HTML 作为网络应用程序提供

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

相关问题 拉取 Google Sheets 作为 Google Gantt Chart 的数据源 - Pulling Google Sheets as data source for Google Gantt Chart 从网站 HTML 表中获取数据并使用 App-Script 传输到 Google 表格 - Grab data from website HTML table and transfer to Google Sheets using App-Script Google表格应用脚本HTML的onload无法正常工作 - Google Sheets App Script Html onload not working 谷歌甘特图 - Google Gantt Chart 使用 Google 应用脚本从多个工作表中获取数据 - Fetch data from multiple sheets using Google app script 如何使我的 Google Apps 脚本在电子表格多色甘特图上运行得更快? - How to make my Google Apps script run faster for spreadsheet multi-color gantt-chart? 我可以在Google表格中创建一个函数并从网络应用程序Google Apps脚本中使用它吗? - Can I create a function in Google Sheets and use it from a web app Google Apps Script? 尝试使用Google脚本从Google表格中获取数据并在具有水平标签的网页上显示 - Trying to fetch data from Google Sheets using Google script and displaying on web page having horizontal tab 当我尝试从 Google 表格中获取数据时,如果此数据包含日期或时间,则不会将其提取到 Google 应用脚本中 - When I try to fetch data from Google Sheets, if this data contains a date or time, it is not fetched into the Google app script 谷歌甘特图不更新高度 - Google gantt chart not updating height
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM