Class Session Stay organized with collections Save and categorize content based on your preferences.
The Session class provides access to session information, such as the user's email address (insome circumstances) and language setting.
Methods
Method | Return type | Brief description |
---|---|---|
get | User | Gets information about the current user. |
get | String | Gets the language setting of the current user as a string—for example,en for English. |
get | User | Gets information about the user under whose authority the script is running. |
get | String | Gets the time zone of the script. |
get | String | Gets a temporary key that is unique to the active user but does not reveal the user identity. |
Deprecated methods
Method | Return type | Brief description |
---|---|---|
| String | Gets the time zone of the script. |
| User | Gets information about the current user. |
Detailed documentation
getActiveUser()
Gets information about the current user. If security policies do not allow access to the user'sidentity,User.getEmail()
returns a blank string. The circumstances in which theemail address is available vary: for example, the user's email address is not available in anycontext that allows a script to run without that user's authorization, like a simpleon
oron
trigger, a custom function in Google Sheets, or a web appdeployed 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 themselvesor belongs to the same Google Workspace domain as the user.
// Log the email address of the person running the script.constemail=Session.getActiveUser().getEmail();Logger.log(email);
Return
User
— the current user
Authorization
Scripts that use this method require authorization with one or more of the followingscopes:
https://www.googleapis.com/auth/userinfo.email
getActiveUserLocale()
Gets the language setting of the current user as a string—for example,en
for English.
// Log the language setting of the person running the script.Logger.log(Session.getActiveUserLocale());
Return
String
— a string that represents the user's language setting
getEffectiveUser()
Gets information about the user under whose authority the script is running. If the script is aweb app set to "execute as me" (the developer), this returns the developer's user account. Ifthe script is running under aninstallabletrigger, this returns the account of the user who created the trigger. In most otherscenarios, this returns the same account asget
.
// Log the email address of the user under whose authority the script is// running.constemail=Session.getEffectiveUser().getEmail();Logger.log(email);
Return
User
— the user under whose authority the script is running
Authorization
Scripts that use this method require authorization with one or more of the followingscopes:
https://www.googleapis.com/auth/userinfo.email
getScriptTimeZone()
Gets the time zone of the script. New scripts default to the owner's time zone, but thescript's time zone can be changed by clickingFile > Project properties in the scripteditor. Note that spreadsheets have a separate time zone, which can be changed by clickingFile > Spreadsheet settings in Google Sheets. Spreadsheet time zones that differ fromthe script time zone are a frequent source of scripting bugs.
// Log the time zone of the script.consttimeZone=Session.getScriptTimeZone();Logger.log(timeZone);
Return
String
— the time zone of the script
getTemporaryActiveUserKey()
Gets a temporary key that is unique to the active user but does not reveal the user identity.The temporary key rotates every 30 days and is unique to the script.
// Log the temporary key of the person running the script.Logger.log(Session.getTemporaryActiveUserKey());
Return
String
— the temporary active user key
Deprecated methods
getTimeZone()
getTimeZone()
Deprecated. This function is deprecated and should not be used in new scripts.
Gets the time zone of the script. New scripts default to the owner's time zone, but thescript's time zone can be changed by clickingFile > Project properties in the scripteditor. Note that spreadsheets have a separate time zone, which can be changed by clickingFile > Spreadsheet settings in Google Sheets. Spreadsheet time zones that differ fromthe script time zone are a frequent source of scripting bugs.
// Log the time zone of the script.consttimeZone=Session.getTimeZone();Logger.log(timeZone);
Return
String
— the time zone of the script
getUser()
getUser()
Deprecated. This function is deprecated and should not be used in new scripts.
Gets information about the current user.
Return
User
— the currently signed in user
Authorization
Scripts that use this method require authorization with one or more of the followingscopes:
https://www.googleapis.com/auth/userinfo.email
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-12-02 UTC.