如何在电子邮件的表格上安排数组值?

时间:2015-11-19 02:58:48

标签: javascript arrays email html-table

对于模糊的标题,我们深表歉意。我对编程很陌生,我在这里有这个代码:

 function onOpen() {
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('Confirm Order')
      .addItem('Send Email', 'email')
      .addToUi();  
}  

function email() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  var currentRow = sheet.getActiveRange().getRow();
  var product = sheet.getRange(2,5,1,11).getValues(); //header names
  var order1 = sheet.getRange(currentRow, 5, 1,11).getValues();
  var order2 = sheet.getRange(currentRow, 17, 1,11).getValues();
  var order3 = sheet.getRange(currentRow, 29, 1,11).getValues();
  var email = sheet.getRange(currentRow,2).getValues();

  var tab = ""
  var z ="";

  for(var i in product){
    if(product[i]!="") z += "<tr><td style='padding:5px'>" + product[i] + "</td><td style='padding:5px'>" + order1[i] + "</td><td style='padding:5px'>" + order2[i] + "</td><td style='padding:5px'>" + order3[i] + "</td></tr>";
  }

  tab = "<table><tr><th>Product Name</th><th>Order 1</th> <th>Order 2</th> <th>Order 3</th></tr>" + z + "</table>";

//  MailApp.sendEmail(Session.getActiveUser().getEmail(),"Your report", report, {htmlBody: report});
  MailApp.sendEmail(email,"Order Confirmation", tab, {htmlBody: tab});
}

以下代码会在电子表格中发送所选数据的电子邮件,但电子邮件中的结果不会低于另一个。例如,我希望它看起来像这样:

Product Name      order 1     order 2     order 3
prod A               2           2           1
prod B               3           1           4
Prod C               12          7           9

但最终看起来像这样:

Product Name           order 1     order 2     order 3
prod A,prod B,prod C    2,3,12      2,1,7       1,4,9

请告诉我我做错了什么。我真的很想学。

1 个答案:

答案 0 :(得分:1)

getValues返回一个二维数组。这就是product[0]显示为“prod A,prod B,prod C”的原因。这就是数组toString的功能。因此,您需要打印product[0][0]product[0][1]product[0][2]

for(var i in product) {
    for (var j in product[i]) {
        z += "<tr><td style='padding:5px'>" + product[i][j] + "</td><td style='padding:5px'>" + order1[i][j] + "</td><td style='padding:5px'>" + order2[i][j] + "</td><td style='padding:5px'>" + order3[i][j] + "</td></tr>";
    }
}

这假定productorder1order2order3都具有相同的尺寸。