对 Google 脚本中函数的 HTTP 请求
HTTP Request to a function in Google Scripts
由于我对 HTTP 请求和 Google 脚本完全没有经验,所以我很难理解它。
所以,我的问题如下:
我目前正在尝试在我的 lua 脚本中获取信息并将其发送到 google 电子表格。但是,google 电子表格保存信息的方式取决于我正在调用和传递信息的 Google 脚本中的哪个函数。
所以,我的问题是:我的 lua 脚本(此时只允许我访问 HTTP 请求)如何连接到特定功能,例如下面的功能?
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 应用程序并使用 URL 参数向脚本传递您需要的信息。
更多信息:
来自 Google 关于网络应用程序的文档:
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 请求将此功能用于 运行 您 sheet 上的脚本。
修改脚本:
为了让您的脚本接受 URL 参数,您必须首先修改您的代码,以便在 HTTP GET
请求上完成处理。您可以使用 Apps 脚本 doGet()
函数和事件参数 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 > Deploy as web app...
菜单项,在新打开的模式 window 中,您需要 select 以下设置:
- 项目版本:
New
- 以我的身份执行应用程序:我 (your-email@address.here)
- 谁有权访问该应用程序:任何人,甚至是匿名的
然后点击Deploy
。在这里,您将在一个新的、更小的模态中得到一个 URL,形式如下:
https://script.google.com/a/your-domain.com/macros/s/some-script-id/exec
发出请求:
剩下的部分现在变得微不足道了——您可以在上一步中向脚本 URL 发出 HTTP 请求,但提供您需要的 URL 参数以便为应用程序提供您要设置的值的信息。
例如,如果你想将值设置为数字 20,那么你的 get 请求如下:
GET https://script.google.com/a/your-domain.com/macros/s/some-script-id/exec?name=20
注意末尾的 ?name=20
为 Web 应用程序提供了参数 name
,值为 20
。 doGet(e)
函数从 e.parameter.name
读取它并将其发送到您的 callName(name)
函数进行处理和执行。
参考文献:
由于我对 HTTP 请求和 Google 脚本完全没有经验,所以我很难理解它。
所以,我的问题如下:
我目前正在尝试在我的 lua 脚本中获取信息并将其发送到 google 电子表格。但是,google 电子表格保存信息的方式取决于我正在调用和传递信息的 Google 脚本中的哪个函数。
所以,我的问题是:我的 lua 脚本(此时只允许我访问 HTTP 请求)如何连接到特定功能,例如下面的功能?
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 应用程序并使用 URL 参数向脚本传递您需要的信息。
更多信息:
来自 Google 关于网络应用程序的文档:
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 请求将此功能用于 运行 您 sheet 上的脚本。
修改脚本:
为了让您的脚本接受 URL 参数,您必须首先修改您的代码,以便在 HTTP GET
请求上完成处理。您可以使用 Apps 脚本 doGet()
函数和事件参数 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 > Deploy as web app...
菜单项,在新打开的模式 window 中,您需要 select 以下设置:
- 项目版本:
New
- 以我的身份执行应用程序:我 (your-email@address.here)
- 谁有权访问该应用程序:任何人,甚至是匿名的
然后点击Deploy
。在这里,您将在一个新的、更小的模态中得到一个 URL,形式如下:
https://script.google.com/a/your-domain.com/macros/s/some-script-id/exec
发出请求:
剩下的部分现在变得微不足道了——您可以在上一步中向脚本 URL 发出 HTTP 请求,但提供您需要的 URL 参数以便为应用程序提供您要设置的值的信息。
例如,如果你想将值设置为数字 20,那么你的 get 请求如下:
GET https://script.google.com/a/your-domain.com/macros/s/some-script-id/exec?name=20
注意末尾的 ?name=20
为 Web 应用程序提供了参数 name
,值为 20
。 doGet(e)
函数从 e.parameter.name
读取它并将其发送到您的 callName(name)
函数进行处理和执行。