Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active November 9, 2023 02:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/c5c401e0a9c197d400fbf0b059d7c297 to your computer and use it in GitHub Desktop.
Save code-boxx/c5c401e0a9c197d400fbf0b059d7c297 to your computer and use it in GitHub Desktop.
NodeJS Add New Rows To CSV File

NODEJS ADD NEW ROWS TO CSV

https://code-boxx.com/nodejs-csv-add-rows/

NOTES

Run unpack.bat (Windows) unpack.sh (Mac/Linux), this will automatically:

  • Install csv-stringify csv-parser
  • Run all the examples.

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 MODULES
// npm install csv-stringify
// https://www.npmjs.com/package/csv-stringify
const fs = require("fs"),
csvs = require("csv-stringify");
// (B) DATA TO APPEND
var data = [["A", "B"]];
// (C) CREATE CSV FILE
csvs.stringify(data, (err, output) => {
fs.appendFileSync("x-dummy.csv", output);
console.log("OK");
});
// (A) LOAD MODULES
// npm install csv-stringify csv-parser
// https://www.npmjs.com/package/csv-stringify
// https://www.npmjs.com/package/csv-parser
const fs = require("fs"),
csvp = require("csv-parser"),
csvs = require("csv-stringify");
// (B) READ CSV FILE
var rows = [];
fs.createReadStream("x-dummy.csv")
.pipe(csvp({ headers : false }))
.on("data", data => rows.push(data))
.on("end", () => {
// (C) REARRANGE TO FLAT ARRAY
rows.forEach((r,i) => rows[i] = Object.values(r));
// (D) PREPEND
rows.splice(0, 0, ["C", "D"]);
// (E) WRITE
csvs.stringify(rows, (err, output) => {
fs.writeFileSync("x-dummy.csv", output);
console.log("OK");
});
});
// (A) LOAD MODULES
// npm install csv-stringify csv-parser
// https://www.npmjs.com/package/csv-stringify
// https://www.npmjs.com/package/csv-parser
const fs = require("fs"),
csvp = require("csv-parser"),
csvs = require("csv-stringify");
// (B) READ CSV FILE
var rows = [];
fs.createReadStream("x-dummy.csv")
.pipe(csvp({ headers : false }))
.on("data", data => rows.push(data))
.on("end", () => {
// (C) REARRANGE TO FLAT ARRAY
rows.forEach((r,i) => rows[i] = Object.values(r));
// (D) INSERT AT EXACT ROW
rows.splice(3, 0, ["E", "F"]);
// (E) SEARCH & INSERT AT ROW
let at = null;
for (let [i,r] of Object.entries(rows)) {
if (r.includes("Jou Doe")) { at = i; break; }
}
if (at !== null) { rows.splice(at, 0, ["G", "H"]); }
// (F) WRITE
csvs.stringify(rows, (err, output) => {
fs.writeFileSync("x-dummy.csv", output);
console.log("OK");
});
});
call npm i csv-stringify csv-parser
node 1-append.js
node 2-prepend.js
node 3-insert.js
source "npm i csv-stringify csv-parser"
node ./1-append.js
node ./2-prepend.js
node ./3-insert.js
Job Doe job@doe.com
Joe Doe joe@doe.com
Joi Doe joi@doe.com
Jon Doe jon@doe.com
Jou Doe jou@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