如何通过 openById 或 openByUrl 获取打开电子表格的权限。

huangapple go评论65阅读模式
英文:

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?

huangapple
  • 本文由 发表于 2023年6月18日 20:06:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76500452.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定