Reputation: 41
I'm attempting to pull data from Google Sheets and use it as a source for my Gantt chart. I've followed the example in the Google Charts documentation for pulling Sheets data for a columnchart, but not sure if more customization is necessary.
I'm not as familiar with Javascript, so not sure what is triggering the error. Here is the code in JSFiddle.
google.charts.load('current', {'packages':['gantt']});
google.charts.setOnLoadCallback(drawChart);
function daystoMilliseconds(days) {
return days * 24 * 60 * 60 * 1000;
}
function drawGID() {
var queryString = encodeURIComponent('SELECT A, B, C, D, E, F, G, H');
var query = new google.visualization.Query(
'https://docs.google.com/spreadsheets/d/1f0wxDrEfptRKCRY5pQPu6Dc_ue_tIX_ja5pQO3vXjOY/edit#gid=0&headers=1&tq=' + queryString);
query.send(handleSampleDataQueryResponse);
}
function handleSampleDataQueryResponse(response) {
if (response.isError()) {
alert('Error in query: ' + response.getMessage() + ' ' + response.getDetailedMessage());
return;
}}
function drawChart() {
var otherData = response.getDataTable();
var options = {
height: 275,
gantt: {
defaultStartDateMillis: new Date(2015, 3, 28)
}
};
var chart = new google.visualization.Gantt(document.getElementById('chart_div'));
chart.draw(otherData, options);
}
Upvotes: 0
Views: 1398
Reputation: 14480
The following Google project allows you to view a Google Spreadsheet as a Gantt Chart: https://github.com/google/ezgantt
Upvotes: 1
Reputation: 61285
first, the callback
should be --> drawGID
instead of --> drawChart
next, notice the data format for a Gantt chart
both the 'Start'
and 'End'
dates are required and cannot be blank,
as in the spreadsheet
see following working snippet...
a new data table is built, using the data from the spreadsheet (otherData
),
using the 'Duration'
column to fill in the the dates
google.charts.load('current', {
callback: drawGID,
packages: ['gantt']
});
function drawGID() {
var queryString = encodeURIComponent('SELECT A, B, C, D, E, F, G, H');
var query = new google.visualization.Query(
'https://docs.google.com/spreadsheets/d/1f0wxDrEfptRKCRY5pQPu6Dc_ue_tIX_ja5pQO3vXjOY/edit#gid=0&headers=1&tq=' + queryString);
query.send(handleSampleDataQueryResponse);
}
function handleSampleDataQueryResponse(response) {
if (response.isError()) {
alert('Error in query: ' + response.getMessage() + ' ' + response.getDetailedMessage());
return;
}
var otherData = response.getDataTable();
var ganttData = new google.visualization.DataTable({cols: [
{type: 'string', label: 'Task Id'},
{type: 'string', label: 'Task Name'},
{type: 'string', label: 'Resource'},
{type: 'date', label: 'Start'},
{type: 'date', label: 'End'},
{type: 'number', label: 'Duration'},
{type: 'number', label: '% Complete'},
{type: 'string', label: 'Dependencies'}
]});
var duration = 0;
var startDate = new Date(2016, 0, 1);
var endDate;
for (var i = 0; i < otherData.getNumberOfRows(); i++) {
startDate = new Date(startDate.getTime() + duration);
duration += otherData.getValue(i, 5);
endDate = new Date(startDate.getTime() + duration);
ganttData.addRow([
otherData.getValue(i, 0),
otherData.getValue(i, 1),
otherData.getValue(i, 2),
startDate,
endDate,
otherData.getValue(i, 5),
parseFloat(otherData.getValue(i, 6)),
otherData.getValue(i, 7)
]);
}
var options = {
height: 275,
gantt: {
defaultStartDateMillis: new Date(2015, 3, 28)
}
};
var chart = new google.visualization.Gantt(document.getElementById('chart_div'));
chart.draw(ganttData, options);
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>
Upvotes: 1