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);
win32oleto access and automate Excel through its COM interface.