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

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.

Related

onEdit event object active user is returning my email address instead of the user doing the edit

I have a Google sheet with an installed trigger / app script. The script uses the event object (I call it e) to do various things. One thing I am trying is to get the email address of the user who is doing the editing of the Google sheets. So, if the event object is e, the e.user should return the user id / email address of the person logged into the Google sheet and performing the action. But, what it does instead do is return MY email address. I'm guessing the e.user id is the id of the user the app script is running under. Can any Google engineers chime in? How can I get the user id of the person doing the editing?
Ok, wow, I finally got this working, and that was a weird one. So, if the trigger is installable as opposed to simple, and the users doing the editing belong to my same domain, then the e.user should work. It wasn't. What I eventually figured out is when one of the shared collaborators opens up the apps script editor from the container, for some reason, everything started working. So, I'm now able to get the user id's and emails from all the users in my domain when they perform an action. It seems like this is a bug. I dunno. But it works now.
You can obtain the User's Email with e.user.getEmail();
Here's the documentation comment:
getEmail()
Gets the user's email address, if available. If security policies do not allow access to the user's email address, this method returns a blank string. The circumstances in which the email address is available vary: for example, the user's email address is not available in any context that allows a script to run without that user's authorization, like a simple onOpen(e) or onEdit(e) trigger, a custom function in Google Sheets, or a web app deployed to "execute as me" (that is, authorized by the developer instead of the user). However, these restrictions generally do not apply if the developer runs the script themselves or belongs to the same G Suite domain as the user.

How to get current user name (mail id) in app 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.

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 Apps Script - get/assign user unique identifier

I am developing a Google Docs Add-on and need to know if the user currently using the add-on has used it before in the current document or any document. I can't figure out which service gives me some kind of unique identifier for the current user that I can use to identify the current user.
The base service's User.getEmail is not good enough as it doesn't give access to the email under certain conditions and is not reliable.
Thoughts on best way to do this?
Unfortunately, there is no such service. Google wants users' personal information to remain private, so AFAIK you won't usually get even user's email, unless your script is written for Google Apps domain and the user is in the same domain as the script's author.
The only thing you can do in the situation is use something like DocumentApp.getActiveDocument().getId() and check if the script was used in this document, but of course this won't give you an idea if user has used your script in other documents or not.
Also you can show a Prompt Dialog to user, asking him to enter email, but of course there is no way you can check if he entered his real email.
Unique user identifier you can get for apps script is Session.getEffectiveUser().getEmail()
This will return the email of user for which the script run. This is better than ActiveUser because if you use trigger EffectiveUser will well return the email of the user who install the script.
I use that for several apps to identify license assignment and it works great.
It looks like the best solution is to roll your own solution using the following to set the userId at an appropriate time:
PropertiesService.getUserProperties().setProperty("userId",<someGeneratedUId>)
then whenever you want to look up if this user is a recognizable user,
var userId = PropertiesServices.getUserProperties().getProperty("userId")
Looks like once set, the UserProperties thus set is available to this particular user (per user) running this add-on everywhere (i.e. all docs, not just the current on) that have this particular add-on enabled/running.
I'll validate this further but seems like this works in my tests.

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.