我的代码现在看起来如下:
GmailApp.sendEmail(address, subject, body, {htmlBody : body});我的参考问题: 在使用应用程序脚本创建的电子邮件中添加签名的语法错误
我的问题:它显示发件人为alex.ken@..。
我想要的东西:,我希望脚本以用户的名字命名。所有用户都在Google设置中为他们的电子邮件地址设置了不同的名称。
PS -请注意,我有多个用户使用工作表/宏,所以我不能在name参数中有fix值。
请帮帮忙。
编辑1=Solution:
var user = Session.getActiveUser().getUsername(); //gives username
var firstpart = user.split('.')[0]; //gives first part of username
var uppercase = firstpart.charAt(0).toUpperCase() + firstpart.substring(1); //makes first letter upper case
var display = uppercase + " with Ban Company";
.
.
.
.
GmailApp.sendEmail(address, subject, body, {htmlBody : body + signature,cc: "test@gmail.com",name:display});发布于 2022-03-10 19:32:23
您的解决方案适用于name.surname@...格式的电子邮件,但是如果用户有类似于itadmin@...、昵称或任何其他电子邮件变体的东西,您可能会遇到问题。
另一种解决方案是使用People API高级服务检索当前用户的配置文件名,因为Apps脚本的Session.getActiveUser()只包含用户的电子邮件,还有一些场景在那里不起作用。
这包括更多的步骤,它们必须授权People API,但是您可以获得任何用户的配置文件名称。

{
...
"oauthScopes": [
"https://www.googleapis.com/auth/userinfo.profile"
],
...
}//This will contain a user's names
function getCurrentUser(){
var currentuser = People.People.get("people/me", {"personFields":"names"})
return currentuser.names[0]
}
var firstname = getCurrentUser().givenName //gets the user's first name
var fullname = getCurrentUser().displayName // gets the user's full profile name
//You can then add either variable as the name
GmailApp.sendEmail(address, subject, body, {htmlBody : body + signature,cc: "test@gmail.com",name:firstname});如果您需要它,您还可以检索更多的不仅仅是名称,还可以检索其他用户配置文件信息。查看文档这里。
发布于 2022-03-10 18:06:41
使用
const name = 'Alex from Ban' // replace the string by someway to assign the value that you want to use.
GmailApp.sendEmail(address, subject, body, {htmlBody : body, name: name });参考文献
https://stackoverflow.com/questions/71428113
复制相似问题