1

I created a code that inputs data into an excel spreadsheet. For example: I have Sheet1 (which will receive data via code) and Sheet2 (which has dozens of formulas). For illustration, on Sheet2 there is the following sum: =sum(Sheet1B2:B3). The problem lies in the following. Excel only calculates the result after opening the spreadsheet and if I try to import the sum result before opening, saving and closing the spreadsheet the values ​​are null and zero.

In Python I was able to solve this problem by opening and closing the spreadsheet, but in Nodejs it didn't work, since the libraries used, such as xlsxpopulate, xlsx, ExcelJS, didn't support doing this. I also tried some instructions like recalculating formulas but without success. The code in python is:

import xlwings as xl
 app = xl.App(visible = False)
        book = app.books.open(os.path.join(path, 'test.xlsx'))
        book.save()
        app.kill()

Making what I want very clear: I would like the nodejs code to open, save and close the file in xlsx or when inputting the data in excel to calculate the values ​​of the formulas so that they can be imported into another file. To input the data in Excel, I use a code similar to this:

const ExcelJS = require('exceljs');

async function insertDFExcel(archive, df) {
    const workbook = new ExcelJS.Workbook();

    try {        
        await workbook.xlsx.readFile(arquivo);

        const worksheet = workbook.getWorksheet('Sheet2'); 

        
        df.forEach((row) => {
            worksheet.addRow(row);
        });
        
        await workbook.xlsx.writeFile(archive);
        console.log('Ok.');
    } catch (error) {
        console.error('Error:', error);
    }
}

const df = [
    ['Name', 'Age'],
    ['João', 30],
    ['Maria', 25]
];

const archive = 'path/archive.xlsx';

// 
insertDFExcel(archive, df);

2
  • Sounds like you want to open actual Microsoft Excel, so it can handle the update, is that it? In this case you could use win32ole to access and automate Excel through its COM interface. Commented Apr 23, 2024 at 16:35
  • Yes, it was a trick I found, considering that I didn't find a way to make it calculate the formulas automatically without opening it. Commented Apr 23, 2024 at 16:46

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.