提供发送 MailApp 邮件

Sending MailApp mail provided

这样的问题:有一个代码发送一封电子邮件,其中包含单元格“var RANGE”的数据在“if”函数中“var check”> 10 的条件下需要做的是:

  1. “var check”条件没有解析单个单元格,而是一个范围。 (只写Q123:Q126,不行,脚本不执行)并且条件不应该>10,而是等于=10,因为触发器每天运行函数并检查table.

  2. 如果数字 10 在“var check”范围内,则应通过电子邮件发送整行。如果不行,至少要这样做>

  3. “var RANGE”可以选择性地获取单元格(例如 B121:N129、R123:T129)

我能得到一些建议吗?

var ID = "1sBtjJ9x4IKwb9GKu3x3DwoNWTxPej7HJWk5ze8aK"; //speadsheet id
var EMAIL = "Test@gmail.com"; //email 
var RANGE = "Warranty_2021!B121:n129"; //data range to send
var check = "Warranty_2021!Q123:Q123"; //parametr
var text = "Servis"; //subject


function sendData() {
  var spreadsheet = SpreadsheetApp.openById(ID);
  var data = spreadsheet.getRangeByName(RANGE).getValues();
  var message = {};
  if (SpreadsheetApp.openById(ID).getRangeByName(check).getValues()>10){ //condition
  message.subject = "[La Test] " + text;
  message.to = EMAIL;
  message.htmlBody = dataToHtmlTable_(data) +
    "<br><br>[IT care you ;)]";
  MailApp.sendEmail(message);
  }
  let quota = MailApp.getRemainingDailyQuota();
  console.log(quota)
}

Array.prototype.datesToString = function(){
  return this.map(function(row){
    return row.map(function(cell){
      return cell && cell.getTime ? Utilities.formatDate(cell, Session.getScriptTimeZone(), "yyyy-MM-dd ") : cell;
    });
  });
}


function dataToHtmlTable_(data){
  return JSON.stringify(data, null, "  ")
  .replace(/^\[/g, "<table>")
  .replace(/\]$/g, "</table>")
  .replace(/^\s\s\[$/mg, "<tr>")
  .replace(/^\s\s\],{0,1}$/mg, "</tr>")
  .replace(/^\s{4}"{0,1}(.*?)"{0,1},{0,1}$/mg, "<td></td>");
}

完成了。 @cooper 帮我一点点。 解决方案:

var ID = "1sBtjJ9x4IKwb9GKu3x3DwoNWTxPej7"; //speadsheet id
var EMAIL = "Test@gmail.com"; //email 
var RANGE_LUT = "Warranty_2021!B10:Q20"; //data range to send
var text = "Time to ..."; //subject

function sendData() {
  var spreadsheet = SpreadsheetApp.openById(ID);
  var dataLut = spreadsheet.getRangeByName(RANGE_LUT).getValues();
  var message = {};
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Warranty_2021");
  var colValuesLut = sheet.getRange(11, 16, 10, 1).getValues();

for (var i = 0; i < colValuesLut.length; i++) {
  if (colValuesLut[i][0] == -10) { //condition, value = -10 
  message.subject = "[La test] " + text;
  message.to = EMAIL;
  message.htmlBody = dataToHtmlTable_(dataLut) +
    "<br><br>[IT care you]";
  MailApp.sendEmail(message);
  }
  let quota = MailApp.getRemainingDailyQuota();
  console.log(quota)
}
}