【问题标题】:Google Sheets Script getLastRow() - How to get submission values, for each NEW ITEM (rows)?Google Sheets Script getLastRow() - 如何获取每个新项目(行)的提交值?
【发布时间】:2021-06-10 07:07:47
【问题描述】:

谁能指导我,我尝试了很多不同的方法,但无法找出问题。

<!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. 我想用在谷歌表格中提交的所有新项目(行)向不同的接收者发送电子邮件。 到目前为止,当我运行脚本时,只会发送 1 行,row3。对于要发送的“所有包含数据的行”,我缺少哪一部分?

  1. 从“后台”选项卡将电子邮件添加到分发列表,其中包含收件人电子邮件列表,运行时没有错误,但不会向人员列表发送电子邮件。我想这里缺少什么?

[![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 
}

【问题讨论】:

  • 您的代码包含以下行:` var row = 3; subVals = sheet.getRange(row, 1, 1, 22).getValues()` - 所以您只从该行检索值。如果您还对其他行感兴趣,则需要相应地循环遍历它们 - 显示您的电子表格(或它的屏幕截图)并更详细地描述您要完成的工作会有所帮助。
  • 除了上面的评论,更具体地说,sheet.getRange(row, 1, 1, 22).getValues() 表示您要求从第 3 行和第 1 列开始收集总共 1 行和 22 列的值。您的电子邮件分发列表与.getLastRow() 有正确的想法,但您需要稍作调整。在您的分发列表构建中:那里什么都没有。您所做的就是获取第 1 列值的数组。收件人必须是逗号分隔的字符串,而不是数组。
  • @ziganotschka 我添加了我的电子表格的屏幕截图。请您看一下并指导我。非常感谢!
  • 我确实尝试了不同的方法(1 和 2),但都没有奏效。例如,而不是`var row = 3; subVals = sheet.getRange(row, 1, 1, 22).getValues()` : 1. //lastRow = sheet.getLastRow(); //subVals = sheet.getRange(sheet.getLastRow(), 1, 1 ,22).getValues(); // 测试有数据的最后一行 2. // subVals = sheet.getRange(lastRow -1 , 1, 1 ,22).getValues(); // 测试有数据的最后一行 //lastRow-1
  • 当你说I'd like to sendEmail to the different receivers with all NEW ITEM (rows) that are submitted in google sheet 提交是什么意思。您是在谈论表单提交吗?

标签: function email google-apps-script datatable range


【解决方案1】:

我修改了你的 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都变了

【讨论】:

  • 非常感谢您的帮助。我添加了 2 个屏幕截图:升级选项卡 + 后台选项卡。
猜你喜欢
  • 1970-01-01
  • 2017-04-11
  • 2010-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多