0

全部,

我使用与以下类似的脚本从谷歌电子表格发送电子邮件:

// Sends an email when .......
function emailInfoRequired(row) { 
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var subject = "Info Needed: " + sheet.getRange("F19").getValues();
  var recipients = "email@email.com"
  var message = "<HTML><BODY>" 
    + "<P>" + sheet.getRange("K1").getValues()
    + "<BR>" + sheet.getRange("K2").getValues()
    + "</HTML></BODY>"; 
  MailApp.sendEmail(recipients, subject, "", {htmlBody: message});
}

我想做的不是使用单个单元格,而是要包含一个范围,例如: sheet.getRange("K2:N2").getValues()

问题是,当我这样做时,电子邮件正文在该范围内的每个单元格之间都有一个逗号,这使得电子邮件没有任何意义而且看起来很丑。我尝试将单元格格式化为纯文本,但这没有帮助。有没有办法发送范围的电子邮件而不显示逗号?

4

2 回答 2

1

您甚至可以通过使用这样的 html 表使其更优雅:

// Sends an email when .......
function emailInfoRequired(row) { 
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var subject = "Info Needed: " + sheet.getRange("F19").getValues();
  var recipients = "testmail@gmail.com"
  var data = sheet.getRange('A2:C25').getValues();// define your range here
  var message = '<HTML><BODY><table style="background-color:lightblue;border-collapse:collapse;" border = 1 cellpadding = 5><tr>';// change eventually the color here
  for (var row=0;row<data.length;++row){
    for(var col = 0;col<data[0].length;++col){
      message += '<td>'+data[row][col]+'</td>';
    }
    message += '</tr><tr>';
  }
    message += '</tr></table></body></HTML>';
   MailApp.sendEmail(recipients, subject, "", {htmlBody: message});
}

插图示例:

在此处输入图像描述

于 2013-08-22T15:01:43.807 回答
1

Range.getValues()返回一个 2D 数组,并在您的电子邮件中打印该 2D 数组的字符串表示形式。根据您希望在电子邮件中显示的方式,您必须单独处理每个元素。就像是...

function emailInfoRequired(row) { 
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var subject = "Info Needed: " + sheet.getRange("F19").getValues();
  var recipients = "email@email.com"
  var values = sheet.getRange("K2:N2").getValues();
  var message = "<HTML><BODY>" ;
  for (var row = 0 ; row < values.length ; row++){
    message +=  "<P>" ;
    for (var col= 0 ; col < values[row].length ; col++){
      message +=  "<BR>" + values[row][col];
    }
  }
  message  += "</HTML></BODY>"; 
  MailApp.sendEmail(recipients, subject, "", {htmlBody: message});
}
于 2013-08-22T14:22:30.657 回答