Google表格发送范围作为电子邮件

时间:2018-03-03 00:30:38

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

我正在尝试在电子邮件中发送特定范围作为Pdf附件或HTML表格,单元格a2将包含电子邮件,消息应该在C7:I25范围内,这是我到目前为止所拥有的,我收到错误,找不到收件人,能帮助我吗?

function correoe() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 2;  
  var numRows = 2;   
  var dataRange = sheet.getRange(startRow, 1, numRows, 5)
  var data = dataRange.getValues();
  for (i in data) {
    var row = data[i];
    var emailAddress = row[0];  
    var message = sheet.getRange ("C7:I25");
    var subject = "Su Cotizacion";
    MailApp.sendEmail(emailAddress, subject, message);
  }
}

1 个答案:

答案 0 :(得分:1)

你需要一些函数来从单元格中获取html文本。

var data = sheet.getRange ("C7:I25").getValues();
var message = getTable(data);

函数getTable可能如下所示:

  function getTable(data) {
    var result = ["<table border=1'>"];
    var ll = data[0].length;
    var row = 0;
    for(var i = 0, l = data.length; i < l; i++) {
        row = data[i];
        result.push("<tr>");
        for(var ii = 0; ii < ll; ii++){
            result.push('<td>' + row[ii] + '</td>');
        }
        result.push("</tr>");
    }
    result.push("</table>");
    return result.join('\n');
  }

测试

虚拟数据

[["row 1, cell 1", "row 1, cell 2"], 
 ["row 2, cell 1", "row 2, cell 2"]];

虚拟HTML文本输出

<table border=1'>
<tr>
<td>row 1, cell 1</td>
<td>row 1, cell 2</td>
</tr>
<tr>
<td>row 2, cell 1</td>
<td>row 2, cell 2</td>
</tr>
</table

用法

需要使用此语法发送电子邮件:

 MailApp.sendEmail({
     to: emailAddress,
     subject: subject,
     htmlBody: message
 });