英文:
How to acquire permission for opening spreadsheet using openById or openByUrl
问题
I was trying to store user entered data to other spreadsheet so I declared some global variables for calling sheets of other spreadsheet and called spreadsheet using openByUrl function. It worked.
After this I tried adding custom menu to my current spreadsheet from where I have to take user entry and tried to add menu to menu bar onOpen but execution failed. Stating error -
Jun 18, 2023, 4:58:57 PM Error Exception: You do not have permission to call SpreadsheetApp.openByUrl. Required permissions: https://www.googleapis.com/auth/spreadsheets
at unknown function
And when I comment out variable declaration it runs.
Codes are as under -
var edUrl = "https://docs.google.com/spreadsheets/d/1qTUKifG4qsk2quRok4XIMYnPO-gzcJlECgCmjxnclaQ/edit";
var ssData = SpreadsheetApp.openByUrl(edUrl);
var wsClients = ssData.getSheetByName("Clients");
var wsCliPrj = ssData.getSheetByName("CLI-PRJ");
var wsEstData = ssData.getSheetByName("EstimateData");
var wsPrjEst = ssData.getSheetByName("PRJ-EST");
var wsEstTc = ssData.getSheetByName("EST-TC");
var wsItems = ssData.getSheetByName("Items");
function onOpen(e){
menu();
}
function menu() {
var ui = SpreadsheetApp.getUi();
var menu = ui.createMenu('GHAR');
menu.addItem('My Menu Item', 'myFunction');
menu.addToUi();
}
function myFunction(){
SpreadsheetApp.getActiveSpreadsheet().getRange("A1").setValue("Hello");
}
This is the error shown.
Error Required Permission image
I want to add custom menu and designate some functions to those menu in which it will read and write to different spreadsheet.
英文:
I was trying to store user entered data to other spreadsheet so I declared some global variables for calling sheets of other spreadsheet and called spreadsheet using openByUrl function. It worked.
After this I tried adding custom menu to my current spreadsheet from where I have to take user entry and tried to add menu to menu bar onOpen but execution failed. Stating error -
Jun 18, 2023, 4:58:57 PM Error Exception: You do not have permission to call SpreadsheetApp.openByUrl. Required permissions: https://www.googleapis.com/auth/spreadsheets
at unknown function
And when I comment out variable declaration it runs.
Codes are as under -
var edUrl = "https://docs.google.com/spreadsheets/d/1qTUKifG4qsk2quRok4XIMYnPO-gzcJlECgCmjxnclaQ/edit";
var ssData = SpreadsheetApp.openByUrl(edUrl);
var wsClients = ssData.getSheetByName("Clients");
var wsCliPrj = ssData.getSheetByName("CLI-PRJ");
var wsEstData = ssData.getSheetByName("EstimateData");
var wsPrjEst = ssData.getSheetByName("PRJ-EST");
var wsEstTc = ssData.getSheetByName("EST-TC");
var wsItems = ssData.getSheetByName("Items");
function onOpen(e){
menu();
}
function menu() {
var ui = SpreadsheetApp.getUi();
var menu = ui.createMenu('GHAR');
menu.addItem('My Menu Item', 'myFunction');
menu.addToUi();
}
function myFunction(){
SpreadsheetApp.getActiveSpreadsheet().getRange("A1").setValue("Hello");
}
This is the error shown.
Error Required Permission image
I want to add custom menu and designate some functions to those menu in which it will read and write to different spreadsheet.
答案1
得分: 0
onOpen(e)函数在受限制的上下文中运行,其中不可用需要授权的方法。 SpreadsheetApp.openByUrl()需要授权,因此会导致onOpen(e)出现错误。
在onOpen(e)中没有调用SpreadsheetApp.openByUrl(),但它出现在一个全局变量的赋值中,该赋值在onOpen(e)运行之前进行评估。
您可以通过将所有全局变量移至初始化函数内部或使用getter来避免此问题。有关示例代码,请参阅如何在避免权限错误的同时使用全局变量?
英文:
The onOpen(e) function runs in a restricted context where methods that require authorization are not available. SpreadsheetApp.openByUrl() requires authorization, and will thus cause onOpen(e) to error out.
You are not calling SpreadsheetApp.openByUrl() in onOpen(e), but it appears in the assignment of a global variable which gets evaluated before onOpen(e) runs.
You can avoid the issue by moving all global variables inside an initializer function or by using a getter. For sample code, see How to use global variables while avoiding permission errors?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论