Reputation: 355
I'm working on a NodeJS project and I need to modify an existing Excel spreadsheet but without changing the original format or links, just adding values to a couple of cells. Currently I've been using exceljs
which works very well when it comes to modify the spreadsheet but the output removes all the original styles, formats and links.
Is there another javascript npm or library that can help me with this issue or is it possible with exceljs
?
Here's what I have so far:
Fetch('url of the file')
.then(res => res.buffer())
.then(buffer => {
var workbook = new Excel.Workbook();
workbook.xlsx.load(buffer).then(function() {
var worksheet = workbook.getWorksheet(1);
var row = worksheet.getRow(4);
row.getCell(2).value = 'It works!';
row.commit();
workbook.xlsx.write(stream)
.then(function() {
// done
// here I take the stream and upload it to an AWS S3 bucket
});
});
})
Thanks!
Upvotes: 4
Views: 8261
Reputation: 89
For those looking for a TypeScript version of this ...
import { fromFileAsync } from 'xlsx-populate';
...
fromFileAsync('input.xlsx').then(workbook => {
let ws=workbook.sheet("Sheet1");
ws.cell("C1").value("value in C1");
ws.cell("C2").value("value in C2");
return workbook.toFileAsync('input.xlsx');
});
Hope it's useful to someone. 😊
Upvotes: 1
Reputation: 355
I found another npm
that does it great! It's xlsx-populate.
Here's the same code that used with exceljs
to do the same job:
Fetch('file url')
.then(res => res.buffer())
.then(buffer => {
XlsxPopulate.fromDataAsync(buffer)
.then(workbook => {
// Make edits.
workbook.sheet(0).cell("A1").value("foo");
// Get the output
return workbook.outputAsync();
})
.then(data => {
// upload data to AWS S3
})
.catch(err => console.error(err));
});
Hope this helps to someone going through the same case :)
Upvotes: 6