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

How to use Web App approach when having functions run by editors on protected ranges, using Google Apps Scritps?

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

如何访问另一个文件的示例: 文件: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() 函数,并且要使用 Executed by: MeAccessed by: Anyone 进行部署。 但是,我不知道如何将它与绑定到电子表格的现有功能联系起来。

感谢您的attention/help!

你的情况,下面的修改怎么样?

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 应用程序。

详细信息见the official document

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

注:

参考文献: