Hi,
I am using your package for a project. It is great but I have a quick question: How do you add a row to an existing excel document please?
If you want to add a row to the end of a worksheet, there are two helper functions sheet_add_aoa and sheet_add_json.
Calling either function with the option origin:-1 adds to the end of the worksheet:
XLSX.utils.sheet_add_aoa(worksheet, [
["new data", 1, 2, 3]
], {origin: -1});
Here's a sample fiddle which fetches an XLS file, adds a new row to the first worksheet and exports to XLSX: https://jsfiddle.net/sheetjs/y459abLf/
Fiddle code (click to show)
fetch('https://crossorigin.me/https://obamawhitehouse.archives.gov/sites/default/files/omb/budget/fy2014/assets/receipts.xls').then(r => r.arrayBuffer()).then(r => {
var wb = XLSX.read(r, {type:"array"});
var ws = wb.Sheets[wb.SheetNames[0]];
XLSX.utils.sheet_add_aoa(ws, [
["new data", 1, 2, 3]
], {origin: -1});
XLSX.writeFile(wb, 'new.xlsx');
});
Hi,
Thanks for your answer. I tried to use this function but I have this error message: TypeError: XLSX.utils.sheet_add_aoa is not a function
Update to the latest version
If you want to add a row to the end of a worksheet, there are two helper functions
sheet_add_aoaandsheet_add_json.Calling either function with the option
origin:-1adds to the end of the worksheet:XLSX.utils.sheet_add_aoa(worksheet, [ ["new data", 1, 2, 3] ], {origin: -1});Here's a sample fiddle which fetches an XLS file, adds a new row to the first worksheet and exports to XLSX: https://jsfiddle.net/sheetjs/y459abLf/
Fiddle code (click to show)
how to insert rows at the start of a sheet , do not replace data?
just set {origin:"A1"} instead of {origin: -1}
sheet_add_aoa or sheet_add_json adds rows to the end of the sheet. How to add rows to start of the sheet after exporting table_to_sheet. need help.
Most helpful comment
If you want to add a row to the end of a worksheet, there are two helper functions
sheet_add_aoaandsheet_add_json.Calling either function with the option
origin:-1adds to the end of the worksheet:Here's a sample fiddle which fetches an XLS file, adds a new row to the first worksheet and exports to XLSX: https://jsfiddle.net/sheetjs/y459abLf/
Fiddle code (click to show)