使用Google App脚本从不同帐户发送电子邮件 [英] Sending Emails from different accounts with Google App Script

查看:187
本文介绍了使用Google App脚本从不同帐户发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用



我还要求PersonA& PersonB在他们的账户下做同样的事情,并确保他们已经触发了脚本至少一次。例如:





我的问题是,在我做了上述所有这些事情,无论谁在谷歌电子表格中改变任何东西,电子邮件总是从PersonB的帐户发出(我最好的猜测是我搞砸了一些东西,而PersonB是最后一个触发了这个脚本的人)。



对于我,PersonA和PersonB,还是我需要更改的其他项目,项目版本是否需要不同?任何帮助都将不胜感激!

解决方案

首先,您的项目版本不需要针对每个用户有所不同。



我假设您使用On Change事件,而不是Web应用程序。如果是这种情况,您应该完全禁用WebApp,因为它不是必需的。



当PersonA创建On Change触发器时,任何用户(例如PersonA或PersonB)编辑电子表格。当它被触发时,代码将作为PersonA执行,因为PersonA创建了触发器。因此,电子邮件将从PersonA的帐户发送。

针对您描述的场景,PersonA和PersonB都创建了OnChange触发器并授权脚本。我希望电子邮件是从两个已创建触发器的帐户发送的。



要仅发送一封电子邮件,从编辑过的帐户开始,我认为您需要进行检查以确定进行编辑的用户是否与用户匹配脚本在谁的权限下运行。我从来没有这样做过,文档没有真正清楚它是否可能。尝试检查包含在Change事件中的User对象(例如e.user),并查看该电子邮件地址是否反映了进行编辑的不同用户。

>

如果是,您可以将其与有效用户进行比较,如果匹配则发送电子邮件。



https:// developers.google.com/apps-script/reference/base/user

https://developers.google.com/apps-script/reference/base/session#getEffectiveUser()



最后,在电子邮件主题中包含编辑人员的地址可能会更简单,并且只需从一个帐户发送所有电子邮件即可。这样您就知道是谁进行了编辑,但不需要让每个用户都创建一个触发器并执行所有额外的检查。


I have generated the codes with app script in order to send emails automatically if users changed some values in google spreadsheet, which is working fine.

Now I am sharing my google spreadsheet with the app script to two more person (e.g: PersonA, PersonB). Thus, I want whoever changes anything on google spreadsheet will be sending an email under his/her own account. E.g: [All of us needs to edit within the google spreadsheet itself, without going to the "current web app url", (thanks Cameron Roberts for the clarification] if PersonA changes anything, then an email will be sent out under PersonA's account, and an email will be sent out under PersonB's account if PersonB changes anything.

I did something based on the advice from user2970721 and Cameron Roberts. I adjusted "Deploy web App" as "User accessing the web app". e.g:

I also asked PersonA & PersonB to do the same under their accounts and made sure they have triggered the script at least once. e.g:

My issue is that after I have done all these mentioned above, no matter who changes anything on google spreadsheet, emails were always sent out from PersonB's account (my best guess is that I messed up something and PersonB was the last one who triggered the script).

Does the "Project version" need to be different for me, PersonA, and PersonB, or anything else I need to change? Any help would be greatly appreciated!

解决方案

First, Your project versions do not need to be different for each user.

I'm assuming you are using the On Change event, rather than a web-app. If that's the case, you should disable the WebApp entirely, as it's not needed.

When PersonA creates an On Change trigger, that will be triggered anytime any user (eg PersonA or PersonB) edits the spreadsheet. When it is triggered the code will execute as PersonA , because PersonA created the trigger. As a result, the email will be sent from PersonA's account.

For the scenario you describe, where both PersonA and PersonB have created OnChange triggers and authorised the script. I would expect emails to be sent from both accounts that have created triggers.

To send only one email, from the account that did the edit, I think you would need to do a check to determine if the user who did the edit matches the user under who's authority the script is running. I've never done this before and the docs don't really make it clear if it's possible.

Try checking the User object (Eg e.user) included with the Change event, and see if that email address is reflecting the different users making the edits.

If it is, you can compare it with the effective user and send the email if they match.

https://developers.google.com/apps-script/reference/base/user

https://developers.google.com/apps-script/reference/base/session#getEffectiveUser()

Finally, it might be simpler to just include the address of the person making the edit in your email subject, and just have all the emails send from one account. That way you know who made the edit, but don't need to have every user create a trigger and do all the extra checking.

这篇关于使用Google App脚本从不同帐户发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆