Google Sheets Script getLastRow() - 如何获取每个新项目(行)的提交值?

Google Sheets Script getLastRow() - How to get submission values, for each NEW ITEM (rows)?

谁能指导我,我试了很多不同的方法都找不到问题。

<!DOCTYPE html>
<html>


<body>

  <head>
    <base target="_top">
  </head>
<body style="font-family:roboto,light,300;font-size:14px;font-style:normal;">

<div >
  <p> 
Hi there,<br/><br/>
The following case requires to be process.<br/>Please have a look and update the spreadsheet, click here to open: <br/>
https://docs.google.com/spreadsheets/d/1hPfBxMpXABG09cPOvzAQu0SDZLf-TSUTvv4_02BA8HE/edit#gid=0  <br/> 
  </p> 

</div>

</body>


<head>
  <style>
  table {width:75%;}
  table, th, td 
  {
  border: 1px solid black;
  border-collapse: collapse;
  }
  th, td 
  {
  padding: 14px;
  text-align: left;
  }

  #t01 tr:nth-child(even) 
  {
  background-color: #eee;
  }
  #t01 tr:nth-child(odd) 
  {
 background-color: #fff;
  }
  #t01 th 
  {
  background-color: #3e8cbd;
  color: white;
  }
  </style>
</head>
<body>

<table id="t01">
 <tr>
    <th width="100" scope="col" >Status</th>
    <th scope="col"  >Language</th>
    <th scope="col"  >P/R</th>
    <th scope="col"  >DJID F-Code</th>
    <th scope="col"  >Deadline</th>
  </tr>

  <tr> 
    <td><?= subVals[0][1] ?></td>
    <td><?= subVals[0][5] ?></td>
     <td><?= subVals[0][6] ?></td>
    <td><?= subVals[0][7] ?></td> 
    <td><?= deadline ?></td>
  </tr>
</table>  

</body>

<body>
  <br/>

  <head>
  <base target="_top">
  </head>
  <body style="font-family:roboto,light,300;font-size:14px;font-style:normal;">

  <div >
<p> Should you have any questions or doubts please reach out to us: xxxxxx@gmail.com <br/> <br/>  Best Regards, </p> 
  </div>
</body>


</html>

  1. 我想将在 google sheet 中提交的所有新项目(行)发送给不同的收件人。 到目前为止,当我 运行 脚本时,只会发送 1 行,第 3 行。对于要发送的“所有包含数据的行”,我缺少哪一部分?

  1. 从带有收件人电子邮件列表的“后台”选项卡将电子邮件添加到通讯组列表 运行ning 没有错误,但它不会向人员列表发送电子邮件。我想这里少了什么?

[![enter image description here][3]][3]




function sendEmail() {
  var ss = SpreadsheetApp.getActiveSpreadsheet(),
      sheet = ss.getSheetByName('ESCALATION');                          //Code running off "ESCALATION" tab
    
    var row = 3; 
      subVals = sheet.getRange(row, 1, 1, 22).getValues(),              

      deadline = sheet.getRange(3, 13).getDisplayValue(),             //Gets display value of deadline date to pass to html file due to time/date formatting
      subject = "Escalations Report";                                 //Create subject variable

  var template = HtmlService.createTemplateFromFile('Email Table');   //Create template of html file
  
  template.subVals = subVals;                                         //Send subVals variables to template so can reference in html using scriplets
  template.deadline = deadline;                                        //Send deadline variable to template so can reference in html using scriplets
  
  var html = template.evaluate().getContent();                        //Evalutate and create html output
  
  var sendto = "xxxxxx@gmail.com";                                    //Test send Email only to myself 
 

// Add Email to Distribution List    > is not working!!???
      var BACKsheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Backstage");   // "Backstage" tab with a list of recipience email
      var columnValues = BACKsheet.getRange(1, 10, BACKsheet.getLastRow()).getValues();    //1st is header row 
   
  
 GmailApp.sendEmail(sendto, subject, '', {htmlBody:html});   //send email with recipients from subVals and options set htmlBody to html variable we created 
}

我修改了你的html并提供了我的一些假数据并发送了两封电子邮件。我还修改了需要展平并用逗号连接的代码的收件人部分。

gs:

function sendEmail() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const esh = ss.getSheetByName('ESCALATION');
  const subVals = esh.getRange(3, 1, 1, 22).getValues();
  const deadline = esh.getRange(3, 13).getDisplayValue();
  const subject = "Escalations Report";
  let template = HtmlService.createTemplateFromFile('ah1');
  template.subVals = subVals;
  template.deadline = deadline;
  let html = template.evaluate().getContent();
  SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutput(html),'Test')
  let bsh = ss.getSheetByName("Backstage");
  let recipient = bsh.getRange(1, 11, bsh.getLastRow()).getValues().flat().join(',');
  GmailApp.sendEmail(recipient,subject, '', { htmlBody: html });
}

html:

<!DOCTYPE html>
<html>
<body>
  <head>
    <base target="_top">
      <style>
  table {width:75%;}
  table, th, td {border: 1px solid black;border-collapse: collapse;}
  th, td {padding: 14px;text-align: left;}
  #t01 tr:nth-child(even) {background-color: #eee;}
  #t01 tr:nth-child(odd) {background-color: #fff;}
  #t01 th {background-color: #3e8cbd;color: white;}
  </style>
  </head>
<body style="font-family:roboto,light,300;font-size:14px;font-style:normal;">
<div >
  <p> Hi there,<br/><br/>The following case requires to be process.<br/>Please have a look and update the spreadsheet, click 
<a href="https://docs.google.com/spreadsheets/d/1hPfBxMpXABG09cPOvzAQu0SDZLf-TSUTvv4_02BA8HE/edit#gid=0">here</a> to open: <br/> 
  </p> 
</div>
<table id="t01">
 <tr><th width="100" scope="col" >Status</th><th scope="col"  >Language</th><th scope="col"  >P/R</th><th scope="col"  >DJID F-Code</th><th scope="col"  >Deadline</th>
  </tr>
  <tr> <td><?= subVals[0][1] ?></td><td><?= subVals[0][5] ?></td><td><?= subVals[0][6] ?></td><td><?= subVals[0][7] ?></td><td><?= deadline ?></td></tr>
</table>  
  <br/>
   <div >
<p> Should you have any questions or doubts please reach out to us: xxxxxx@gmail.com <br/> <br/>  Best Regards, </p> 
  </div>
</body>
</html>

邮箱:

三行代码: GS:

function sendEmail() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const esh = ss.getSheetByName('Sheet1');
  const bsh = ss.getSheetByName("Sheet2");
  const recipient = bsh.getRange(1, 11, bsh.getLastRow()).getValues().flat().join(',');
  const vs = esh.getRange(3, 1, 3, 22).getValues();
  const subject = "Escalations Report";
  vs.forEach(r => {
    let template = HtmlService.createTemplateFromFile('ah1');
    template.subVals = r;
    let html = template.evaluate().getContent();
    SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutput(html), 'Test');
    let end = "is near";
    GmailApp.sendEmail(recipient, subject, '', { htmlBody: html });
  });
}

HTML:

<!DOCTYPE html>
<html>
<body>
  <head>
    <base target="_top">
      <style>
  table {width:75%;}
  table, th, td {border: 1px solid black;border-collapse: collapse;}
  th, td {padding: 14px;text-align: left;}
  #t01 tr:nth-child(even) {background-color: #eee;}
  #t01 tr:nth-child(odd) {background-color: #fff;}
  #t01 th {background-color: #3e8cbd;color: white;}
  </style>
  </head>
<body style="font-family:roboto,light,300;font-size:14px;font-style:normal;">
<div >
  <p> Hi there,<br/><br/>The following case requires to be process.<br/>Please have a look and update the spreadsheet, click 
<a href="https://docs.google.com/spreadsheets/d/1hPfBxMpXABG09cPOvzAQu0SDZLf-TSUTvv4_02BA8HE/edit#gid=0">here</a> to open: <br/> 
  </p> 
</div>
<table id="t01">
 <tr><th width="100" scope="col" >Status</th><th scope="col"  >Language</th><th scope="col"  >P/R</th><th scope="col"  >DJID F-Code</th><th scope="col"  >Deadline</th>
  </tr>
  <tr> <td><?= subVals[1] ?></td><td><?= subVals[5] ?></td><td><?= subVals[6] ?></td><td><?= subVals[7] ?></td><td><?= subVals[12] ?></td></tr>
</table>  
  <br/>
   <div >
<p> Should you have any questions or doubts please reach out to us: xxxxxx@gmail.com <br/> <br/>  Best Regards, </p> 
  </div>
</body>
</html>

注意:gs 和 html 都已更改