【问题标题】:error "Invalid argument:" when using UrlFetchApp.fetch(url) with where date 'yyy-MM-dd'使用 UrlFetchApp.fetch(url) 与 where date 'yyyy-MM-dd' 时出现错误“无效参数:”
【发布时间】:2020-07-28 15:11:02
【问题描述】:

我使用this sheet,我想将日期为>的值设为“2020-07-21”, 我用于 html 输出的数据源 URL 是:https://docs.google.com/spreadsheets/d/1u78Qx5YIB2mektPyErz6xYtTXMLLtCapXlEpp63NTYI/gviz/tq?tqx=out:html&tq=select B where date '2020-07-21' > A &gid=0

当我在 chrome 浏览器中设置数据源 URL 时,我可以看到响应数据,但是当使用 UrlFetchApp.fetch(url) 函数运行它时,我收到一个错误。 脚本编辑器中的代码:

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);
}

【问题讨论】:

标签: google-apps-script google-sheets google-visualization urlfetch google-query-language


【解决方案1】:

你的情况有以下修改点。

修改点:

  • 请对select B where date '2020-07-21' > A 进行 URL 编码。
  • 请使用访问令牌向端点请求。
    • 在这种情况下,因为取回了数据,所以我认为可以使用https://www.googleapis.com/auth/drive.readonly的范围。

当这些点反映到你的脚本中时,它变成如下。

修改脚本:

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 不是必需的。届时,您可以删除// DriveApp.getFiles()
  • 如果你想使用https://www.googleapis.com/auth/drive的范围代替https://www.googleapis.com/auth/drive.readonly,请使用// DriveApp.createFile()代替// DriveApp.getFiles()

参考资料:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 2020-12-14
    • 2011-12-02
    • 2013-02-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多