Handsontable - 计算包含逗号的值

时间:2016-12-28 23:26:07

标签: javascript handsontable

我有一个使用handsontable的网页。在这个页面中,我试图以编程方式将一些数据放入电子表格并执行一些公式。我的公式正常工作,直到值包含逗号。例如,在this JSFiddle中,我有以下代码:

var hot =  null;

initHot();
loadFormulas();
loadData();

function initHot() {
  var container = document.getElementById('example');

  var hCols = [ 'A', 'B', 'C', 'D', 'E' ];
  var hRows = [ '1', '2', '3', '4', '5', '6', '7', '8', '9', '10' ];
  var sdata = [
    [ 0, 0, 0, 0, 0, 0 ],
    [ 0, 0, 0, 0, 0, 0 ],
    [ 0, 0, 0, 0, 0, 0 ],
    [ 0, 0, 0, 0, 0, 0 ],
    [ 0, 0, 0, 0, 0, 0 ],
    [ 0, 0, 0, 0, 0, 0 ],
    [ 0, 0, 0, 0, 0, 0 ],
    [ 0, 0, 0, 0, 0, 0 ],
    [ 0, 0, 0, 0, 0, 0 ],
    [ 0, 0, 0, 0, 0, 0 ]  
  ];

  hot = new Handsontable(container, {
    startRows: hRows.length,
    rowHeaders: hRows,
    startCols: hCols.length,
    colHeaders: hCols,
    data: sdata,
    formulas: true,
    height: 300
  });
}

function loadFormulas() {
  // In my app, this happens async, but I'm just plugging it in here to demo it.
  hot.setDataAtCell(2, 0, '=A1-A2');
  hot.setDataAtCell(9, 0, '=SUM(A3:A9)');
}

function loadData() {
  // In my app, this happens at runtime, but I'm just plugging it in here to demo it.
  hot.setDataAtCell(0, 0, '1000');
  hot.setDataAtCell(1, 0, '3000');

  hot.setDataAtCell(3, 0, '5000');
  hot.setDataAtCell(4, 0, '6500');
  hot.setDataAtCell(5, 0, '7000');
  hot.setDataAtCell(6, 0, '8000');
  hot.setDataAtCell(7, 0, '9000');
  hot.setDataAtCell(8, 0, '10,000');
}

单元格A10应该是值43500.但是,由于单元格A9中的逗号,它计算错误。当逗号存在时,Handsontable将单元格中的值视为零。如果将10,000更改为10000,则会正确计算。如何在值中支持逗号,以使公式仍然正确执行?

1 个答案:

答案 0 :(得分:0)

由于localmachine的区域设置,您的电子表格可能无法正确计算。尝试改变"。"用","或更改日期 - 时间 - 本地计算机上的数字格式。