例外:您无权以非所有者用户身份从 Google 电子表格 (GAS) 访问调用网络应用程序所请求的文档
我创建了一个包含受保护范围的 Google 电子表格,因此域用户无法执行某些任务,例如对表格进行排序。 因此,我创建了一个在我的帐户(管理员)下运行的网络应用程序并执行此操作。当我运行脚本时,反复试验使其正常工作。 问题是,当其他域用户调用 Web 应用程序时,会引发以下错误:
Google 电子表格代码:
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")
}
部署设置
I have created a Google Spreadsheet which includes protected ranges, therefore, domain users cannot perform certain tasks, for instance sorting the sheet.
As such, I have created a web app which runs under my account (admin) and does this. Trial and error got it to work, when I am running the script.
The issue is that when other domain users are invoking the web app the following error is thrown:
Google Spreadsheet code:
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)
}
Unbound Web App code:
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")
}
Deployment settings
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于对此感兴趣的任何人,我已阅读 该文档解释了网络应用程序必须与通过电子表格调用它的用户共享。感谢@TheMaster 指出这一点并感谢@Tanaike 撰写文档
For anyone who's interested in this, I have read this document which explains that the web app must be shared with users who are invoking it through the spreadsheet. Thanks to @TheMaster for pointing it out and to @Tanaike for writing the document