1

I'm using node.js to process my excel sheets with exceljs module.

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 E.g. I have below sheet structure: enter image description here

I'hv written below code to insert values in cells A1 and 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.

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:

enter image description here

How to get computed values or how to trigger computation in excel programatically and to store it in cell?

Output result of cell C1 should be 10 in console i.e. A1+B1 (2+8).

Rupali
  • 1,068
  • 1
  • 12
  • 18
  • Hi s-rupali, i have struggle this question about excel file download can you check my question: https://stackoverflow.com/questions/44451884/excel-file-download-not-working-in-node-js-using-exceljs?noredirect=1#comment75904522_44451884 – Vinoth Jun 09 '17 at 14:17

2 Answers2

4

hot-formula-parser can parse formulas... use in combination with exceljs

Here is the possible connecting code.

davidhq
  • 4,660
  • 6
  • 30
  • 40
2

exceljs allows you to modify the file directly without using 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.

If you open Excel, it will calculte everything and value will be correct.

Nico
  • 13,320
  • 6
  • 32
  • 33
  • Okay, i got your point. So is it possible to compute the value of a cell based on its formula? Is there any possibility with exceljs? Because i have a requirement that i need to write the values in excel and need to commit it in SVN programatically without opening the sheet. So if i'll get the computed values then i'll store it in that particular cell and that'll fix my problem. – Rupali May 27 '17 at 06:12
  • The real question is "should you?" Anyone opening the file with Excel will see the computed value. – Nico May 27 '17 at 08:39
  • 1
    And no I don't think it's possible through exceljs. – Nico May 27 '17 at 08:42
  • I know after opening excel one can see the computed values but in my case no one is going to open the sheet. Sheet will go into SVN, and through Jenkins job I'm inserting computed values in Database. But anyways thanks for your answer. Will figure out some other way :) – Rupali May 28 '17 at 06:49
  • If nobody is opening Excel at the end, Excel is not the good choice of storage. A database is better. – Nico May 29 '17 at 09:45
  • I know, but its a huge old system based on excel computation so i cant change this process. After excel computations we are going to store data in database. Excel is just for generating data to feed in database tables. – Rupali May 29 '17 at 17:34
  • Hi Nico, i have struggle this question about excel file download can you check my question: https://stackoverflow.com/questions/44451884/excel-file-download-not-working-in-node-js-using-exceljs?noredirect=1#comment75904522_44451884 – Vinoth Jun 09 '17 at 14:18