MailApp.sendEmail 错误消息 - “无权调用 sendEmail" [英] MailApp.sendEmail Error Message - "do not have permission to call sendEmail"

查看:42
本文介绍了MailApp.sendEmail 错误消息 - “无权调用 sendEmail"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我更改电子表格中的某些内容时,onEdit() 触发器会运行,并且我可以看到我放入代码中的所有 msgbox.

When I change something in my spreadsheet, the onEdit() trigger runs, and I can see all the msgbox's that I put in my code.

我的函数停在这一行

MailApp.sendEmail(emailAddress, subject, message);

我从来没有看到消息电子邮件已发送!",并且在执行转录中出现错误:

I never see the message 'Email sent!', and get an error in the EXECUTION TRANSCRIPT:

您没有权限调用 sendEmail

You do not have permission to call sendEmail

如果我直接在脚本编辑器中运行脚本,一切正常...

If I run the script directly in the script editor, everything works fine...

这是我的代码:

function onEdit() {
      var sheet = SpreadsheetApp.getActiveSheet();
      var sheetname = sheet.getName()
      var AcCellRange = sheet.getActiveCell()
      var AcCol = AcCellRange.getColumn()
      var AcRow = AcCellRange.getRow()

      if (sheetname=="Questions/Réponses") {
        //Browser.msgBox(AcCol+' / '+AcRow)
       //liste d'instructions
        //Boucle si les colonne sont comprise dans le range
        if ((AcCol==3) || ((AcCol==7))){
          //Browser.msgBox(AcCol)
          if (AcRow > 7){
            //Browser.msgBox(AcRow)
            sendEmails()
          }
        }
      }
      else
      {}
    }    
function sendEmails() {
      Browser.msgBox('SendEmails')
      var spreadsheet = SpreadsheetApp.getActive();
      var sheet = spreadsheet.getSheetByName('ListCourriel');
      Browser.msgBox('SendEmails2')
      var sheetDonnee = spreadsheet.getSheetByName('Questions/Réponses');
      var RangeProjet = sheetDonnee.getRange(1, 3)
      var NoProjet = RangeProjet.getValue()
      var RangeProjet = sheetDonnee.getRange(4, 3)
      var ProjName = RangeProjet.getValue()
      Browser.msgBox('SendEmails3')
      var startRow = 2;  // First row of data to process
      var LastRows = sheet.getRange(1,4)
      var numRows = LastRows.getValue();   // Number of rows to process
      // Fetch the range of cells A2:B3
      var dataRange = sheet.getRange(startRow, 1, numRows, 2)
      // Fetch values for each row in the Range.
      var data = dataRange.getValues();
      Browser.msgBox('SendEmails4')
      //Permet d'aller cherche les info de la ligne active
      var ActiveCellRange = sheetDonnee.getActiveCell()
      var ActiveRows = ActiveCellRange.getRow()
      var NoLigne = sheetDonnee.getRange(ActiveRows,1)
      var sDep = sheetDonnee.getRange(ActiveRows,2)
      var sDate = sheetDonnee.getRange(ActiveRows,4)
      var sInitiale = sheetDonnee.getRange(ActiveRows,5)
      var sQuestion = sheetDonnee.getRange(ActiveRows,3)
      Browser.msgBox('SendEmails5')
      var rDate = sheetDonnee.getRange(ActiveRows,9)
      var rInitiale = sheetDonnee.getRange(ActiveRows,10)
      var rReponse = sheetDonnee.getRange(ActiveRows,7)

      Browser.msgBox('SendEmails6')
      var subject = 'Modif. Question/Réponse - Projet: ('+NoProjet+') '+ProjName;
      var message = "No Ligne : "+NoLigne.getValue()+String.fromCharCode(10)+String.fromCharCode(10)+"Reponsable : "+sInitiale.getValue()+String.fromCharCode(10)+"Date : "+sDate.getValue()+String.fromCharCode(10)+"Question : "+String.fromCharCode(10)+sQuestion.getValue()+String.fromCharCode(10)+String.fromCharCode(10)+"************************************"+String.fromCharCode(10)+String.fromCharCode(10)+"Reponsable : "+rInitiale.getValue()+String.fromCharCode(10)+"Date : "+rDate.getValue()+String.fromCharCode(10)+"Réponse : "+String.fromCharCode(10)+rReponse.getValue()
      //Browser.msgbox(subject)

      Browser.msgBox('SendEmails7')
      for (i in data) {
        Browser.msgBox('SendEmails8')
        var row = data[i];
        var emailAddress = row[0];  // First column
        Browser.msgBox('SendEmails9')
        MailApp.sendEmail(emailAddress, subject, message);
        Browser.msgBox('Email sent')
      }
    }

推荐答案

运行 SIMPLE onEdit() 触发器时权限不同.相比之下,有一个 INSTALLABLE 触发器.以下是简单触发器限制的文档:

The permissions are different when running a SIMPLE onEdit() trigger. By contrast, there is an INSTALLABLE trigger. Here is the documentation for simple trigger restrictions:

Google 文档 - 触发器 - 限制

文档说明:

他们(一个简单的触发器)无法访问需要授权的服务.例如,一个简单的触发器无法发送电子邮件,因为 Gmail 服务需要授权

They (a SIMPLE trigger) cannot access services that require authorization. For example, a simple trigger cannot send an email because the Gmail service requires authorization

您需要设置一个可安装触发器才能发送电子邮件.

You need to set up an installable trigger to be able to send the email.

编辑菜单中,选择当前项目触发器.

将您的函数命名为与 onEdit 不同的名称.

Name your function something different than onEdit.

这篇关于MailApp.sendEmail 错误消息 - “无权调用 sendEmail"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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