问题
I'm trying to create a Google Chart to Google Apps Script using a formula that already exists.
this formula works fine in the spreadsheet:
=query(A:A; "select A where A >= datetime '"&TEXT(today();"yyyy-MM-dd HH:mm:ss")&"'";-1)
this code doesn't work to Google Apps Script as intended:
query.setQuery("select A, B Where A >= toDate( now() )");
var query = new
google.visualization.Query('https://docs.google.com/spreadsheets/d/key');
query.setQuery("select A, B Where A = date '" + nowone + "'");
var nowone = getNowDate();
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;
}
I've tried many times without success to replicate the formula to Google Apps Script.
thanks in advance...
回答1:
I tried the query in the Spreadsheet and was able to get the expected results.
When coming to your code there are two things to be changed.
First one is you are calling the nowone variable before it is set to some value. For that you can just add that statement before the set query.
The second thing I found is that I instead of date in the query you should give datetime.
Please find the below code for reference:
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);
}
In order to set the query using visualization class, you have to 1. Add this code in html file in apps script console. 2. create HTML output from file. 3. Then Deploy this as a webapp.
Tried this code below to get the data from query:
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;
}
When I access the webapp, I was able to see that the response is returning some object. In the handleQueryResponse(response) function you could add some more code to create a chart or a table for the returned data.
You can refer to this documentation for creating table for the data values. Hope that helps!
来源:https://stackoverflow.com/questions/31419110/query-setquery-to-current-day-with-google-apps-script