我在电子表格中尝试了查询,并且能够得到预期的结果。
当涉及到您的代码时,有两件事需要更改。
第一个是您在将 nowone 变量设置为某个值之前调用它。为此,您可以在 set 查询之前添加该语句。
我发现的第二件事是我应该在查询中给出日期时间而不是日期。
请参考以下代码:
function drawDashboard() {
var query = new google.visualization.Query(
'https://docs.google.com/spreadsheets/d/key/edit#gid=0');
var nowone = getNowDate();
//alert(nowone);
//query.setQuery("select A, B Where A >= toDate( now() )");
query.setQuery("select A,B where A >= datetime '"+nowone+"'");
query.send(handleQueryResponse);
}
为了使用可视化类设置查询,您必须
1.在apps脚本控制台的html文件中添加这段代码。
2. 从文件创建 HTML 输出。
3. 然后将其部署为 webapp。
尝试使用下面的代码从查询中获取数据:
index.html:
<html>
<head>
<!--Load the AJAX API-->
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
var data ;
// Load the Visualization API and the controls package.
google.load('visualization', '1.0', {'packages':['controls']});
// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(drawDashboard);
// Callback that creates and populates a data table,
// passes in the data and draws it.
function drawDashboard() {
var query = new google.visualization.Query(
'https://docs.google.com/spreadsheets/d/key/edit#gid=0');
var nowone = getNowDate();
//alert(nowone);
//query.setQuery("select A, B Where A >= toDate( now() )");
query.setQuery("select A,B where A >= datetime '"+nowone+"'");
query.send(handleQueryResponse);
}
function getNowDate(){
var date = new Date();
var year = date.getFullYear();
var month = date.getMonth() + 1;
var date = date.getDate();
if (month < 10) {
month = "0" + month;
}
if (date < 10) {
date = "0" + date;
}
var strDate = String(year + "-" + month + "-" + date + " 00:00:00");
return strDate;
}
function handleQueryResponse(response) {
if (response.isError()) {
alert('Error in query: ' + response.getMessage() + ' ' + response.getDetailedMessage());
return;
}
data = response.getDataTable();
// Create a dashboard.
alert(data);
}
</script>
</head>
code.gs:
function doGet() {
var html = HtmlService.createHtmlOutputFromFile('index');
html.setSandboxMode(HtmlService.SandboxMode.IFRAME);
return html;
}
当我访问 webapp 时,我能够看到响应正在返回一些对象。在 handleQueryResponse(response) 函数中,您可以添加更多代码来为返回的数据创建图表或表格。
您可以参考此documentation 为数据值创建表。希望对您有所帮助!