【问题标题】:TypeError: Cannot read property "range" from undefined [duplicate]TypeError:无法从未定义中读取属性“范围”[重复]
【发布时间】:2015-05-08 18:50:23
【问题描述】:

我已经编写了这个谷歌应用脚​​本。 目的:每次我编辑“位置更新”脚本时都应该向“经纪人电子邮件”发送一封电子邮件。 Here is Link to spread sheet

function onEdit(e) {
  var activateSheet = SpreadsheetApp.getActiveSpreadsheet();
  SpreadsheetApp.setActiveSheet(activateSheet.getSheetByName('Responses'));
  var sheet = SpreadsheetApp.getActiveSheet();
  var range = e.range;
  var row = e.range.getRowIndex();//Gets the row the change was made to.  

  var timestamp = sheet.getRange(row, 1).getValues(); //Gets the email
  var load = sheet.getRange(row, 2).getValues(); //Gets the email
  var email = sheet.getRange(row, 3).getValues(); //Gets the email
  var location = sheet.getRange(row, 4).getValues(); //Gets the email
  var template = HtmlService.createTemplateFromFile("template");
 template.load = load;
 template.location =location;


  MailApp.sendEmail(email,"Hi","this is my email",{htmlBody: template.evaluate().getContent()});
   }
<html>
<h1><b>This is a Check-In call for load number: <?= load ?></b></h1>
<p>
<?= location ?>
</p>
<p>
Please do not reply this email, we don't read these emails. If you have any questions/concers please <a href="google.com">Click Here</a>. We will get back to as soon as we can.
</p>
</html>

【问题讨论】:

标签: google-apps-script google-sheets triggers


【解决方案1】:

错误:

无法从未定义中读取属性“范围”

表示从事件对象e获取range失败。

如果您从脚本编辑器中运行代码,您将收到范围错误。对于简单的onEdit() 触发器,您只能通过实际编辑单元格来测试代码。 e 事件变量不会分配任何东西,除非您实际编辑单元格。当事件(单元格编辑)发生时,e 变量将被分配一个事件对象。

函数名onEdit() 是保留的函数名。为了在编辑单元格时运行onEdit() 函数,您需要做的就是创建一个具有该名称的函数,而不是通过设置过程将事件分配给函数名称。 onEdit() 是一个简单的触发器。并且需要设置“可安装”触发器。

我运行了你的代码,没有出现任何范围错误,如果我通过编辑单元格来触发它。

我收到一条错误消息,指出我无权发送电子邮件。这是因为触发器是“简单”触发器。您可能需要创建一个可安装的触发器才能获得发送电子邮件的权限。

【讨论】:

    【解决方案2】:

    试试这个脚本(也发布在谷歌文档帮助论坛上)

    function sendMail(e) {
    
    var sheet = e.source.getActiveSheet();
    if (sheet.getName() !== 'Responses' || e.range.columnStart !== 6 || e.range.rowStart < 2) return;
    var values = e.range.offset(0, -5, 1, 6)
       .getValues()[0];
    var htmlBody = '<body>' + 'Hi<br>'
    + '<h1><b>This is a Check-In call for load number:' + values[3] + '</b></h1>'
    + '<p>' + values[5] + '<\p>'
    + '<p>' + 'Please do not reply this email, we don\'t read these emails. If you have any questions/concers please <a href="google.com">Click Here</a>.'
    + 'We will get back to as soon as we can.</p>'
    + '<\body>'
    MailApp.sendEmail(values[4], 'subject', "", {
       htmlBody: htmlBody
    });
    }
    

    设置一个可安装的 onEdit 触发器,看看它是否有效?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-21
      • 2021-03-18
      • 2017-03-10
      • 2021-10-12
      • 1970-01-01
      • 2014-09-07
      • 2017-12-15
      • 1970-01-01
      相关资源
      最近更新 更多