在我的Google图表代码中,我使用SELECT函数查询我将使用的行和列的相关Google表格
我相信我使用的查询是SQL,所以考虑到这一点,我使用以下Query来尝试从另一个工作表中选择数据:
SELECT A, B, C, D
FROM 'Sheet 2'
但是出现错误,说语法不正确。下面是图表本身的代码,当代码中没有FROM语法时,它会完美显示:
<head>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script>
google.load("visualization", '1', {packages:['corechart', 'bar']});
google.setOnLoadCallback(drawChart);
function drawChart() {
var query = new google.visualization.Query(
'https://docs.google.com/spreadsheets/d/1oKxlfCjcvjCzGh8fpTX3ax7qWbWA2uVVQgk-jzOWX3k/edit?usp=sharing');
query.setQuery('SELECT A, B, C, D FROM "Sheet 2"');
query.send(handleQueryResponse);
}
function handleQueryResponse(response) {
if (response.isError()) {
alert('Error in query: ' + response.getMessage() + ' ' + response.getDetailedMessage());
return;
}
var data = response.getDataTable();
for (var i = 0; i < data.getNumberOfColumns(); i++) {
}
var chart = new google.visualization.ColumnChart(document.getElementById('columnchart'));
chart.draw(data);
}
</script>
</head>
<body>
<div id='columnchart'></div>
</body>
如果不是FROM语法,那么如何使用这种创建图表的方法定义多个工作表?
您应该将工作表设置为“gid = 838232320”,而不是在queryString中:
//google.load("visualization", '1', {packages:['corechart', 'bar']});
google.load('visualization', '1.0', {'packages':['corechart'], 'callback': drawChart});
google.setOnLoadCallback(drawChart);
function drawChart() {
console.log("drawChart: Init");
var queryString = encodeURIComponent('SELECT A, B, C, D');
var query = new google.visualization.Query('https://docs.google.com/spreadsheets/d/1oKxlfCjcvjCzGh8fpTX3ax7qWbWA2uVVQgk-jzOWX3k/gviz/tq?gid=838232320&headers=1&tq=' + queryString);
// Apply query language statement.
// query.setQuery('SELECT A, B, C, D FROM "Sheet 2"');
// Send the query with a callback function.
query.send(handleQueryResponse);
}
function handleQueryResponse(response) {
console.log("handleQueryResponse: ", response);
if (response.isError()) {
alert('Error in query: ' + response.getMessage() + ' ' + response.getDetailedMessage());
return;
}
var data = response.getDataTable();
for (var i = 0; i < data.getNumberOfColumns(); i++) {
}
var chart = new google.visualization.ColumnChart(document.getElementById('columnchart'));
chart.draw(data);
}
<div id='columnchart'></div>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>