r/GoogleAppsScript • • Nov 14 '20

Question get user email in a web app run as me

Hi everyone,

for a bit of background, I work for a big company that wont allow me to make the tools to make my job faster so I am using a grey zone to be able to work on the company's data : using a google sheet as back end for a web app . it works fine for the most part. (as a side note: I am not a developer, I am an enthusiast so the questions are probably stupid, please bear with me :))

ok so my question : I would like to be able to retrieve the user's email when they visit my web app. I would use it as a way to pre fill certain fields and personalise the experience a bit. however considering I am using a google sheet as back end, I need to execute the app "as me" as far as I understand, if not I will need to give everyone access to the sheet and that's not desirable (I want users to add data in a controlled manner) nor practical. is that correct?

Is it also correct that I cannot retrieve the actual user's email if the web app runs 'as me'? if both are correct how do you guys do it? is there a way to get the user's email if the script is run as me? or is there a way to not need to give access to the backend spreadsheet to everyone in my company if I run the web app as the user?

thank you in advance !

6 Upvotes

8 comments sorted by

5

u/shm613 Nov 14 '20

That is correct you will need to set the script to run as you in order to not have to share the sheet with everyone but you can get the email for the person loading the app with the following function.

Session.getActiveUser();

I have had to do the same thing with my company because IT restricts access to most things within Gsuite.

1

u/elazard Nov 14 '20

Hey, thank you for your answer ! I will test that ASAP. by the way on that topic do you know if there is a more handy way to test that stuff than asking my colleagues to print screen the console haha? I havent yet found a way to test my stuff as "someone in my org just no me"

2

u/shm613 Nov 14 '20

Sadly I haven't found a good way to do any testing. The 2 apps I have made so far both needed an organizational chart database (was also created with Sheets and Gas) so I just made a copy of that and filled in all the email addresses as MyEmail+FirstNameLastName@domain (this keeps any emails being sent out by the app to come to you also) and when I wanted to test different levels I would just move my actual email to the person I wanted to test.

Not sure if that helps for what you are trying to do but it has been the only way I have found "to be someone else".

1

u/elazard Nov 15 '20

Thanks man I will try that

2

u/[deleted] Nov 15 '20

The way I do it is that my main account that's tied to my name is a normal pleb account like everyone else in my domain, then I have a separate GAPS account that I develop all my scripts on. I'm in the IT Department, though, so I can create/manage as many accounts as I need without needing to ask. So if you're not in the tech department, it may take some convincing to get that for yourself.

2

u/elazard Nov 15 '20

Yeah that’s the issue I am working really far from anything it so basically that’s why they simply won’t give me a server and a database to do this stuff normally and I have to use this grey zone .. anyway thanks man I will keep this idea on hand in case my situation changes one day

2

u/takishan Nov 15 '20

You can do this by having them logged in as their account so you can use the easy GAS function to get their email info, and then using the Sheets API to modify the sheet. Not the GAS sheets stuff, but the API you can run outside of GAS. Let me find link...

Here: https://developers.google.com/sheets/api

Be warned, it's kinda shitty working with the sheets api after getting used to how convenient GAS is lol

2

u/elazard Nov 15 '20

Thanks man I will take a look into it