r/GoogleAppsScript • • 6d ago

Resolved SpreadsheetApp.openById related issue

Previous post: https://www.reddit.com/r/GoogleAppsScript/comments/1xzjvlk/how_to_call_a_function_from_another_project_when/

I previously used the following code:

ss = SpreadsheetApp.getActiveSpreadsheet();

However, I have now changed it to:

ss = SpreadsheetApp.openById("1iD........Yq8");

The reason for this change is that I need to call the function (reason: above post).

I have an onOpen() function. Previously when I opened the sheet for first time, it worked correctly with SpreadsheetApp.getActiveSpreadsheet(), but it seems to fail when I use SpreadsheetApp.openById() instead.

Interestingly, if the spreadsheet is already open and I manually run the onOpen() function, it works without any issues.

I'm not entirely sure what is causing this behavior, it seems onOpen does not work well with SpreadsheetApp.openById. Could it be a timing issue, where the spreadsheet ID or access to the spreadsheet is not yet available when onOpen() first runs?

Is there a way to work around this issue?

Edit: I solved it by changing onOpen to a different name handleOpen, then create a trigger to run handleOpen when opening the sheet.

3 Upvotes

10 comments sorted by

4

u/marcnotmark925 6d ago

Simple Triggers are not allowed to run code that requires auth scopes.

1

u/mommasaidmommasaid 6d ago

True, but I believe the issue here is that onOpen() is only called when a user opens the sheet, not when you programmatically open it with SpreadsheetApp.openById()

1

u/marcnotmark925 6d ago

I do not believe that is the case

1

u/mommasaidmommasaid 6d ago

Rereading OP's post I'm not entirely sure what he's doing.

But regarding when onOpen() is called... I'm pretty confident about that and a simple test seems to verify it. Happy to be proven wrong.

It's similar to how onEdit() isn't called when you programmatically edit a sheet.

1

u/marcnotmark925 6d ago

I meant that's not OP's case. I agree they don't work like that.

0

u/VAer1 6d ago

Edit: I solved it by changing onOpen to a different name handleOpen, then create a trigger to run handleOpen when opening the sheet.

1

u/delaney1414 6d ago

Custom triggers will work with scopes just keep in mind it runs from your auth so if you have a ticker that counts each time the sheet is opened, when you check edit history you will be the only user editing that field

1

u/VAer1 6d ago edited 6d ago

That’s a good point. Files that are accessed frequently may contribute to higher quota usage.

Edit: I will create a button Refresh, and assign "handleOpen" to the button.

1

u/BewareTheGiant 6d ago

The issue is probably, as the error indicates, related to the permissions. You probably have a narrow scope only to the current sheet, which is why getActiveSpreadsheet() and onOpen() work. openById, however, requires access to all your spreadsheets, because it isn't checking whether the id you are passing is the id the script is attached to

0

u/VAer1 6d ago

Edit: I solved it by changing onOpen to a different name handleOpen, then create a trigger to run handleOpen when opening the sheet.