简体   繁体   English

从Google电子表格发送电子邮件,省略逗号

[英]Send email from Google Spreadsheet omitting the commas

All, 所有,

I use a similar script to the below to send emails from a google spreadsheet: 我使用类似下面的脚本从谷歌电子表格发送电子邮件:

// 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});
}

What i would like to do is instead of using single cells,i want to include a range such as: sheet.getRange("K2:N2").getValues() 我想做的是不使用单个单元格,我想包括一个范围,例如:sheet.getRange(“K2:N2”)。getValues()

The problem is, when i do this, the email body has a comma between every cell within that range making the email not make any sense plus look ugly. 问题是,当我这样做时,电子邮件正文在该范围内的每个单元格之间都有一个逗号,使得电子邮件没有任何意义加上看起来很难看。 I tried formatting the cells as plain text but that didnt help. 我尝试将单元格格式化为纯文本,但没有帮助。 Is there a way to send the email of a range and not show the commas? 有没有办法发送范围的电子邮件而不显示逗号?

Range.getValues() returns a 2D array and a string representation of this 2D array is printed in your email. Range.getValues()返回一个2D数组,并在您的电子邮件中打印此2D数组的字符串表示形式。 Depending on how you want to show it in your email, you have to process each element individually. 根据您希望在电子邮件中显示的方式,您必须单独处理每个元素。 Something like... 就像是...

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});
}

You could even make it more elegant by using a html table like this : 您甚至可以通过使用这样的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});
}

Illustration example : 插图示例:

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM