javascript 将表格内容“逐行”附加到Google工作表中的电子邮件中

piztneat  于 2023-03-28  发布在  Java
关注(0)|答案(1)|浏览(105)

我挣扎着发送电子邮件给自己与表的内容'一行一行'.我的需求很简单:我对一列进行计数,如果值大于0,那么我的Google触发器将定期检查并向我发送电子邮件。
但是我无法将行(行)的内容放入到电子邮件的“消息”中,类似于
内文:

  • 第1行|新闻资讯|文本
  • 第2行|新闻资讯|文本
  • 第三行|新闻资讯|文本
  • ....

我的表格截图附件。我在电子邮件中要求的范围是'A3:D'(如果不是空的)。所以这是我现在的简单代码:

function sendEmail() {
  // Fetch due Today
  var myRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("DueToday").getRange("D1"); 
  var todaydue = myRange.getValue();

  // for now I check only one cell - but would like to get the set of rows (line by line)
  var myDetailsRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("DueToday").getRange("B3:B3");
  var myDetails = myDetailsRange.getValues();  
  var result = [].concat.apply([], myDetails).filter(String);

  // Check totals due today
    if (todaydue > 0) {
    // Send Alert Email
        var emailAddress = 'mymail@gmail.com'
        var subject = 'JRFgo² Reminder: ' + todaydue+ ' due today';
        var message = '‣ '+result+ ;
        GmailApp.sendEmail(emailAddress, subject, message);
      }
}

nlejzf6q

nlejzf6q1#

试试这个:

function sendEmail() {
  
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("DueToday");//your Sheet name
  const lastRow = sheet.getLastRow();
  let message = "";
  const headersLength = 2;  
  
  //if lastRow is higher than 2, means there are rows with tasks
  if (lastRow > headersLength){

    //get all data range with tasks
    const data = sheet.getRange(3,1,lastRow - headersLength,sheet.getLastColumn()).getValues();
    
    //Loop through each row to build the message
    for (var i = 0; i < data.length; i++) {

      message+= `Row ${i+1} Data: ${data[i]}\n\n`

    }
    const subject = `JRFgo² Reminder: ${lastRow-headersLength} due today`;
    const emailAddres = "YOUR EMAIL ADDRESS";
    GmailApp.sendEmail(emailAddres, subject, message);
  }
}

此方法不需要D1中的值即可工作,因为它会检查工作表的长度。请注意,如果在标题上方删除或添加更多行,则需要更新headersLength值,以匹配要从任务的数据范围中排除的行数

相关问题