Sheetjs: Add new row to Exel

Created on 9 Apr 2018  路  6Comments  路  Source: SheetJS/sheetjs

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?

Most helpful comment

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');
});

All 6 comments

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_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)

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

magtuan picture magtuan  路  3Comments

happy0088 picture happy0088  路  3Comments

Alex0007 picture Alex0007  路  3Comments

upasana-shah picture upasana-shah  路  4Comments

jamesbillinger picture jamesbillinger  路  4Comments