有没有办法在必须授权 Google 应用程序脚本的情况下获取当前用户的名称
Is there a way to get the current user's name with having to Authorise the Google App Script
我创建了一个 Google 文档模板。该模板包含 运行 从 OnOpen() 触发器到 auto-populate 一些基本信息(日期、分类、标题、版本、作者)的代码。我请求提示以获得这些问题的答案。问题是这样的:要获取作者姓名,除了使用此代码我别无他法:
var email = Session.getActiveUser().getEmail()
然后我从电子邮件地址中提取并格式化名称。
问题是 getEmail() 函数需要用户 运行 将代码授权 session。
如果用户只需执行一次,这不会有问题,但由于这是一个文档模板,任何基于该模板的新文档都需要 re-authorisation 代码。
是否有更好的方法获取作者姓名?
很遗憾没有。请参阅 https://developers.google.com/apps-script/reference/base/session#getActiveUser() 了解更多信息。
Gets information about the current user. If security policies do not allow access to the user's identity, User.getEmail()
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.
我创建了一个 Google 文档模板。该模板包含 运行 从 OnOpen() 触发器到 auto-populate 一些基本信息(日期、分类、标题、版本、作者)的代码。我请求提示以获得这些问题的答案。问题是这样的:要获取作者姓名,除了使用此代码我别无他法:
var email = Session.getActiveUser().getEmail()
然后我从电子邮件地址中提取并格式化名称。
问题是 getEmail() 函数需要用户 运行 将代码授权 session。
如果用户只需执行一次,这不会有问题,但由于这是一个文档模板,任何基于该模板的新文档都需要 re-authorisation 代码。
是否有更好的方法获取作者姓名?
很遗憾没有。请参阅 https://developers.google.com/apps-script/reference/base/session#getActiveUser() 了解更多信息。
Gets information about the current user. If security policies do not allow access to the user's identity,
User.getEmail()
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 simpleonOpen(e)
oronEdit(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.