例外情况:您无权以非所有者用户身份从 Google 电子表格 (GAS) 访问调用网络应用程序的请求文档

Exception: You do not have permission to access the requested document calling web app from Google Spreadsheet (GAS) as a non-owner user

我创建了一个包含受保护范围的 Google Spreadsheet,因此,域用户无法执行某些任务,例如对 sheet 进行排序。 因此,我创建了一个在我的帐户(管理员)下运行的网络应用程序并执行此操作。当我 运行 脚本时,反复试验让它起作用。 问题是当其他域用户调用 Web 应用程序时会抛出以下错误:

Google 传播sheet 代码:

function onOpen() {
  var ui = SpreadsheetApp.getUi();
  var m = ui.createMenu('Extra');
  m.addItem('↑↓ Surname','sortByName');
  m.addItem('↑↓ Section','sortBySection');
  m.addItem('↑↓ DOB','sortByDOB');
  m.addToUi();
}

function sortByName(){
  sortSheet("byName");
}
function sortBySection(){
  sortSheet("bySection");
} 
function sortByDOB(){
  sortSheet("byDOB");
}

function sortSheet(sortType){
  if (!isClassSheet()){
    return;
  }
  var sheet = aS.getActiveSheet();
  var sheetId = aS.getId();
  var baseUrl ="https://script.google.com/a/macros/ourdomain.com/s/xxxx/exec"
  var queryString = "?sheetID="+sheetId+"&sortType="+sortType+"&sheetName="+sheet.getName()+"&firstDataRow="+FIRST_DATA_ROW+"&classColStudentName="+S_CLASS_COL_STUDENT_NAME+"&classColSection="+S_CLASS_COL_SECTION+"&classColDOB="+S_STUDENTS_COL_DOB;
  var url = encodeURI(baseUrl + queryString)
  var params = {method: "get", headers: {Authorization: "Bearer " + ScriptApp.getOAuthToken()}};
  var request = UrlFetchApp.fetch(url,params)
    //logsheet.appendRow([request])
  Logger.log(request)
}

未绑定的 Web 应用程序代码:

function doGet(e) {
  var param = e.queryString;
  var parameters = param.split("&")  // This just checks only 7 parameters are present else gives a invalid link

  if (param != null && parameters.length == 7){
    param = e.parameter;
    var sheetId = param.sheetID;
    var name = param.sheetName;
    var sortType = param.sortType;
    var S_CLASS_COL_STUDENT_NAME = Number(param.classColStudentName);
    var S_CLASS_COL_SECTION = Number(param.classColSection);
    var S_STUDENTS_COL_DOB = Number(param.classColDOB);
    var FIRST_DATA_ROW = Number(param.firstDataRow);
  } else {
      return ContentService.createTextOutput("Bad")
    }
  try{  
     var ss = SpreadsheetApp.openById(sheetId)  
     var sheet = ss.getSheetByName(name)
     var dataRange = sheet.getRange(FIRST_DATA_ROW,1,(sheet.getLastRow() - FIRST_DATA_ROW + 1),sheet.getLastColumn()); 
     
     switch (sortType){
       case "byName":
          dataRange.sort({column: S_CLASS_COL_STUDENT_NAME, ascending: true});
          break;
       case "bySection":
          dataRange.sort([{column: S_CLASS_COL_SECTION, ascending: true}, {column: S_CLASS_COL_STUDENT_NAME, ascending: true}]);
          break;  
       case "byDOB":
          dataRange.sort([{column: S_STUDENTS_COL_DOB, ascending: true}, {column: S_CLASS_COL_STUDENT_NAME, ascending: true}]);
          break;   
     }
     
     }
     catch (err){
      return ContentService.createTextOutput(err)
     }
 return ContentService.createTextOutput("Success")
}

部署设置

对于对此感兴趣的任何人,我已阅读 this document,其中解释了网络应用程序必须与通过电子表格调用它的用户共享。感谢@TheMaster 指出并感谢@Tanaike 编写文档