0

I am using react-google-apps-script to implement a Google Apps Script UI for a spreadsheet.

I have code in my server implementation as

const getActiveUserEmail = () => {
  return Session.getActiveUser().getEmail();
};

It is referenced from the client via:

import { getActiveUserEmail } from '../../../server';
const email = getActiveUserEmail();

After I deploy to a spreadsheet and run it, I get 'Session' undefined when the code is hit. I also get 'PropertiesService undefined' if that code is hit.

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

My appscript.json file contains

{
  "timeZone": "America/New_York",
  "dependencies": {},
  "exceptionLogging": "STACKDRIVER",
  "oauthScopes": [
    "https://www.googleapis.com/auth/script.container.ui",
    "https://www.googleapis.com/auth/spreadsheets",
    "https://www.googleapis.com/auth/script.external_request",
    "https://www.googleapis.com/auth/userinfo.email"
  ],
  "runtimeVersion": "V8"
}

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

Update: Enushi has provided a working code example.

Glenn
  • 1,996
  • 2
  • 24
  • 32

1 Answers1

1

It's very likely that ...

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

are in the in client-side code (someway they are passed to HtmlService.htmlOutput) rather in server-side code.

The above methods might be used with Html.htmlTemplate but should be included inside scriptlets to be executed as server-side code.

Related

References

Rubén
  • 34,714
  • 9
  • 70
  • 166
  • 1
    This was in fact the problem. The fact I was importing from server should have been a red flag but the autocomplete added it without me looking at my imports. The actual fix is to utilize the serverFunctions shim provided by the build system. e.g. `serverFunctions.getActiveUserEmail().then((response) => console.log(response))` as provided in [this example](https://github.com/enuchi/React-Google-Apps-Script/commit/d25c94d89909ad6a223b4f4ac7f88a9d69a6e427) – Glenn Oct 23 '22 at 01:53
  • 1
    I'm marking this as the solution - Don't directly access server side code from the client but use a shim! – Glenn Oct 23 '22 at 01:54