简体   繁体   English

获取Node.js中Excel工作表单元格的计算值

[英]get computed value of excel sheet cell in Node.js

I'm using node.js to process my excel sheets with exceljs module. 我正在使用node.js和exceljs模块处理我的excel表。

I'm writing values into few cells and other cells are already containing formulas. 我正在将值写入几个单元格,而其他单元格已经包含公式。 I want to trigger those formulas and want to store values in sheet programatically. 我想触发这些公式,并希望以编程方式将值存储在工作表中。

For Eg I have below sheet structure: 例如,我具有以下工作表结构: 在此处输入图片说明

I'hv written below code to insert values in cells A1 and B1. 我用下面的代码在单元格A1和B1中插入值。

var workbook = new Excel.Workbook();
workbook.xlsx.readFile(__base + 'test.xlsx')
  .then(function() {
    var worksheet = workbook.getWorksheet(2);
    var row = worksheet.getRow(1);
    row.getCell(1).value = 2; // A1's value set to 2
    row.getCell(2).value = 8; // B1's value set to 8
  };
  row.commit();
  workbook.xlsx.writeFile(__base + 'test.xlsx');                            
});

When I'm trying to fetch value of c1 then its returning me formula with result 0. Below is the code to fetch values. 当我尝试获取c1的值时,它将返回结果为0的公式。下面是获取值的代码。

workbook.xlsx.readFile(__base + 'test.xlsx')
    .then(function() {
    var worksheet = workbook.getWorksheet(2);
    var row = worksheet.getRow(1);
    console.log(row.getCell(1).value);
    console.log(row.getCell(2).value);
    console.log(row.getCell(3).value);
});

OUTPUT: OUTPUT:

在此处输入图片说明

How to get computed values or how to trigger computation in excel programatically and to store it in cell? 如何获取计算值或如何以编程方式在excel中触发计算并将其存储在单元格中?

Output result of cell C1 should be 10 in console ie A1+B1 (2+8). 在控制台中,单元格C1的输出结果应为10,即A1 + B1(2 + 8)。

hot-formula-parser can parse formulas... use in combination with exceljs hot-formula-parser可以解析公式...与exceljs结合使用

Here is the possible connecting code . 这是可能的连接代码

exceljs allows you to modify the file directly without using Excel. exceljs允许您直接修改文件,而无需使用Excel。
There won't be any calcultation done at the file level, the result value will be the latest calculted by Excel last time you opened it. 在文件级别不会进行任何煅烧,结果值将是您上次打开Excel时最新的煅烧。

If you open Excel, it will calculte everything and value will be correct. 如果您打开Excel,它将计算一切,并且值将是正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM