Class Session
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
เซสชัน
Class เซสชันให้สิทธิ์เข้าถึงข้อมูลเซสชัน เช่น อีเมลของผู้ใช้ (ในบางกรณี) และการตั้งค่าภาษา
เอกสารประกอบโดยละเอียด
getActiveUser()
รับข้อมูลเกี่ยวกับผู้ใช้ปัจจุบัน หากนโยบายความปลอดภัยไม่อนุญาตให้เข้าถึงข้อมูลประจำตัวของผู้ใช้ User.getEmail()
จะแสดงผลสตริงว่าง บริบทที่อีเมลพร้อมใช้งานจะแตกต่างกันไป เช่น อีเมลของผู้ใช้จะไม่พร้อมใช้งานในบริบทใดก็ตามที่อนุญาตให้สคริปต์ทำงานโดยไม่ได้รับอนุญาตจากผู้ใช้รายนั้น เช่น ทริกเกอร์ onOpen(e)
หรือ onEdit(e)
ธรรมดา ฟังก์ชันที่กำหนดเองใน Google ชีต หรือเว็บแอปที่ทําให้ "เรียกใช้ในฐานะฉัน" (นั่นคือ ได้รับอนุญาตจากนักพัฒนาซอฟต์แวร์แทนผู้ใช้)
อย่างไรก็ตาม โดยทั่วไปแล้ว ข้อจำกัดเหล่านี้จะไม่มีผลหากนักพัฒนาซอฟต์แวร์เรียกใช้สคริปต์ด้วยตนเองหรืออยู่ในโดเมน Google Workspace เดียวกับผู้ใช้
// Log the email address of the person running the script.
const email = Session.getActiveUser().getEmail();
Logger.log(email);
รีเทิร์น
User
— ผู้ใช้ปัจจุบัน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับอนุญาตด้วยขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/userinfo.email
getActiveUserLocale()
รับการตั้งค่าภาษาของผู้ใช้ปัจจุบันเป็นสตริง เช่น en
สำหรับภาษาอังกฤษ
// Log the language setting of the person running the script.
Logger.log(Session.getActiveUserLocale());
รีเทิร์น
String
— สตริงที่แสดงการตั้งค่าภาษาของผู้ใช้
getEffectiveUser()
รับข้อมูลเกี่ยวกับผู้ใช้ที่มีสิทธิ์ให้สคริปต์ทํางาน หากสคริปต์เป็นเว็บแอปที่ตั้งค่าเป็น "ดำเนินการในฐานะฉัน" (นักพัฒนาแอป) ระบบจะแสดงบัญชีผู้ใช้ของนักพัฒนาแอป หากสคริปต์ทํางานภายใต้ทริกเกอร์ที่ติดตั้งได้ ระบบจะแสดงบัญชีของผู้ใช้ที่สร้างทริกเกอร์ ในสถานการณ์อื่นๆ ส่วนใหญ่ การดำเนินการนี้จะแสดงผลบัญชีเดียวกับ getActiveUser()
// Log the email address of the user under whose authority the script is
// running.
const email = Session.getEffectiveUser().getEmail();
Logger.log(email);
รีเทิร์น
User
— ผู้ใช้ที่มีสิทธิ์เรียกใช้สคริปต์
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับอนุญาตด้วยขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/userinfo.email
getScriptTimeZone()
รับเขตเวลาของสคริปต์ สคริปต์ใหม่จะใช้เขตเวลาของเจ้าของเป็นค่าเริ่มต้น แต่คุณเปลี่ยนเขตเวลาของสคริปต์ได้โดยคลิกไฟล์ > พร็อพเพอร์ตี้ของโปรเจ็กต์ในเครื่องมือแก้ไขสคริปต์ โปรดทราบว่าสเปรดชีตมีเขตเวลาที่แยกต่างหาก ซึ่งคุณเปลี่ยนได้โดยคลิกไฟล์ > การตั้งค่าสเปรดชีตใน Google ชีต เขตเวลาของสเปรดชีตที่แตกต่างจากเขตเวลาของสคริปต์มักเป็นแหล่งที่มาของข้อบกพร่องของสคริปต์
// Log the time zone of the script.
const timeZone = Session.getScriptTimeZone();
Logger.log(timeZone);
รีเทิร์น
String
— เขตเวลาของสคริปต์
getTemporaryActiveUserKey()
รับคีย์ชั่วคราวที่ไม่ซ้ำกันสำหรับผู้ใช้ที่ใช้งานอยู่ แต่ไม่เปิดเผยตัวตนของผู้ใช้
คีย์ชั่วคราวจะเปลี่ยนทุก 30 วันและคีย์ดังกล่าวจะซ้ำกันไม่ได้สำหรับสคริปต์แต่ละรายการ
// Log the temporary key of the person running the script.
Logger.log(Session.getTemporaryActiveUserKey());
รีเทิร์น
String
— คีย์ผู้ใช้ที่ใช้งานอยู่ชั่วคราว
วิธีการที่เลิกใช้งาน
getTimeZone()
เลิกใช้งานแล้ว ฟังก์ชันนี้เลิกใช้งานแล้วและไม่ควรนำมาใช้ในสคริปต์ใหม่
รับเขตเวลาของสคริปต์ สคริปต์ใหม่จะใช้เขตเวลาของเจ้าของเป็นค่าเริ่มต้น แต่คุณเปลี่ยนเขตเวลาของสคริปต์ได้โดยคลิกไฟล์ > พร็อพเพอร์ตี้ของโปรเจ็กต์ในเครื่องมือแก้ไขสคริปต์ โปรดทราบว่าสเปรดชีตมีเขตเวลาที่แยกต่างหาก ซึ่งคุณเปลี่ยนได้โดยคลิกไฟล์ > การตั้งค่าสเปรดชีตใน Google ชีต เขตเวลาของสเปรดชีตที่แตกต่างจากเขตเวลาของสคริปต์มักเป็นแหล่งที่มาของข้อบกพร่องของสคริปต์
// Log the time zone of the script.
const timeZone = Session.getTimeZone();
Logger.log(timeZone);
รีเทิร์น
String
— เขตเวลาของสคริปต์
getUser()
เลิกใช้งานแล้ว ฟังก์ชันนี้เลิกใช้งานแล้วและไม่ควรนำมาใช้ในสคริปต์ใหม่
รับข้อมูลเกี่ยวกับผู้ใช้ปัจจุบัน
รีเทิร์น
User
— ผู้ใช้ที่ลงชื่อเข้าใช้อยู่ในปัจจุบัน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับอนุญาตด้วยขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/userinfo.email
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-26 UTC
[null,null,["อัปเดตล่าสุด 2025-07-26 UTC"],[[["\u003cp\u003eThe \u003ccode\u003eSession\u003c/code\u003e class in Apps Script allows you to access information about the current user and the script's environment, such as the user's locale and the script's time zone.\u003c/p\u003e\n"],["\u003cp\u003eYou can retrieve information about the active user, effective user, script time zone, and a temporary active user key using methods like \u003ccode\u003egetActiveUser()\u003c/code\u003e, \u003ccode\u003egetEffectiveUser()\u003c/code\u003e, \u003ccode\u003egetScriptTimeZone()\u003c/code\u003e, and \u003ccode\u003egetTemporaryActiveUserKey()\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eSome methods like \u003ccode\u003egetTimeZone()\u003c/code\u003e and \u003ccode\u003egetUser()\u003c/code\u003e are deprecated and should be replaced with their updated counterparts such as \u003ccode\u003egetScriptTimeZone()\u003c/code\u003e and \u003ccode\u003egetActiveUser()\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eWhile the user's email address can be accessed in certain circumstances using \u003ccode\u003egetActiveUser().getEmail()\u003c/code\u003e or \u003ccode\u003egetEffectiveUser().getEmail()\u003c/code\u003e, it may not be available if security policies restrict access or in contexts without explicit user authorization.\u003c/p\u003e\n"],["\u003cp\u003eAccessing user information may require specific authorization scopes, like \u003ccode\u003ehttps://www.googleapis.com/auth/userinfo.email\u003c/code\u003e, to be granted to the script.\u003c/p\u003e\n"]]],[],null,["# Class Session\n\nSession\n\nThe Session class provides access to session information, such as the user's email address (in\nsome circumstances) and language setting. \n\n### Methods\n\n| Method | Return type | Brief description |\n|-------------------------------------------------------------|------------------------------------------|-----------------------------------------------------------------------------------------------|\n| [getActiveUser()](#getActiveUser()) | [User](/apps-script/reference/base/user) | Gets information about the current user. |\n| [getActiveUserLocale()](#getActiveUserLocale()) | `String` | Gets the language setting of the current user as a string---for example, `en` for English. |\n| [getEffectiveUser()](#getEffectiveUser()) | [User](/apps-script/reference/base/user) | Gets information about the user under whose authority the script is running. |\n| [getScriptTimeZone()](#getScriptTimeZone()) | `String` | Gets the time zone of the script. |\n| [getTemporaryActiveUserKey()](#getTemporaryActiveUserKey()) | `String` | Gets a temporary key that is unique to the active user but does not reveal the user identity. |\n\n### Deprecated methods\n\n| Method | Return type | Brief description |\n|---------------------------------|------------------------------------------|------------------------------------------|\n| [getTimeZone()](#getTimeZone()) | `String` | Gets the time zone of the script. |\n| [getUser()](#getUser()) | [User](/apps-script/reference/base/user) | Gets information about the current user. |\n\nDetailed documentation\n----------------------\n\n### `get``Active``User()`\n\nGets information about the current user. If security policies do not allow access to the user's\nidentity, [User.getEmail()](/apps-script/reference/base/user#getEmail()) returns a blank string. The circumstances in which the\nemail address is available vary: for example, the user's email address is not available in any\ncontext that allows a script to run without that user's authorization, like a simple `on``Open(e)` or `on``Edit(e)` trigger, a custom function in Google Sheets, or a web app\ndeployed to \"execute as me\" (that is, authorized by the developer instead of the user).\nHowever, these restrictions generally do not apply if the developer runs the script themselves\nor belongs to the same Google Workspace domain as the user.\n\n```javascript\n// Log the email address of the person running the script.\nconst email = Session.getActiveUser().getEmail();\nLogger.log(email);\n```\n\n#### Return\n\n\n[User](/apps-script/reference/base/user) --- the current user\n\n#### Authorization\n\nScripts that use this method require authorization with one or more of the following [scopes](/apps-script/concepts/scopes#setting_explicit_scopes):\n\n- `https://www.googleapis.com/auth/userinfo.email`\n\n*** ** * ** ***\n\n### `get``Active``User``Locale()`\n\nGets the language setting of the current user as a string---for example, `en` for English.\n\n```javascript\n// Log the language setting of the person running the script.\nLogger.log(Session.getActiveUserLocale());\n```\n\n#### Return\n\n\n`String` --- a string that represents the user's language setting\n\n*** ** * ** ***\n\n### `get``Effective``User()`\n\nGets information about the user under whose authority the script is running. If the script is a\nweb app set to \"execute as me\" (the developer), this returns the developer's user account. If\nthe script is running under an [installable\ntrigger](/apps-script/understanding_triggers), this returns the account of the user who created the trigger. In most other\nscenarios, this returns the same account as [getActiveUser()](#getActiveUser()).\n\n```javascript\n// Log the email address of the user under whose authority the script is\n// running.\nconst email = Session.getEffectiveUser().getEmail();\nLogger.log(email);\n```\n\n#### Return\n\n\n[User](/apps-script/reference/base/user) --- the user under whose authority the script is running\n\n#### Authorization\n\nScripts that use this method require authorization with one or more of the following [scopes](/apps-script/concepts/scopes#setting_explicit_scopes):\n\n- `https://www.googleapis.com/auth/userinfo.email`\n\n*** ** * ** ***\n\n### `get``Script``Time``Zone()`\n\nGets the time zone of the script. New scripts default to the owner's time zone, but the\nscript's time zone can be changed by clicking **File \\\u003e Project properties** in the script\neditor. Note that spreadsheets have a separate time zone, which can be changed by clicking\n**File \\\u003e Spreadsheet settings** in Google Sheets. Spreadsheet time zones that differ from\nthe script time zone are a frequent source of scripting bugs.\n\n```javascript\n// Log the time zone of the script.\nconst timeZone = Session.getScriptTimeZone();\nLogger.log(timeZone);\n```\n\n#### Return\n\n\n`String` --- the time zone of the script\n\n*** ** * ** ***\n\n### `get``Temporary``Active``User``Key()`\n\nGets a temporary key that is unique to the active user but does not reveal the user identity.\nThe temporary key rotates every 30 days and is unique to the script.\n\n```javascript\n// Log the temporary key of the person running the script.\nLogger.log(Session.getTemporaryActiveUserKey());\n```\n\n#### Return\n\n\n`String` --- the temporary active user key\n\nDeprecated methods\n------------------\n\n### `get``Time``Zone()`\n\n\n**Deprecated.** This function is deprecated and should not be used in new scripts.\n\nGets the time zone of the script. New scripts default to the owner's time zone, but the\nscript's time zone can be changed by clicking **File \\\u003e Project properties** in the script\neditor. Note that spreadsheets have a separate time zone, which can be changed by clicking\n**File \\\u003e Spreadsheet settings** in Google Sheets. Spreadsheet time zones that differ from\nthe script time zone are a frequent source of scripting bugs.\n\n```javascript\n// Log the time zone of the script.\nconst timeZone = Session.getTimeZone();\nLogger.log(timeZone);\n```\n\n#### Return\n\n\n`String` --- the time zone of the script\n\n*** ** * ** ***\n\n### `get``User()`\n\n\n**Deprecated.** This function is deprecated and should not be used in new scripts.\n\nGets information about the current user.\n\n#### Return\n\n\n[User](/apps-script/reference/base/user) --- the currently signed in user\n\n#### Authorization\n\nScripts that use this method require authorization with one or more of the following [scopes](/apps-script/concepts/scopes#setting_explicit_scopes):\n\n- `https://www.googleapis.com/auth/userinfo.email`"]]