How to get current user name (mail id) in app script - google-apps-script

How can I get the current user's email address using Google Apps Script?
For example, if my Google Sheet is shared among different users and multiple users are accessing the same sheet concurrently.
I'd like to capture the individual user name for whoever has modified text in Column A and record it in column Z.
I've tried using:
Session.getActiveUser().getEmail();
However this isn't working, it returns nothing.

Related

Google Sheets Script access searching contacts dialog

Is it possible to access my Google contacts from Google Sheets? I've created a simple spread sheet where I enter information for my drivers. Once the data on a row is entered it may be modified by other users that access the same sheet, but eventually - one of the users will need to send the data in that row to a specific driver via email. I currently accomplish this by using a Google Sheets Script to take the row values and build a mailto: hyperlink onEdit() in the last cell:
var emailtoString = '=HYPERLINK("mailto:changethis#address.com?&subject=' + subjectData + '&body=' + formatedData + '", "send email")';
e.source.getActiveSheet().getRange(row,9).setValue(emailtoString);
Which is working as expected and allows for whatever users is clicking the hyperlink to open the preformated gmail in their account - they can then delete the "changethis#address.com" email address that is allowing the hyperlink formula to create and then they have access to their contacts in the to: field - so if they start typing a user name, a selection list displays to let them select the contact.
This works, but separates the process of sending the email from the sheets script - just to get access to the drop down contacts list. If I could access that TO: dialog directly from the sheet cell - so the user sending the email is really just selecting an address from their contacts, then I could use the sheets script to just send the email message without having to open a gmail message and I could further flag another cell as "Message sent to emailaddress". By using the hyperlink it is making it difficult for me to figure out how to set a flag letting other users know an email has already been sent to this driver. So I am wondering if its possible avoid all this hyperlink creation and instead, just present the user(s) of the spread sheet with a selection dialog from the user accessing the sheets personal contacts so they can select someone to send the email and I can script sending the email rather than building the Hyperlink to open Gmail.
From the question
Is it possible to access my Google contacts from Google Sheets?
Yes it's possible. Use the Contacts Service (ContactApp)
Reference
https://developers.google.com/apps-script/reference/contacts
Related
Create a Contact Group via Google Spreadsheet

Does "Calendar.getName()" function run right? It seems same to "Calendar.getId()"

Calendar.getName() function returns calendar owner's email address.This behavior is the same as Calendar.getId().
I want to get the calendar owner's display name.
What function should I use?
This is for Google Apps Script.
https://developers.google.com/apps-script/reference/calendar/calendar
Logger.log('calendar.getName() : ' + calendar.getName());
I expect the output of Mizuki Kojima[IT department], but the actual output is mkojima51#example.co.jp.
The Calendar.getName() function is working as expected: It returns the display name of the calendar, and in case the calendar is a primary calendar of a user and its name has never been edited, it will be the same as the obtained via Calendar.getId() (the user's e-mail address).
If what you want is to get the user's name of the calendar owner, you only have one option. Also, this will be assuming that you are using a G Suite account, and that both the one executing the script and the calendar owner are part of the same domain:
Get the calendar's id. If it is an e-mail (there are many ways you can do this verification), that means that the owner of it has an account with that same e-mail.
Get the user's profile using the G Suite Admin SDK. For that, you will make a call to Users.get(). Afterwards, you can get the name and surname from the profile object returned.

How to fetch user ID in google script?

I am trying to create a Google script linked to a spreadsheet containing multiple email ids and some data related to them. The script will send an email containing an HTML button to every id in the spreadsheet. What I am doing is that when the user clicks that button in its mail, it will fetch the data from the spreadsheet respective to that email id.
What I am unable to do is how to fetch user id of that particular user when that user clicks the button (that script sent in the mail) and send it with the POST response, so that script can know which user is asking for information and fetch data from spreadsheet only for that particular user.
I followed Casper's suggestion and used the email address of the active user and then split it to get the id part which then I can verify.

Display the result of a calculation in Google Sheet to the user after he sent a Google Form

I created a Google Form that send its results to a Google Sheet for further calculations. Therefore, I'd like to display the results of those calculations to the user once he has sent the Google Form (either instantly or via email).
How can I achieve this ? Should I use a module ? Or a Google Apps Script ?
Thanks for your time
Considering that, You are collecting data from multiple user into the spread sheet collect their email Id's as well along with other data,
How to get email Id column in spreadsheet?
Go inside google form edit mode -> see the settings icon from the right corner of the page > click on that -> check the Collect email addresses checkbox -> Save.
Now, you will have Email Address column in the spreadsheet.
Lets suppose you are receiving the marks of 3 subject, calculate the sum of those these 3 values, same for each users records(row) in loop or use VLOOKUP/ formula to calculate sum, get email address from the same row of the respective user and send mail to that user using MailApp api and add sum inside email body with some text formatting.
Perhaps, check out two addons for Google sheets, autoCract and Docotpus. We use them all the time to get and send student's results. They are very helpful.

Google Sheets : format a cell differently based on user viewing the sheet?

Is it possible to format a cell (set the background color, for instance) based on the user currently viewing the sheet?
I'm using Google Sheets for an employee scheduling application and would like to highlight the name of the employee currently viewing the sheet everywhere that name appears on the schedule.
So if Tom opens the sheet, all cells with "Tom" in them would appear highlighted. If Bob opens the same sheet, all cells with "Bob" in them would appear highlighted.
Thanks in advance!
According to Google Apps Script:
getActiveUser()
Gets information about the current user. Returns a User object
describing the current user. In limited-privilege executions (such as
in response to onOpen or onEdit events), we only return the identity
of the active user if both the user and the script owner are part of
the same domain. This is to protect the privacy of consumer users, who
may not want their email address exposed. For more information, see
the active user entry in the Glossary.
So if you have someone using the spreadsheet from within your domain (for business, education), then you could have an onOpen script and attempt a filter/highlight function based on your parameters and script.
Cheers!
You could create specific filter views for your team. Select Data > Filter Views and name your filter view after the teammate. Filter views allow different people to view the same sheet differently. I may be wrong about this, but I don't believe there is a way to turn the filter views on and off programmatically through GAS, but each filter view has a unique URL, so you could use getActiveUser() in conjunction with an alert prompt linking the active user to their personal filter view.