对Google脚本中的函数的HTTP请求 [英] HTTP Request to a function in Google Scripts

查看:34
本文介绍了对Google脚本中的函数的HTTP请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我对HTTP Request和Google Script完全不了解,因此无法解决问题.

Since I'm not experienced at all with HTTP Request and Google Scripts, I'm having trouble wraping my head around it.

因此,我的问题如下:

我目前正在尝试在lua脚本中获取信息,并将其发送到Google Spreadsheet.但是,Google电子表格应保存信息的方式取决于我正在调用和传递信息的Google脚本中的哪个功能.

I'm currently trying to get information in my lua script and send it to a google Spreadsheet. However, the way the google spreadsheet should save the info would be dependent on which function on the Google Script I'm calling and passing information.

所以,我的问题是:我的lua脚本(目前仅允许我访问HTTP请求)将如何连接到像下面这样的特定功能?

SO, my question is: How would my lua script (that only gives me access to HTTP Requests at this time) connect to a specific function like the one bellow?

function callName(name) {

  // Get the last Row and add the name provided
  var sheet = SpreadsheetApp.getActiveSheet();
  sheet.getRange(sheet.getLastRow() + 1,1).setValue([name]);
}

此外,我认为我的脚本也是错误的,但是我更担心如何真正建立连接.

Also, I think my script is wrong as well, but I'm more worried about how to actually make the connection.

推荐答案

答案:

您可以将脚本发布为Web应用程序,并使用URL参数向脚本传递所需的信息.

从有关网络应用程序的Google文档中:

From the Google documentation about web apps:

如果为脚本构建用户界面,则可以将脚本发布为Web应用程序.例如,让用户安排与支持团队成员的约会的脚本最好以Web应用程序的形式显示,以便用户可以直接从其浏览器访问它.

If you build a user interface for a script, you can publish the script as a web app. For example, a script that lets users schedule appointments with members of a support team would best be presented as a web app so that users can access it directly from their browsers.

但是,即使没有构建用户界面,您也可以使用此功能通过利用HTTP请求在工作表上运行脚本.

However, even without building a user interface, you can use this functionality to run scripts on your sheet by utilising HTTP requests.

为了允许您的脚本接受URL参数,您必须首先修改代码,以便对 HTTP GET 请求进行处理.您可以使用Apps脚本 doGet()函数和事件参数 e :

In order to allow your script to accept URL parameters, you must first modify your code so that processing is done on a HTTP GET request. You can do this with the Apps Script doGet() function and the event parameter e:

function doGet(e) {
  callName(e.parameter.name);
}

function callName(name) {
  // Get the last Row and add the name provided
  var sheet = SpreadsheetApp.getActiveSheet();
  sheet.getRange(sheet.getLastRow() + 1,1).setValue([name]);
}

设置Web应用程序:

在Apps脚本用户界面中,按照 Publish>部署为Web应用... 菜单项,然后在新打开的模式窗口中,选择以下设置:

Setting up the Web App:

From the Apps Script user interface, follow the Publish > Deploy as web app... menu item, and in the newly-opened modal window, you'll want to select the following settings:

  • 项目版本:新建
  • 以以下身份执行该应用程序:我(在您的电子邮件地址@此处)
  • 有权访问该应用的人:所有人,甚至是匿名人

然后单击 Deploy .在这里,您将获得一个新的,较小形式的URL,其格式如下:

And click Deploy. Here, you will be given a URL in a new, smaller modal in the following form:

https://script.google.com/a/your-domain.com/macros/s/some-script-id/exec

提出请求:

现在剩下的事情变得微不足道了-您可以在上一步中向脚本URL发出HTTP请求,但是提供所需的URL参数,以便为te app提供您希望设置的值的信息.

Making the request:

The rest of this is now trivial - you can make your HTTP request to the script URL in the previous step, but providing the URL parameter that you need in order to give te app the information of the value you wish to set.

例如,如果要将值设置为数字20,请这样进行get请求:

For example, if you want to set the value to the number 20, make your get request as so:

GET https://script.google.com/a/your-domain.com/macros/s/some-script-id/exec?name=20

请注意,最后的?name = 20 为Web App提供了值为 20 的参数 name . doGet(e)函数从 e.parameter.name 读取此信息,并将其发送到您的 callName(name)函数进行处理和执行.

Note the ?name=20 at the end gives the Web App the parameter name with a value of 20. The doGet(e) function reads this from e.parameter.name and sends it to your callName(name) function for processing and execution.

希望对您有帮助!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆