如何解决GAS中的调用表功能 修改点:流量:示例脚本:注意:参考文献: 1准备脚本 2部署Web应用 3使用自定义功能测试Web应用
是否可以在google-app-script中调用工作表函数?
我想调用MATCH(),IMPORTRANGE()和INDEX()函数来计算函数定义中的返回值,如下所示。
function abc(foo,bar) {
a = MATCH(foo,IMPORTRANGE("address of sheet","A:A"),0)
b = MATCH(bar,"B:B"),0)
return INDEX(IMPORTRANGE("address of sheet","C:Z"),a,b)
}
应用@Tanaike提供的示例
function doGet(e) {
const [sheet1,sheet2,sheet3] = [e.parameter.url1,e.parameter.url2,e.parameter.url3].map(f => SpreadsheetApp.openByUrl(f).getSheets()[0]);
const a = sheet1.getRange("A:A").createTextFinder(e.parameter.foo).findNext().getRow();
const b = sheet2.getRange("B:B").createTextFinder(e.parameter.bar).findNext().getRow();
const res = sheet3.getRange("C:Z").offset(a - 1,b - 1).getValue();
return ContentService.createTextOutput(res);
}
function abc(foo,bar) {
const url1 = "https://docs.google.com/spreadsheets/d/###/edit";
const url2 = "https://docs.google.com/spreadsheets/d/###/edit";
const url3 = "https://docs.google.com/spreadsheets/d/###/edit";
const baseUrl = "https://script.google.com/a/###/macros/s/###/exec";
const res = UrlFetchApp.fetch(`${baseUrl}?url1=${encodeURIComponent(url1)}&url2=${encodeURIComponent(url2)}&url3=${encodeURIComponent(url3)}&foo=${foo}&bar=${bar}`);
console.log(res.getContentText())
return res.getContentText();
}
在上面基本上是样本的副本,abc函数返回#VALUE错误,长度为50,000个字母。
控制台日志如下所示开始。
Logging output too large. Truncating output.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta content="width=300,initial-scale=1" name="viewport">
解决方法
这个答案怎么样?
修改点:
- 不幸的是,在当前阶段,电子表格的内置功能无法与Google Apps脚本一起使用。
- 当我在伪脚本中看到您的公式时,我以为
MATCH(foo,IMPORTRANGE("address of sheet","A:A",0)
可能是MATCH(foo,"A:A"),0)
。 - 因此,为了将公式转换为Google Apps脚本,需要使用
openByUrl
来实现IMPORTRANGE("address of sheet",0)
。如果要将此转换后的脚本用作自定义函数,在这种情况下,由于许可,自定义函数不能使用openByUrl
。因此,在这种情况下,需要使用脚本编辑器,自定义菜单和电子表格中的按钮来运行脚本。这是Google方面的当前规范。请注意这一点。- 根据您的问题,我不了解您想如何运行脚本以及有关
address of sheet
的信息。因此,在这个答案中,我想建议使用脚本编辑器运行脚本。
- 根据您的问题,我不了解您想如何运行脚本以及有关
当上述问题反映到您的问题的伪脚本中时,它如下所示。该示例的流程如下。
流量:
- 检索工作表。
- 检索
a
和b
的值。- 在这种情况下,我使用了TextFinder。
- 使用
a
和b
检索结果值。
示例脚本:
请复制以下脚本并将其粘贴到电子表格的脚本编辑器中。并且,请设置变量url1
,url2
和url3
,foo
和bar
。运行脚本时,请使用脚本编辑器运行myFunction
的功能。这样,您可以在控制台上看到检索到的值,并且还将值作为示例放入活动单元格中。
function abc(foo,bar) {
const url1 = "https://docs.google.com/spreadsheets/d/###/edit"; // Please set this.
const url2 = "https://docs.google.com/spreadsheets/d/###/edit"; // Please set this.
const url3 = "https://docs.google.com/spreadsheets/d/###/edit"; // Please set this.
// 1. Retrieve sheets.
const [sheet1,sheet2,sheet3] = [url1,url2,url3].map(e => SpreadsheetApp.openByUrl(e).getSheets()[0]); // Modified
// 2. Retrieve values of `a` and `b`.
const a = sheet1.getRange("A:A").createTextFinder(foo).findNext().getRow();
const b = sheet2.getRange("B:B").createTextFinder(bar).findNext().getRow();
// 3. Retrieve the result value using `a` and `b`.
return sheet3.getRange("C:Z").offset(a - 1,b - 1).getValue();
}
// Please run this function.
function myFunction() {
const foo = "###"; // Please set this.
const bar = "###"; // Please set this.
const res = abc(foo,bar);
console.log(res)
SpreadsheetApp.getActiveRange().setValue(res); // Heare,the value is put to the active cell.
}
- 很遗憾,根据您的问题,我无法理解每个
address of sheet
是否是相同的URL。因此,在上面的示例脚本中,可以使用3个不同的URL。另外,可以使用相同的3个URL。 -
const a = sheet1.getRange("A:A").createTextFinder(foo).findNext().getRow();
和const b = sheet2.getRange("B:B").createTextFinder(bar).findNext().getRow();
分别是a = MATCH(foo,0)
和b = MATCH(bar,"B:B",0)
。-
IMPORTRANGE("address of sheet","A:A")
表示address of sheet
中第一个标签的“ A:A”。
-
-
return sheet3.getRange("C:Z").offset(a - 1,b - 1).getValue();
是return INDEX(IMPORTRANGE("address of sheet","C:Z"),a,b)
。
注意:
-
当伪脚本中
address of sheet
的所有值都是活动电子表格时,abc
可以进行如下修改。在这种情况下,您可以将此功能用作自定义功能,例如=abc(foo,bar)
。function abc(foo,bar) { const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0]; const a = sheet.getRange("A:A").createTextFinder(foo).findNext().getRow(); const b = sheet.getRange("B:B").createTextFinder(bar).findNext().getRow(); return sheet.getRange("C:Z").offset(a - 1,b - 1).getValue(); }
-
请在启用V8的情况下使用此脚本。
参考文献:
已添加:
作为另一种方法,为了在自定义函数中使用openByUrl
,我想建议使用Web Apps。
用法:
请执行以下流程。
1。准备脚本。
Web Apps的示例脚本是Google Apps脚本。请复制以下脚本(Google Apps脚本)并将其粘贴到脚本编辑器中。该脚本适用于Web Apps。并且请设置URL。关于baseUrl
,请在部署Web Apps之后进行设置。
function doGet(e) {
const [sheet1,sheet3] = [e.parameter.url1,e.parameter.url2,e.parameter.url3].map(f => SpreadsheetApp.openByUrl(f).getSheets()[0]);
const a = sheet1.getRange("A:A").createTextFinder(e.parameter.foo).findNext().getRow();
const b = sheet2.getRange("B:B").createTextFinder(e.parameter.bar).findNext().getRow();
const res = sheet3.getRange("C:Z").offset(a - 1,b - 1).getValue();
return ContentService.createTextOutput(res);
}
function abc(foo,bar) {
const url1 = "https://docs.google.com/spreadsheets/d/###/edit";
const url2 = "https://docs.google.com/spreadsheets/d/###/edit";
const url3 = "https://docs.google.com/spreadsheets/d/###/edit";
const baseUrl = "https://script.google.com/macros/s/###/exec";
const res = UrlFetchApp.fetch(`${baseUrl}?url1=${encodeURIComponent(url1)}&url2=${encodeURIComponent(url2)}&url3=${encodeURIComponent(url3)}&foo=${foo}&bar=${bar}`);
return res.getContentText();
}
2。部署Web应用。
- 在脚本编辑器上,通过“发布”->“作为Web应用程序部署”打开一个对话框。
- 为“以以下方式执行应用”:选择“我” 。
- 通过这种方式,脚本以所有者身份运行。
- 为“有权访问该应用的用户:” 选择“任何人,甚至是匿名的” 。
- 在这种情况下,不需要请求访问令牌。我认为我建议使用此设置来测试此变通办法。
- 当然,您也可以使用访问令牌。使用访问令牌时,请包括Drive API的作用域之一,例如
https://www.googleapis.com/auth/drive.readonly
。 - 而且,我认为可以将键值而不是访问令牌用作查询参数。
- 单击“部署”按钮作为新的“项目版本”。
- 自动打开“需要授权”对话框。
- 点击“查看权限”。
- 选择自己的帐户。
- 在“此应用未验证”中单击“高级”。
- 点击“转到###项目名称###(不安全)”
- 单击“允许”按钮。
- 单击“确定”。
- 复制Web应用程序的URL。就像
https://script.google.com/macros/s/###/exec
。- 修改Google Apps脚本后,请重新部署为新版本。这样,修改后的脚本将反映到Web Apps。请注意这一点。
3。使用自定义功能测试Web应用。
请将您的Web应用程序的URL设置为上述Google Apps脚本的baseUrl
,然后将Web Apps重新部署为新版本。这样,最新的脚本就会反映到Web应用程序中。
并且,作为对此脚本的测试,请将=abc(foo,bar)
放在单元格中。这样,将运行函数abc
,并使用UrlFetchApp检索结果值。
注意:
- 修改Web应用程序的脚本后,请重新部署Web应用程序为新版本。这样,最新脚本将反映到Web Apps。请注意这一点。
参考文献:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。