Google Apps脚本无法通过电子表格发送电子邮件 [英] Google Apps Script send email through spreadsheet not working

查看:99
本文介绍了Google Apps脚本无法通过电子表格发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对某些Google脚本问题有疑问.基本上,我的目标是检查脚本,以查看客户的案件是否已解决,然后向他们发送电子邮件,说明问题已解决.我已经确定了何时发送电子邮件的逻辑,但是每次尝试将其实施到电子表格中时,都会收到错误消息:

I have a problem with some Google Script stuff. Basically, my goal is to have the script check to see if a client's case was resolved and then send an email to them that the issue has been resolved. I've gotten the logic done on when to send an email, but every time I try and implement it into the spreadsheet, I get the error:

Error You do not have permission to call MailApp.sendEmail. Required permissions: https://www.googleapis.com/auth/script.send_mail (line 8).

Error You do not have permission to call MailApp.sendEmail. Required permissions: https://www.googleapis.com/auth/script.send_mail (line 8).

我有一个简单的函数来测试它的功能,并且在脚本编辑器中运行时,它可以正常运行,但不能在电子表格中运行.这是我的示例函数:

I've got a simple function to test the functionality of it, and when run in the script editor it works fine, but not on the spreadsheet. Here is my sample function:

function myFunction(row) {
var sheet = SpreadsheetApp.getActiveSheet();
var rng = sheet.getRange(row, 1, 1, 2);
var ara = rng.getValues();
var email = ara[0][0];
MailApp.sendEmail(email, "TEST", "This is a test of sendEmail().");
return "Email sent.";}

推荐答案

根据:

如果您的自定义函数抛出错误消息You do not have permission to call X service.,则该服务需要用户授权,因此不能在自定义函数中使用.

If your custom function throws the error message You do not have permission to call X service., the service requires user authorization and thus cannot be used in a custom function.

要使用除上面列出的服务以外的其他服务,请创建一个运行Apps脚本功能的自定义菜单,而不是编写一个自定义功能.通过菜单触发的功能将在必要时询问用户授权,因此可以使用所有Apps Script服务.

To use a service other than those listed above, create a custom menu that runs an Apps Script function instead of writing a custom function. A function that is triggered from a menu will ask the user for authorization if necessary and can consequently use all Apps Script services.

方法1

基本上,您可以使用以下命令复制上述两个函数的所需行为:

Method 1

Basically, you can replicate the wanted behavior of the two functions above with this:

function SendEmail() {
   var message = "This is your response";
   var subject = "You have feed back in the parking lot";
   var ss = SpreadsheetApp.getActiveSheet();
   var textrange = ss.getRange("F2");
   var emailAddress = ss.getRange("B2").getValue();
   if (textrange.isBlank() == false)
      MailApp.sendEmail(emailAddress, subject, message);

}

并且为了触发该功能的执行,您可以使用应用程序脚本触发器,然后选择最适合您的用例的一个.

And in order to trigger the execution of this function, you can make use of Apps Script triggers and choose one which is the most convenient for your use-case.

您还可以创建一个自定义菜单,并可以选择触发上述功能.您只需要添加以下内容:

You can also create a custom menu and with the option of triggering the above function. You only need to add this:

function onOpen() {
  var ui = SpreadsheetApp.getUi();
  ui.createMenu("My Menu")
      .addItem("Send Email", "SendEmail")
      .addToUi();
}

这就是电子表格上的样子:

And this is how it will look like on the Spreadsheet:

应用程序脚本范围类-isBlank();

应用程序脚本自定义菜单" ;

应用脚本触发器.

这篇关于Google Apps脚本无法通过电子表格发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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