Reputation: 3
I use this sheet and I want to get the values where the date is > to '2020-07-21',
the data source URL I used with html output is: https://docs.google.com/spreadsheets/d/1u78Qx5YIB2mektPyErz6xYtTXMLLtCapXlEpp63NTYI/gviz/tq?tqx=out:html&tq=select B where date '2020-07-21' > A &gid=0
the problem that when I se the data source URL in chrome browser, I could see the response data but when running that with the function UrlFetchApp.fetch(url) I receice an error. the code in script editor:
function myFunction() {
var url ="https://docs.google.com/spreadsheets/d/1u78Qx5YIB2mektPyErz6xYtTXMLLtCapXlEpp63NTYI/gviz/tq?tqx=out:csv&tq=select B where date '2020-07-21' > A &gid=0"
var response = UrlFetchApp.fetch(url).getContentText();
Logger.log(response);
}
Upvotes: 0
Views: 363
Reputation: 201553
In your case, there are the following modification points.
select B where date '2020-07-21' > A
.https://www.googleapis.com/auth/drive.readonly
can be used.When these points are reflected to your script, it becomes as follows.
function myFunction() {
var query = "select B where date '2020-07-21' > A";
var url ="https://docs.google.com/spreadsheets/d/1u78Qx5YIB2mektPyErz6xYtTXMLLtCapXlEpp63NTYI/gviz/tq?tqx=out:html&tq=" + encodeURIComponent(query) + "&gid=0";
var params = {headers: {authorization: "Bearer " + ScriptApp.getOAuthToken()}};
var response = UrlFetchApp.fetch(url, params).getContentText();
Logger.log(response);
// DriveApp.getFiles() // This line is used for automatically detecting the scope of "https://www.googleapis.com/auth/drive.readonly". So please don't remove this line.
}
params
is not required. At that time, you can remove // DriveApp.getFiles()
.https://www.googleapis.com/auth/drive
instead of https://www.googleapis.com/auth/drive.readonly
, please use // DriveApp.createFile()
instead of // DriveApp.getFiles()
Upvotes: 2