enuchi / React-Google-Apps-Script

This is your boilerplate project for developing React apps inside Google Sheets, Docs, Forms and Slides projects. It's perfect for personal projects and for publishing complex add-ons in the Google Workspace Marketplace.
MIT License
1.34k stars 178 forks source link

Session and PropertiesService undefined #147

Closed glenne closed 2 years ago

glenne commented 2 years ago

I have code in my server implementation as

const email = Session.getActiveUser().getEmail();
const props = PropertiesService.getScriptProperties().getProperties();

After I deploy and run it, I get 'Session' undefined. I also get 'PropertiesService undefined' if that code is hit.

I'm able to use HtmlService, SpreadsheetApp, and UrlFetch apis without issue.

Why are these undefined and how can force them to be defined?

enuchi commented 2 years ago

Hm, maybe you can share sample repo you are using? I was able to get this to work. First you need to add the right permissions in the appscript.json file. If you run as is you should get an error like Exception: You do not have permission to call Session.getActiveUser. Required permissions: https://www.googleapis.com/auth/userinfo.email. Add "https://www.googleapis.com/auth/userinfo.email" to the list of scopes in appscript.json if you haven't already. I can provide a minimal example if you need it.

enuchi commented 2 years ago

See sample code here: https://github.com/enuchi/React-Google-Apps-Script/commit/d25c94d89909ad6a223b4f4ac7f88a9d69a6e427

glenne commented 2 years ago

My problem was my vscode autocomplete when referencing the server function added a direct import from ../../server instead of going thru the serverFunctions shim. Your example was very helpful in ferreting out the source of my error. Perhaps an enhancement would be to somehow cause client code to not autocomplete to server code or end with a build failure.

I appreciate you taking the time to build an example - very helpful! Thanks.