简体   繁体   English

Exceljs - 公式不会根据单元格的 position 更新

[英]Exceljs - Formulas do not get updated depending on position of cells

I have a Excel file where I need to enter certain values from a JSON file.我有一个 Excel 文件,我需要从 JSON 文件中输入某些值。 The excel sheet has pre-defined formulas that will use the values I will enter from JSON file using Exceljs. excel 工作表具有预定义的公式,这些公式将使用我将使用 Exceljs 从 JSON 文件中输入的值。

Now I know that Exceljs doesn't do calculation.现在我知道 Exceljs 不进行计算。 But here's the problem.但这就是问题所在。 Let's take an example to understand better.让我们举个例子来更好地理解。

A1 = 5 A1 = 5

B1 = 4 B1 = 4

C1 = 7 C1 = 7

E1 = (A1+B1+C1) / 3 ----> which means the value of E1 = 5.33 E1 = (A1+B1+C1) / 3 ---->表示 E1 的值 = 5.33

Excel (or any spreadsheet really) gives us the convenience of dragging a formula cell and spreading it down the column and the formula automatically changes to, say, Excel(或任何电子表格)为我们提供了拖动公式单元格并将其沿列向下展开的便利,公式会自动更改为,例如,

E2 = (A2+B2+C2) / 3 E2 = (A2+B2+C2) / 3

E3 = (A3+B3+C3) / 3 E3 = (A3+B3+C3) / 3

E4 = (A4+B4+C4) / 3 and so on... E4 = (A4+B4+C4) / 3以此类推...

But, when I access these cells in exceljs, the formula they show is,但是,当我在 exceljs 中访问这些单元格时,它们显示的公式是,

E2 = (A1+B1+C1) / 3 E2 = (A1+B1+C1) / 3

E3 = (A1+B1+C1) / 3 E3 = (A1+B1+C1) / 3

And their shareType is set to shared.并且他们的 shareType 设置为 shared。

Is there anyway to update these shared formula depending on position of cell.无论如何根据单元格的 position 更新这些共享公式。

These cells only get updated to correct formula when I open the file in excel and then very intentionally click on the formula and accept the formula.只有当我在 excel 中打开文件然后非常有意地单击公式并接受公式时,这些单元格才会更新为正确的公式。 Then the formula is recognized by exceljs correctly and the formula is not 'shared' anymore in the shareType.然后exceljs正确识别公式,并且shareType中的公式不再“共享”。

Any feedback or help here will be appreciated.任何反馈或帮助将不胜感激。 If you know of another library which can recognize these kind of shared formula will also be helpful.如果您知道另一个可以识别这些共享公式的库也会有所帮助。 Thanks.谢谢。

try creating each formula manually, and also setting cell reference while iterating data:尝试手动创建每个公式,并在迭代数据时设置单元格引用:

for (let i = 1; i <= 4; i++) {

    worksheet.getCell(`E${i}`).value = {
        formula: `(A${i}+B${i}+C${i})/3`
    };
    
}

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

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