CSS代码来将英文数字修改为丹麦数字

huangapple go评论55阅读模式
英文:

CSS code to modify reading from english digits to danish digits

问题

在软件中,我有一个CSS代码,应该在电子表格中创建颜色格式(该电子表格包含在从软件创建的HTML报告中)。
我有所需的CSS代码,适用于英文报告。但是,我遇到了一个问题,因为丹麦语中的数字是(4,1)而不是(4.1),所以当软件的语言代码为丹麦语时,该代码不起作用,因为它无法将数字读取为(4,1)。有没有办法修改代码,使其读取“丹麦语”数字而不是英文?(报告有丹麦语和英文版本,我需要丹麦版本能够工作,我无法从软件中删除丹麦语言包)。

下面是使用的代码,它在丹麦数字上不起作用。如果我只是将代码中的数字修改为(3,5),则格式不起作用。有没有办法修改代码,使格式基于丹麦数字?

我对编程的了解非常有限 CSS代码来将英文数字修改为丹麦数字 非常感谢任何帮助。

<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) {
      number = Number(cell.textContent);
      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>
英文:

In a software i have a CSS code that should create color formatting in a spreadsheet (the spreadsheet is included in a html report created from the software).
I have the needed CSS code which works for reports in english. But i face an issue hence the danish digits are like this (4,1) instead of (4.1), hence the language code in the software is danish, the code don't work as it can't read digits as (4,1). Is there a way i can change the code so it reads "danish" digits instead of english? (there is a danish and a english version of the report, and i need the danish version to work and i cant remove the danish language package from the software).

The code below is the one used, which is not working on dansih digits. If i just modify the digits in the code to (3,5) the formatting is not working. Is there a way to modify the code so the formatiing is based on danish digits?
I have very limited understanding of coding CSS代码来将英文数字修改为丹麦数字 Any help is really appriciated.

"<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) {
      number = Number(cell.textContent);
      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>" 

I expect the color formatting to work in the danish version of the report, but there is just no formatting when i just change the digits in the code from (4.1) to (4,1)

答案1

得分: 1

如何使用 CSS 解决?

CSS 不适合 用于替换文本中的字符,因为 CSS 处理外观和样式,而不是文本内容。字符替换通常通过 JavaScript 完成。

解决方案

使用 JavaScript 可以

cell.textContent.replace('.', ',')

如果我理解正确,cell.textContent 的类型是字符串。所以,你可以将文本中的 . 替换为 ,

如果它不是字符串类型,你可以将其转换为字符串:toString()

或者你可以使用 NumberFormat

const formatter = new Intl.NumberFormat('da-DK', { style: 'decimal' });
const formattedNumber = formatter.format(cell.textContent);

示例

// 如果 cell.textContent 是字符串
tableCellsArray = tableCellsArray.map(function (cell) {

  // 替换 . 为 ,
  cell.textContent = cell.textContent.replace('.', ',');

  return cell;
});


// 如果 cell.textContent 是数字
const formatter = new Intl.NumberFormat('da-DK', { style: 'decimal' });
tableCellsArray = tableCellsArray.map(function (cell) {

  // 值得保存格式化后的值到一个单独的键中
  // 这样可以保留原始数字格式,
  // 并在屏幕上显示丹麦格式。
  cell.danishTextContent = formatter.format(cell.textContent);

  return cell;
});

英文:

How to can solve with CSS?

CSS is not suitable for replacing characters in the text, as CSS deals with the appearance and style, not the content of the text. Character replacement is usually done with JavaScript.

Solution

With JavaScript can use:

cell.textContent.replace('.', ',')

If I understand correctly, then cell.textContent is of type string. So, you can replace . to , in text.

If it's not already of string type, you can convert it to one: toString()

Or you can use NumberFormat:

const formatter = new Intl.NumberFormat('da-DK', { style: 'decimal' });
const formattedNumber = formatter.format(cell.textContent);

Example

// if cell.textContent is string
tableCellsArray = tableCellsArray.map(function (cell) {

  // Replace . to ,
  cell.textContent = cell.textContent.replace('.', ',');

  return cell;
});


// if cell.textContent is number
const formatter = new Intl.NumberFormat('da-DK', { style: 'decimal' });
tableCellsArray = tableCellsArray.map(function (cell) {

  // It's worth saving the formatted value in a separate key
  // so that the original number format is preserved,
  // and the Danish format can be displayed on the screen.
  cell.danishTextContent = formatter.format(cell.textContent);

  return cell;
});

huangapple
  • 本文由 发表于 2023年4月19日 16:23:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76052247.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定