首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将@ curl文件名转换为google应用程序脚本

将@ curl文件名转换为google应用程序脚本
EN

Stack Overflow用户
提问于 2021-06-12 05:56:14
回答 1查看 77关注 0票数 1

文件中的Curl请求:

代码语言:javascript
复制
  curl -X POST -d "xls_url=https://kc.kobotoolbox.org/ukanga/forms/tutorial/form.xls" https://kc.kobotoolbox.org/api/v1/forms

我的应用程序脚本

代码语言:javascript
复制
function patchForm() {
     //Other details saved in scriptProperties
     var payload = "https://docs.google.com/spreadsheets/d/e/.../pub? 
                   output=xlsx";
     //Published spreadsheet, I think this is essentially an xlsx file stored in a url?
     var scriptProperties = PropertiesService.getScriptProperties();
     var url = "https://kc.kobotoolbox.org/api/v1/forms/...";
     var params = {
       "method" : scriptProperties.getProperties()["method"],
       "headers": {"Authorization" : scriptProperties.getProperties()["Authorization"]},
       "xls_url": payload
        };

     Logger.log(params)
     UrlFetchApp.fetch(url, headers = params)
     return(UrlFetchApp.fetch(url, headers = params));

}

如何转换:

代码语言:javascript
复制
     "xls_url=https://kc.kobotoolbox.org/ukanga/forms/tutorial/form.xls"

在url抓取章中的api文档中。

谢谢你,不是真正的程序员。

如果我不能让google分支机构工作,有没有办法通过google脚本发送卷发请求?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-12 06:27:46

我相信你的目标如下。

修改要点:

  • xls_url的属性在params of rlFetchApp.fetch(url, params)中不存在。
  • 当我看到示例curl命令时,xls_url=https://kc.kobotoolbox.org/ukanga/forms/tutorial/form.xls的值作为表单数据发送。
  • 在这种情况下,方法是POST方法。
  • 当我看到您的脚本和示例curl命令时,URL和值是不同的。

当上述要点反映到Google脚本中时,如下所示。

修改脚本:

代码语言:javascript
复制
function patchForm() {
  var url = "https://kc.kobotoolbox.org/api/v1/forms";
  var params = {
    method: "POST", // In this case, even when this line is removed, the POST method is used.
    payload: {xls_url: "https://kc.kobotoolbox.org/ukanga/forms/tutorial/form.xls"}
  };
  var res = UrlFetchApp.fetch(url, params);
  Logger.log(res.getContentText())
}

注意:

  • 当您想使用https://docs.google.com/spreadsheets/d/e/.../pub?output=xlsx而不是https://kc.kobotoolbox.org/ukanga/forms/tutorial/form.xls时,请替换它。
  • 在curl命令中,不使用请求头。但是在您的脚本中,使用了"Authorization" : scriptProperties.getProperties()["Authorization"]。如果需要使用,请按以下方式将其包含到上面的脚本中。在这种情况下,它假设您的scriptProperties.getProperties()["Authorization"]值是有效值。请小心这个。 var scriptProperties = PropertiesService.getScriptProperties();var params ={ method:" POST ",//在本例中,即使删除了该行,也使用POST方法。有效载荷:{xls_url:"https://kc.kobotoolbox.org/ukanga/forms/tutorial/form.xls"},headers:{“授权”:https://kc.kobotoolbox.org/ukanga/forms/tutorial/form.xls"} };

参考资料:

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67946057

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档