从Google电子表格发送范围作为电子邮件(格式问题)

时间:2014-02-10 14:26:14

标签: google-apps-script google-sheets google-spreadsheet-api

以下代码会在第2列和第3列中发送数据电子邮件,但电子邮件中的结果不会低于另一个。

Code.gs
function email() {

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var bulk = ss.getSheetByName("Bulk");
  var lastrow = bulk.getLastRow();

  var data1 = bulk.getRange(1, 2, lastrow).getValues();
  var data2 = bulk.getRange(1, 3, lastrow).getValues();

  var onedata="", twodata="";

  for (var i =1; i < data1.length; i++) {
    if (onedata.length) {
              onedata += ", ";
    }
   onedata += data1[i] 
  }

  for (var i =1; i < data2.length; i++) {
    if (twodata.length) {
              twodata += ", ";
    }
   twodata += data2[i] 
  }

  var report = ""

  report += "<tr><td style='padding:5px'>" + onedata + "</td><td style='padding:5px'>" + twodata + "</td></tr>";


  report ="<table><tr><th'>First data</th><th>Second data</th></tr>" + report + "</table>";

  MailApp.sendEmail(Session.getActiveUser().getEmail(),
                    "Your report", report, {htmlBody: report});

  }

电子邮件中收到的结果

First data                  Second data
Apple, Orange, Grapes,      Red, Orange, Green, Yellow,
Banana, Chickoo             Grey

预期结果

First data  Second data
Apple       Red
Orange      Orange
Grapes      Green
Banana      Yellow
Chickoo     Grey

2 个答案:

答案 0 :(得分:1)

你在for循环中犯了一点错误。

这里你的代码稍作修改:

function email() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var bulk = ss.getSheetByName("Bulk");
  var lastrow = bulk.getLastRow();

  var data1 = bulk.getRange(1, 2, lastrow).getValues(); // fruit column
  var data2 = bulk.getRange(1, 3, lastrow).getValues(); // color column

  var report = ""
  var fruitTable ="";
  for(var i in data1){
    if(data1[i]!="") fruitTable += "<tr><td style='padding:5px'>" + data1[i] + "</td><td style='padding:5px'>" + data2[i] + "</td></tr>";
  }

  report ="<table><tr><th>First data</th><th>Second data</th></tr>" + fruitTable + "</table>";

  MailApp.sendEmail(Session.getActiveUser().getEmail(),
                    "Your report", report, {htmlBody: report});
}

我希望这能满足您的需求 哈罗德

答案 1 :(得分:1)

表构造有一些错误,下面是一个简单循环的代码,随意调整它你想要的方式...因为它增加了一些我用来使我的测试更容易的功能... (风格+条件)

function email() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var bulk = ss.getSheetByName("Bulk");
  var lastrow = bulk.getLastRow();
  var data = bulk.getRange(1, 2, lastrow,2).getValues();  
  var tableData = "<table border=1><th>First data</th><th>Second data</th>"

  for (var i =1; i < data.length; i++) {
    var d1=data[i][0]==''?'empty ':data[i][0];
    var d2=data[i][1]==''?'empty ':data[i][1];
    tableData+="<tr><td>"+d1+"</td><td>"+d2+"</td></tr>"
  }
  tableData+="</table>";

  MailApp.sendEmail(Session.getActiveUser().getEmail(),
                    "Your report", "html only", {htmlBody: tableData});
}

编辑:

利用哈罗德的答案,我添加了一些风格,使其更“性感”; - )

function email() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var bulk = ss.getSheetByName("Bulk");
  var lastrow = bulk.getLastRow();
  var data = bulk.getRange(1, 2, lastrow,2).getValues();  
  var tableData = '<TABLE style="border-collapse:collapse;"border = 1 cellpadding = 5 ><th style=color:blue>First data</th><th style=color:green>Second data</th>'

  for (var i =1; i < data.length; i++) {
    var d1=data[i][0]==''?'empty':data[i][0];
    var d2=data[i][1]==''?'empty':data[i][1];
    tableData+="<tr><td style=color:blue>"+d1+"</td><td style=color:green>"+d2+"</td></tr>"
  }
  tableData+="</table>";

  MailApp.sendEmail(Session.getActiveUser().getEmail(),
                    "Your report", "html only", {htmlBody: tableData});
}