简体   繁体   English

Javascript/GAS-Googlesheet 到 HTML 表-只取第一列

[英]Javascript/GAS- Googlesheet to HTML table- Taking only first column

I have a code that reads a google sheet and supposed to take data to HTML table.我有一个读取谷歌表的代码,应该将数据带到 HTML 表中。

function Getdata(){

const ss= SpreadsheetApp.getActiveSpreadsheet();
const ws= ss.getSheetByName("Projects");
const headers= ws.getRange("A1:F").getValues();
const Project_State = headers[0][0];
const Start_Date = headers[0][1];
const End_Date = headers[0][2];
const Project_ID = headers[0][3];
const Project_Name = headers[0][4];
const Project_Code = headers[0][5];
const First_Name = headers[0][8];

const lr = ws.getLastRow();
const tableRangeValues=ws.getRange(2,1, lr-2).getDisplayValues();

const htmlTemplate = HtmlService.createTemplateFromFile("Notify") 
htmlTemplate.Project_State= Project_State;
htmlTemplate.Start_Date= Start_Date;
htmlTemplate.End_Date= End_Date;
htmlTemplate.Project_ID= Project_ID ;
htmlTemplate.Project_Name=Project_Name;
htmlTemplate.Project_Code=Project_Code;
htmlTemplate.First_Name=First_Name;
htmlTemplate.tableRangeValues=tableRangeValues;

const htmlForEmail = htmlTemplate.evaluate().getContent();

MailApp.sendEmail({
to: Session.getActiveUser().getEmail(),
subject: 'Project End - Action Required',
htmlBody: htmlForEmail,
  });
}

From the research I have done, I have been able to show the headers of the sheet in the HTML table sent via email, I am however having a challenge in capturing row data from the sheet to HTM table.根据我所做的研究,我已经能够在通过 email 发送的 HTML 表中显示工作表的标题,但是我在将行数据从工作表捕获到 HTM 表时遇到了挑战。 I am not sure if my range is off, or I am missing something.我不确定我的范围是否关闭,或者我错过了什么。 The thing is, I am only able to get the first column of data to my HTML table.问题是,我只能将第一列数据放入我的 HTML 表中。

Here is my HTML code:这是我的 HTML 代码:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
        
    <title>Weekly Report</title>
    
  </head>
  <body style="padding:3em;">

<p>Hi Project Leader,</p>
<p>please see below for the latest updates on the projcets you manage.<br>
<br /> Team</p>
<div style=""background-color:blue; height:4px;></div>
<h3> Project Details </h3>
<table>
<thead>
<tr>
<th><?= Project_State ?></th>
<th><?= Start_Date ?></th>
<th><?= End_Date ?></th>
<th><?= Project_ID ?></th>
<th><?= Project_Name?></th>
<th><?= Project_Code ?></th>
</tr>
</thead>
<tbody>
<?tableRangeValues.forEach(r=>{?>
<tr>
<td><?=r[0]?></td>
<td><?=r[1]?></td>
<td><?=r[2]?></td>
<td><?=r[3]?></td>
<td><?=r[4]?></td>
<td><?=r[5]?></td>
</tr>
<?})?>
</tbody>
</table>
<br>

</body>
</html>

This is the structure of the google sheet.这是谷歌表的结构。

在此处输入图像描述

In order to retrieve the row including the columns "A" to "F", I would like to propose the following modification.为了检索包含“A”到“F”列的行,我想提出以下修改。

From:从:

const tableRangeValues=ws.getRange(2,1, lr-2).getDisplayValues();

To:至:

const tableRangeValues=ws.getRange(2,1, lr-2, 6).getDisplayValues();
  • In your script, getRange(2,1, lr-2) is the cells of "A2:A" except for the bottom row.在您的脚本中, getRange(2,1, lr-2)是“A2:A”的单元格,但底行除外。 In order to retrieve the columns "A" to "F", I modified it to getRange(2,1, lr-2, 6) .为了检索“A”到“F”列,我将其修改为getRange(2,1, lr-2, 6) In this case, the range is the cells of "A2:F" except for the bottom row.在这种情况下,范围是“A2:F”的单元格,但底行除外。

  • If you want to retrieve the rows from 2nd row to the last row, please modify as follows.如果要检索从第 2 行到最后一行的行,请进行如下修改。

    • From

       const tableRangeValues=ws.getRange(2,1, lr-2).getDisplayValues();
    • To

       const tableRangeValues=ws.getRange(2,1, lr-1, 6).getDisplayValues();

References:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM