当编辑器使用 Google Apps Scritps 在受保护范围内运行函数时,如何使用 Web 应用程序方法?
因此,电子表格包含多个函数,这些函数在编辑者单击按钮时运行。 有些函数从其他文件中获取数据,而编辑者无权访问这些数据。因此,数据被带入受保护的范围。编辑者将其输入添加到不受保护的范围中,当他们单击“保存”时,结果将保存回其他文件中。
如何访问另一个文件的示例: 文件:globals.gs
const CAD_PRODUTO = 'XXXXXXXXXXXXxxxxxxxxxxxxxxxx';
const config = {
get ssBDCadProd() {
delete this.ssBDCadProd;
return (this.ssBDCadProd = SpreadsheetApp.openById(CAD_PRODUTO));
}
}
const sheetBDCadProd = config.ssBDCadProd.getSheetByName('CadProduto');
这是一个使用上面文件中的数据的函数:
//It generates the next item code, based on the last record in the file above
function gerarRef() {
try {
const response = Browser.msgBox('Would you like to confirm it?', Browser.Buttons.YES_NO);
if (response == 'no') {
return;
} else {
let ref = 0;
const refExistentes = sheetBDCadProd.getRange(2, 1, sheetBDCadProd.getLastRow(), 1).getValues();
let ultRef = Math.max(...refExistentes);
Logger.log('Ult.: ' + ultRef)
if (ultRef == 0 || ultRef == '') {
ref = 10000;
} else {
ref += ultRef + 1;
}
const refRng = sheetCadProd.getRange('B5').setValue(ref);
refRng.offset(0, 2).activate();
}
} catch (err) {
Browser.msgBox('The following error has occurred: ' + err);
}
}
我知道 Web App
方法需要向其中添加 doGet()
函数,并且它是将通过执行者:我
和访问者:任何人
进行部署。 但是,我不知道如何将它与绑定到电子表格的现有函数结合在一起。
感谢您的关注/帮助!
So, the spreadsheet contains multiple functions which run as the editors click on buttons.
Some of the functions bring data from other files, which editors don't have access to. So, data are brought into protected ranges. Editors add their inputs into unprotected ranges and as they click on Save, the results are saved back into other files.
Example of how another file is accessed:
file: globals.gs
const CAD_PRODUTO = 'XXXXXXXXXXXXxxxxxxxxxxxxxxxx';
const config = {
get ssBDCadProd() {
delete this.ssBDCadProd;
return (this.ssBDCadProd = SpreadsheetApp.openById(CAD_PRODUTO));
}
}
const sheetBDCadProd = config.ssBDCadProd.getSheetByName('CadProduto');
Here's a function using the data from the file above:
//It generates the next item code, based on the last record in the file above
function gerarRef() {
try {
const response = Browser.msgBox('Would you like to confirm it?', Browser.Buttons.YES_NO);
if (response == 'no') {
return;
} else {
let ref = 0;
const refExistentes = sheetBDCadProd.getRange(2, 1, sheetBDCadProd.getLastRow(), 1).getValues();
let ultRef = Math.max(...refExistentes);
Logger.log('Ult.: ' + ultRef)
if (ultRef == 0 || ultRef == '') {
ref = 10000;
} else {
ref += ultRef + 1;
}
const refRng = sheetCadProd.getRange('B5').setValue(ref);
refRng.offset(0, 2).activate();
}
} catch (err) {
Browser.msgBox('The following error has occurred: ' + err);
}
}
I understand that the Web App
approach requires a doGet()
function to be added to it and it's to be deployed with Executed by: Me
and Accessed by: Anyone
.
However, I don't know how to tie it together with the existing functions I got bounded to the spreadsheet.
Appreciate your attention/help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据您的情况,进行以下修改如何?
1.修改脚本:
请将以下脚本复制并粘贴到脚本编辑器中并保存脚本。
在此脚本中,未声明
sheetCadProd
,因为我在您的显示脚本中看不到它。请小心这一点。2. 部署 Web 应用程序。
详细信息可以参见官方文档。
https://script.google.com/macros/s/###/exec
。此外,请将您的 Web Apps URL 设置为
const res = UrlFetchApp.fetch("https://script.google.com/macros/s/###/exec");
上述脚本的。并且,请再次将最新的脚本反映到Web Apps。这样,你的脚本就可以工作了。请小心这一点。3. 测试。
在使用此脚本之前,请检查
sheetCadProd
。 在我修改后的脚本中,未声明sheetCadProd
,因为我在您的显示脚本中看不到它。请小心这一点。在上面的脚本中,请运行
gerarRef()
。这样,Web Apps 的脚本就由Web Apps 的所有者来运行。注意:
参考资料:
In your situation, how about the following modification?
1. Modified script:
Please copy and paste the following script to the script editor and save the script.
In this script,
sheetCadProd
is not declared, because I cannot see it in your showing script. Please be careful this.2. Deploy Web Apps.
The detailed information can be seen at the official document.
https://script.google.com/macros/s/###/exec
.And also, please set your Web Apps URL to
const res = UrlFetchApp.fetch("https://script.google.com/macros/s/###/exec");
of the above script. And, please reflect the latest script to the Web Apps again. By this, your script works. Please be careful this.3. Testing.
Before you use this script, please check
sheetCadProd
. In my modified script,sheetCadProd
is not declared, because I cannot see it in your showing script. Please be careful this.In the above script, please run
gerarRef()
. By this, the script of Web Apps is run by the owner of Web Apps.Note:
References: