获取符合条件的列的行并通过电子邮件发送(Google表格)

时间:2018-04-05 12:13:18

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

这里需要在列中设置最小数量,并且一旦库存中有项目' qtys等于或低于该值,每天发送一次带有行的电子邮件。 我将以下脚本用于其他目的,但这会一次发送一行电子邮件,而我们现在需要获取行并通过一封电子邮件发送(HTML格式是理想的)。

var EMAIL_SENT = "EMAIL_SENT";

function sendEmails2() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Pedidos");
  var startRow = 7;  // First row of data to process
  var numRows = sheet.getLastRow();   // Number of rows to process
  // Fetch the range of cells A7:s3
  var dataRange = sheet.getRange(startRow, 1, numRows, 25)
  // Fetch values for each row in the Range.
  var data = dataRange.getValues();
  for (var i = 0; i < data.length; ++i) {
    var rowData = data[i];
    if (rowData[13] === "Ativo") {
    var emailAddress = "";  // First column
    var update = rowData[12];
    var NumPedido = rowData[0];
    var Cliente = rowData[1];
    var UF = rowData[3];
    var Produto = rowData[4];
    var TotaldoPedido = rowData[7];
    var NumNF = rowData[8];
    var Status = rowData[13];
    var Obs = rowData[16];
    var DataAgenda = rowData[11];
    var senha = rowData[15];
    var respons = rowData[17];
    var message =  "<HTML><BODY>"
    //+ "<P>Dear " + firstname + ","
    //+ "<br /><br />"
    + "<P>Status do Pedido " + NumPedido + ", do cliente " + Cliente + " foi atualizado! Veja abaixo:"
    + "<brr /><br />"
    + "<br>Data da Atualização:  </b>" + update + "<br />" 
    + "<br>Nº do Pedido:  </b>" + NumPedido + "<br />" 
    + "<br>Cliente:  </b>" + Cliente + UF + "<br />" 
    + "<br>Produto:  </b>" + Produto + "<br />" 
    + "<br>Nº da NF:  </b>" + NumNF + "<br />"  
    + "<br>Status: </b>" + Status + "<br />"  
    + "<br>Senha:  </b>" + senha + "<br />"  
    + "<br>Observações: </b>" + Obs + "<br />" 
    + "<br>Responsável: </b>" + respons + "<br />" 
    + "<br /><br />"    
    + "<br>Clique para explorar detalhes, ou para atualizar o status: </b>" + "" + "<br />" 
    + "<br /><br />"  
    + "</HTML></BODY>";      
    var emailAtivo = rowData[18];    
    if (emailAtivo != EMAIL_SENT && rowData[13] === "Ativo") {  // Prevents sending duplicates
      var subject = "Status do Pedido " + NumPedido + " do " + Cliente + " " + UF + " atualizado";
  MailApp.sendEmail({
    to: emailAddress, 
    subject: subject, 
    htmlBody: message
  });

  sheet.getRange(startRow + i, 19).setValue(EMAIL_SENT);

  // Make sure the cell is updated right away in case the script is interrupted
  SpreadsheetApp.flush();
//}

} }

这是更新的脚本:         function sendTableEmail(){       var sheet = SpreadsheetApp.getActiveSpreadsheet()。getSheetByName(&#34; Estoque-Almox - ADB&#34;);

  // Get your table data
  var startRow = 7;  // First row of data to process
  var numRows = sheet.getLastRow(); // Number of rows to process
  var dataRange = sheet.getRange(startRow, 1, numRows, 14); // Fetch the range of cells A7:D
  var data = dataRange.getValues(); // Fetch values for each row in the Range.
  var minimum = sheet.getRange("n7:n32").getValue(); // Minimum quantity to check against

  // Loop through the data to build your table
  var message = "<html><body><table style=\"text-align:center\"><tr><th>Código</th><th>Descrição</th><th>Qtd(Cx)</th><th>Estoque Mín. (Cx)</th></tr>";
  for (var i = 0; i < data.length; ++i) {
    var rowData = data[i];
    var Code = rowData[4];
    var Description = rowData[5];
    var InventoryUnit = rowData[10];
    var InventoryBox = rowData[11];
    var InventoryPallet = rowData[12];
    //var MinQty = rowData[13];
    //if (minimum >= 31) {
      if (InventoryBox <= minimum) {
        //var valueA = rowData[0];
        //var valueB = rowData[1];
        message += "<tr><td>"+Code+"</td><td>"+Description+"</td><td>"+InventoryBox+"</td><td>"+minimum+"</td></tr>";
      //} 
    }
  }
  message += "</table></body></html>";
  var subject = "Alerta de Estoque Mín. na Expedição";
  MailApp.sendEmail({
    to: "EMAIL", 
    subject: subject, 
    htmlBody: message
  });
}

...这是我在电子邮件中收到的结果: enter image description here 它带有十进制数字(第4列),它不应该是第5列,第5列显示30,而在电子表格中,公式的结果是&#34;&#34;没有列出产品的地方。 再次感谢一百万人的帮助。

抱歉,这里有: enter image description here

因此,如果列L <=列N,则获取满足此条件的行并通过电子邮件发送。

1 个答案:

答案 0 :(得分:2)

我更清楚地了解你现在在做什么。由于“最小值”对于每一行都是唯一的,因此您可以在启动dataRange时将其拉出。要防止列表包含空白值,请在继续之前检查Code是否为空。要修正十进制数,请使用toFixed()see docs)。最后,为了防止不需要发送电子邮件,我添加了sendEmail布尔值。如果您的所有数量都合适,则不会发送任何电子邮件。

function sendTableEmail() { 
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Estoque-Almox - ADB");

  // Get your table data
  var startRow = 7;  // First row of data to process
  var numRows = sheet.getLastRow(); // Number of rows to process
  var dataRange = sheet.getRange(startRow, 1, numRows, 15); // Fetch the range, including the minimums
  var data = dataRange.getValues(); // Fetch values for each row in the Range.

  // Loop through the data to build your table
  var message = "<html><body><table style=\"text-align:center\"><tr><th>Código</th><th>Descrição</th><th>Qtd(Cx)</th><th>Estoque Mín. (Cx)</th></tr>";
  var sendEmail = false; // If there is content to send, will be set to true
  for (var i = 0; i < data.length; ++i) {
    var rowData = data[i];
    var Code = rowData[4];
    if (Code != "") { // Check for blank Code. If blank, then skip and don't add to the table.
      var Description = rowData[5];
      var InventoryUnit = rowData[10];
      var InventoryBox = rowData[11];
      var InventoryPallet = rowData[12];
      var minimum = rowData[13];
      if (InventoryBox <= minimum) {
        message += "<tr><td>"+Code+"</td><td>"+Description+"</td><td>"+Number(InventoryBox).toFixed(0)+"</td><td>"+minimum+"</td></tr>";
        sendEmail = true; // There is content to send!
      }
    }
  }
  message += "</table></body></html>";
  if (sendEmail) {
    var subject = "Alerta de Estoque Mín. na Expedição";
    MailApp.sendEmail({
      to: "EMAIL", 
      subject: subject, 
      htmlBody: message
    });
  }
}

您没有提供足够的信息来解决您的尝试问题,因此我会根据您提供的内容向您提供一个示例。一般原则是:

我希望这会有所帮助。

function sendTableEmail() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Pedidos");

  // Get your table data
  var startRow = 7;  // First row of data to process
  var numRows = sheet.getLastRow(); // Number of rows to process
  var dataRange = sheet.getRange(startRow, 1, numRows, 4); // Fetch the range of cells A7:D
  var data = dataRange.getValues(); // Fetch values for each row in the Range.
  var minimum = sheet.getRange("B4").getValue(); // Minimum quantity to check against

  // Loop through the data to build your table
  var message = "<html><body><table style=\"text-align:center\"><tr><th>A</th><th>B</th><th>Quantity</th><th>Status</th></tr>";
  for (var i = 0; i < data.length; ++i) {
    var rowData = data[i];
    var status = rowData[3];
    var quantity = rowData[2];
    if (status == "Ativo") {
      if (quantity <= minimum) {
        var valueA = rowData[0];
        var valueB = rowData[1];
        message += "<tr><td>"+valueA+"</td><td>"+valueB+"</td><td>"+quantity+"</td><td>"+status+"</td></tr>";
      } 
    }
  }
  message += "</table></body></html>";
  var subject = "Email Subject";
  MailApp.sendEmail({
    to: "YOUR_EMAIL_ADDRESS@DOMAIN.COM", 
    subject: subject, 
    htmlBody: message
  });
}

这就是电子表格的样子。

enter image description here