Google Script document openbyid permission error

邮差的信 提交于 2019-12-25 08:13:24

问题


If in the onOpen function of a Google Doc I create a spreadsheet object by opening a spreadsheet by id, how can I pass that object to other functions if I'm using the menu system? (longer explanation below)

I have a google doc script that will take a snapshot of a document at a particular time and then export that to .docx and update a spreadsheet with the url of the snapshot. This is initiated by a menu option like so:

function onOpen(e) {
DocumentApp.getUi().createMenu('Senate Secretary Menu')
   .addItem('Snapshot', 'snapshot')
  .addToUi();
}

Which calls the function snapshot()

function snapshot() {
//open the spreadsheet using id
var ss=SpreadsheetApp.openById('spreadsheet_id');  

This generates a you do not have permission to call openbyid() error

I understand that google script prohibits openbyid() from being called in custom functions (which I interpreted as functions in spreadsheet, but whatevs). if I change the onOpen function to include the OpenById() call it will work.

function onOpen(e) {
DocumentApp.getUi().createMenu('Senate Secretary Menu')
   .addItem('Snapshot', 'snapshot')
  .addToUi();
var ss=SpreadsheetApp.openById('spreadsheet_id');
}

My question is how can I pass the file object from the onOpen function to the snapshot function?


回答1:


For anyone who might also have this problem. I took the menu template from the Google Quickstart, but it has @OnlyCurrentDoc flag in the header comment block that I missed. Took that out and my problem was solved.



来源:https://stackoverflow.com/questions/39730614/google-script-document-openbyid-permission-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!