修改JS代码适配丹麦数字格式实现表格单元格着色
解决丹麦语数字格式的表格单元格着色问题
问题说明
现有内嵌在HTML报表的CSS/JS代码,原本可识别以英文小数点(.)格式的数字,给表格单元格按数值范围分级着色。但切换到丹麦语环境后,数字格式变为以逗号(,)作为小数点(比如4,1),原代码无法将这类字符串转换为有效数字,导致着色完全失效,且无法移除丹麦语言包。
修改后的完整代码
<style type="text/css"> div.SpreadsheetBlockRow.TableContainer table{ font-size: 8pt; } </style> <script type="text/javascript"> window.onload = function() { var tableCells = document.querySelectorAll('div.SpreadsheetBlockRow.TableContainer td'), tableCellsArray = Array.from(tableCells), number; tableCellsArray.forEach(function(cell) { // 先把丹麦语的逗号小数点替换成JS能识别的点,再转成数字 number = Number(cell.textContent.replace(',', '.')); if (number <= 2.99){ cell.style.backgroundColor = '#244061'; } else if (number >= 3 && number <= 3.499){ cell.style.backgroundColor = '#366092'; } else if (number >= 3.5 && number <= 3.99){ cell.style.backgroundColor = '#95b3d7'; } else if (number >= 4 && number <= 4.499){ cell.style.backgroundColor = '#b8cce4'; } else if (number >= 4.5 && number <= 5){ cell.style.backgroundColor = '#dbe5f1'; } }); }; </script>
关键修改点
- 仅需改动数字转换的一行:将
number = Number(cell.textContent);替换为number = Number(cell.textContent.replace(',', '.')); - 原理:JavaScript的
Number()函数只能识别以.为小数点的数字字符串,先把丹麦语格式里的,替换成.,就能正常转换为数值,后续的着色判断逻辑无需改动。
内容的提问来源于stack exchange,提问作者Jacob
相关产品推荐
相关产品推荐

