Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created October 17, 2023 05:08
Show Gist options
  • Save code-boxx/08420b9aff7e6a02d69e5fc1cb8c9fe1 to your computer and use it in GitHub Desktop.
Save code-boxx/08420b9aff7e6a02d69e5fc1cb8c9fe1 to your computer and use it in GitHub Desktop.
NodeJS Excel Add Rows

NODEJS ADD NEW ROWS TO EXCEL FILE

https://code-boxx.com/nodejs-add-new-rows-excel-file/

NOTES

  1. Run npm install xlsx
  2. GIST does not allow Excel files. Convert x-dummy.csv to x-dummy.xlsx on your own, or use your own dummy Excel file.

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

// (A) LOAD SHEETJS
const xlsx = require("xlsx");
// (B) LOAD EXISTING WORKSHEET
var workbook = xlsx.readFile("x-dummy.xlsx"),
worksheet = workbook.Sheets[workbook.SheetNames[0]];
// (C) APPEND ROW
xlsx.utils.sheet_add_aoa(worksheet, [["NEW", "ROW"]], {origin: -1});
// (D) SAVE UPDATED
xlsx.writeFile(workbook, "demoA.xlsx");
// (A) LOAD SHEETJS
const xlsx = require("xlsx");
// (B) LOAD EXISTING WORKSHEET
var workbook = xlsx.readFile("x-dummy.xlsx"),
worksheet = workbook.Sheets[workbook.SheetNames[0]];
data = xlsx.utils.sheet_to_json(worksheet, {header: 1})
// (C) PREPEND NEW ROW
delete(worksheet); delete(workbook);
data.splice(0, 0, ["NEW", "ROW"]);
// (D) SAVE UPDATED
workbook = xlsx.utils.book_new();
worksheet = xlsx.utils.aoa_to_sheet(data),
xlsx.utils.book_append_sheet(workbook, worksheet);
xlsx.writeFile(workbook, "demoB.xlsx");
// (A) LOAD SHEETJS
const xlsx = require("xlsx");
// (B) LOAD EXISTING WORKSHEET
var workbook = xlsx.readFile("x-dummy.xlsx"),
worksheet = workbook.Sheets[workbook.SheetNames[0]];
data = xlsx.utils.sheet_to_json(worksheet, {header: 1})
// (C) INSERT AT EXACT ROW
delete(worksheet); delete(workbook);
data.splice(2, 0, ["NEW", "ROW"]);
// (D) SEARCH FOR "INSERT LOCATION"
var at = null;
for (let [i,r] of Object.entries(data)) {
if (r.includes("Jon Doe")) { at = i; break; }
}
if (at !== null) { data.splice(at, 0, ["ANOTHER", "ROW"]); }
// (E) SAVE UPDATED
workbook = xlsx.utils.book_new();
worksheet = xlsx.utils.aoa_to_sheet(data),
xlsx.utils.book_append_sheet(workbook, worksheet);
xlsx.writeFile(workbook, "demoC.xlsx");
Joa Doe joa@doe.com
Job Doe job@doe.com
Joe Doe joe@doe.com
Joi Doe joi@doe.com
Jon Doe jon@doe.com
Joy Doe joy@doe.com
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment