当编辑器使用 Google Apps Scritps 在受保护范围内运行函数时,如何使用 Web 应用程序方法?

发布于 2025-01-16 09:54:48 字数 1439 浏览 1 评论 0原文

因此,电子表格包含多个函数,这些函数在编辑者单击按钮时运行。 有些函数从其他文件中获取数据,而编辑者无权访问这些数据。因此,数据被带入受保护的范围。编辑者将其输入添加到不受保护的范围中,当他们单击“保存”时,结果将保存回其他文件中。

如何访问另一个文件的示例: 文件: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最冷一天 2025-01-23 09:54:48

根据您的情况,进行以下修改如何?

1.修改脚本:

请将以下脚本复制并粘贴到脚本编辑器中并保存脚本。

在此脚本中,未声明 sheetCadProd ,因为我在您的显示脚本中看不到它。请小心这一点。

function doGet() {
  const CAD_PRODUTO = 'XXXXXXXXXXXXxxxxxxxxxxxxxxxx';
  const config = {
    get ssBDCadProd() {
      delete this.ssBDCadProd;
      return (this.ssBDCadProd = SpreadsheetApp.openById(CAD_PRODUTO));
    }
  }
  let ref = 0;
  const sheetBDCadProd = config.ssBDCadProd.getSheetByName('CadProduto');
  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);
  return ContentService.createTextOutput(JSON.stringify({ range: refRng.offset(0, 2).getA1Notation() }));
}

//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 {
      const res = UrlFetchApp.fetch("https://script.google.com/macros/s/###/exec");
      console.log(res.getContentText());
      const { range } = JSON.parse(res.getContentText());
      sheetCadProd.getRange(range).activate();
    }
  } catch (err) {
    Browser.msgBox('The following error has occurred: ' + err);
  }
}

2. 部署 Web 应用程序。

详细信息可以参见官方文档

  1. 在脚本编辑器上,在脚本编辑器的右上角,请点击“点击部署”-> “新部署”。
  2. 请点击“选择类型”-> “网络应用程序”。
  3. 请在“部署配置”下的字段中输入有关Web应用程序的信息。
  4. 请为“执行为”选择“我”
    • 这就是此解决方法的重要性。
  5. 请为“谁有权访问”选择“任何人”
    • 根据您的情况,我认为此设置可能合适。
  6. 请点击“部署”按钮。
  7. 复制 Web 应用程序的 URL。就像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.

function doGet() {
  const CAD_PRODUTO = 'XXXXXXXXXXXXxxxxxxxxxxxxxxxx';
  const config = {
    get ssBDCadProd() {
      delete this.ssBDCadProd;
      return (this.ssBDCadProd = SpreadsheetApp.openById(CAD_PRODUTO));
    }
  }
  let ref = 0;
  const sheetBDCadProd = config.ssBDCadProd.getSheetByName('CadProduto');
  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);
  return ContentService.createTextOutput(JSON.stringify({ range: refRng.offset(0, 2).getA1Notation() }));
}

//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 {
      const res = UrlFetchApp.fetch("https://script.google.com/macros/s/###/exec");
      console.log(res.getContentText());
      const { range } = JSON.parse(res.getContentText());
      sheetCadProd.getRange(range).activate();
    }
  } catch (err) {
    Browser.msgBox('The following error has occurred: ' + err);
  }
}

2. Deploy Web Apps.

The detailed information can be seen at the official document.

  1. On the script editor, at the top right of the script editor, please click "click Deploy" -> "New deployment".
  2. Please click "Select type" -> "Web App".
  3. Please input the information about the Web App in the fields under "Deployment configuration".
  4. Please select "Me" for "Execute as".
    • This is the importance of this workaround.
  5. Please select "Anyone" for "Who has access".
    • In your situation, I thought that this setting might be suitable.
  6. Please click "Deploy" button.
  7. Copy the URL of the Web App. It's like 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:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文