0
votes

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
Just to clarify, are you using the actual spreadsheet ID (a long sequence of letters and numbers) or the string 'spreadsheet_id'?Brady
it is actually a long string of numbers. But I found the error posted below.rwreed

1 Answers

0
votes

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.