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?
'spreadsheet_id'
? – Brady