Javascript 设置表的innerHTML的条件
我有一个表,该表是使用以下HTML文件从excel文件导入的:Javascript 设置表的innerHTML的条件,javascript,excel,Javascript,Excel,我有一个表,该表是使用以下HTML文件从excel文件导入的: <!DOCTYPE html> <html> <head> <title>XLSX to HTML</title> <link rel="stylesheet" href="css/style.css" type="text/css"/> <script src='http://alasql.org/console/xlsx.cor
<!DOCTYPE html>
<html>
<head>
<title>XLSX to HTML</title>
<link rel="stylesheet" href="css/style.css" type="text/css"/>
<script src='http://alasql.org/console/xlsx.core.min.js'></script>
<script src='http://alasql.org/console/alasql.min.js'></script>
</head>
<body>
<script>
alasql('select * into html("#here",{headers:true}) \
from xlsx("data/data.xlsx",\
{headers:true})');
</script>
<div id="here"></div>
</body>
</html>
我的excel文件包含4列23行。第四列的值等于从第三列减去第二列。
现在我想为第四列设置一个条件。如果值大于0,则将与td相关的特定框颜色设置为绿色;如果值等于0,则将其设置为黄色;如果值小于0,则将颜色设置为红色。
如果你能帮助我度过难关,我将不胜感激。我只知道HTML+CSS的基础知识。您可以自己修改代码并生成HTML表,而无需使用Alasql的INTO HTML函数。 请参见下面的示例:
// First: read data from XLSX file into "res" variable
alasql('select * from xlsx("data/data.xlsx", {headers:true})',function(res){
// Start to create HTML text
var s = '<table><thead><th>Col1<th>Col2<tbody>'; // Generate table header
for(var i=0;i<res.len;i++) {
s += '<tr>';
s += '<td>'+res[i].Col1; // Cell for first column
s += '<td ';
// Now add colored cell for second column
if(res[i].Col2>0) s += 'style="background-color:yellow"';
else s += 'style="background-color:red"';
s += '>'+res[i].Col2;
}
s += '</table>';
});
document.getElementById('res').innerHTML = s;
请参阅下面的工作片段,其中介绍了为表格单元格着色的概念
免责声明:我是Alasql的作者
var res=[{a:1,b:1},{a:2,b:-1}];
var s='Col1Col2';
forvar i=0;i0 s+=“样式=背景色:黄色”;
else s+=“样式=背景色:红色”;
s+='>'+res[i].b;
}
s+=;
document.getElementById'res'。innerHTML=s;