【问题标题】:HTML table change text color on conditionHTML表格根据条件更改文本颜色
【发布时间】:2021-09-24 02:52:17
【问题描述】:

我在谷歌表格中有一个谷歌应用脚​​本,它可以提取数据并将其放入表格并通过电子邮件发送。它很好用,但我的问题是,如果表值(例如 r[7])等于 FAULT 而不是 Good,我该如何将文本颜色更改为红色?

function sendEmail() {
  
  const ss  = SpreadsheetApp.getActiveSpreadsheet();
  const ws = ss.getSheetByName('Sheet')//rename sheet name per your file;
  const headers = ws.getRange('A1:H1').getDisplayValues()//get table headers name;
  
  //assign variable to each header value
  const hospital = headers[0][0]; 
  const asset = headers[0][1];
  const time = headers[0][2];
  const helevel = headers[0][3];
  const hepressure = headers[0][4];
  const chillertemp = headers[0][5];
  const roomtemp = headers[0][6];
  const compressor = headers[0][7];

  const lr = ws.getRange('H14').getValues()//get last low row of active range, forula entered in B1 =countif(B3:B,"*")+2;

  const tablerangeValue = ws.getRange(2,1,18,9).getDisplayValues()//get range value, my data range start from 4th row and has 7 columns, pls chage per your need;

  const htmlTemplate = HtmlService.createTemplateFromFile('emailTable');

  htmlTemplate.hospital = hospital;
  htmlTemplate.asset = asset;
  htmlTemplate.time = time;
  htmlTemplate.helevel = helevel;
  htmlTemplate.hepressure = hepressure;
  htmlTemplate.chillertemp = chillertemp;
  htmlTemplate.roomtemp = roomtemp;
  htmlTemplate.compressor = compressor;
  htmlTemplate.tablerangeValue = tablerangeValue;

  const htmlForEmail = htmlTemplate.evaluate().getContent();
  
  
  var toEmail = 'user@email.com';
  var ccEmail = '';

  MailApp.sendEmail({
  to: toEmail,
  //cc: ccEmail,
  subject: "***Daily Readings***",
  htmlBody:htmlForEmail
    });

}
<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <style>
  table, th, td {
  border: 0px solid black;
  font-size:15px;
}

th, thead {
  background-color: lightblue;
}

</style>  
  </head>
  <body>
    <div>
      <table id="table1">
        <thead>
          <th><?= hospital ?></th><th><?= asset ?></th><th><?= time ?></th><th><?= helevel ?></th><th><?= hepressure ?></th><th><?= chillertemp ?></th><th><?= roomtemp ?></th><th><?= compressor ?></th>
        </thead>
        <tbody >
          <? tablerangeValue.forEach(r => {?>
          <tr> 
          <td><?= r[0] ?></td><td><?= r[1] ?></td><td><?= r[2] ?></td><td style="text-align:center"><?= r[3] ?></td><td style="text-align:center"><?= r[4] ?></td><td style="text-align:center"><?= r[5] ?></td><td style="text-align:center"><?= r[6] ?></td><td style="text-align:center" ><?= r[7] ?></td>
          </tr>
          <?})?>
          
        </tbody>

      </table>
      <script> 
       const values = document.querySelectorAll("td");

function changeTdColor() {
  for (let i = 0; i < values.length; i++) { // iterate all thorugh td
      if (values[i].innerText == "r[9]") { // check if td has desired value
          values[i].style.backgroundColor = "red"; // if matches, change color
          }
       }
}

changeTdColor(); // call function
      </script>
      
    </div>
  </body>
  
</html>

我已经添加了从谷歌表格中提取数据的另一部分代码。

【问题讨论】:

    标签: html css google-sheets


    【解决方案1】:

    你可以使用javascript。

    例如,您可以迭代所有 td 并检查值。

    const values = document.querySelectorAll("td");
    
    function changeTdColor() {
      for (let i = 0; i < values.length; i++) { // iterate all thorugh td
          if (values[i].innerText == "r[9]") { // check if td has desired value
              values[i].style.backgroundColor = "red"; // if matches, change color
              }
           }
    }
    
    changeTdColor(); // call function
    <table id="table1">
          <tr> 
            <td>r[0]</td>
            <td>r[1]</td>
            <td>r[2]</td>
            <td>r[9]</td>
            <td>r[4]</td>
            <td>r[9]</td>
        </tr>
    
    </table>

    【讨论】:

    • 我应该把它放在我的 html 代码的什么地方?我是否使用
    • 是的,您可以使用脚本标签。但请确保您的脚本标签是您正文的结尾。如果这有帮助,也不要忘记提出并接受解决方案
    • 这不起作用,我编辑了我的代码。你能看看我做对了吗?
    • 好的@Lane,我在xampp中测试了我的代码(下面的第二个答案),并作为一个PHP文件并且它工作正常。只需复制和粘贴。如果它对你有用,请接受我的回答,非常感谢。
    • @Ezan​​i,这仍然不起作用。我正在添加我的代码的另一部分,看看这是否有帮助。
    【解决方案2】:
    
    <html>
    
      <head>
      
        <base target="_top">
        
        
        <style>
          table, th, td {
          border: 0px solid black;
          font-size:15px;
          }
    
          th, thead {
            background-color: lightblue;
          }
    
    </style>  
    
      </head>
      
      
      <body>
      
        <div>
        
        <?php define("r",["Good", "FAULT", "Good", "Good", "Good", "Good", "Good", "Good"]); ?>
        
          <table id="table1">
          
              <tr> 
              <td><?php if (r[0]=="FAULT") { echo "<td style='color:red'>" . r[0] . "</td>"; } else { echo "<td style='color:black'>" . r[0] . "</td>"; } ?></td>
              <td><?php if (r[1]=="FAULT") { echo "<td style='color:red'>" . r[1] . "</td>"; } else { echo "<td style='color:black'>" . r[1] . "</td>"; } ?></td>
              <td><?php if (r[2]=="FAULT") { echo "<td style='color:red'>" . r[2] . "</td>"; } else { echo "<td style='color:black'>" . r[2] . "</td>"; } ?></td>
              <td><?php if (r[3]=="FAULT") { echo "<td style='color:red'>" . r[3] . "</td>"; } else { echo "<td style='color:black'>" . r[3] . "</td>"; } ?></td>
              <td><?php if (r[4]=="FAULT") { echo "<td style='color:red'>" . r[4] . "</td>"; } else { echo "<td style='color:black'>" . r[4] . "</td>"; } ?></td>
              <td><?php if (r[5]=="FAULT") { echo "<td style='color:red'>" . r[5] . "</td>"; } else { echo "<td style='color:black'>" . r[5] . "</td>"; } ?></td>
              <td><?php if (r[6]=="FAULT") { echo "<td style='color:red'>" . r[6] . "</td>"; } else { echo "<td style='color:black'>" . r[6] . "</td>"; } ?></td>
              <td><?php if (r[7]=="FAULT") { echo "<td style='color:red'>" . r[7] . "</td>"; } else { echo "<td style='color:black'>" . r[7] . "</td>"; } ?></td>
              </tr>
            
          </table>
          
        </div>
        
      </body>
      
      </html>
    

    【讨论】:

    • OP 要求更改特定表格中的颜色。
    • 好的,谢谢@Muhammedogz。答案已编辑。
    • 我需要在单元格读取错误时更改字体颜色,所有其他输出应保持默认。
    猜你喜欢
    • 2021-01-09
    • 2022-07-21
    • 2018-06-19
    • 2021-11-17
    • 1970-01-01
    • 2014-10-22
    • 1970-01-01
    • 2013-10-23
    相关资源
    最近更新 更多