Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

read and update csv file in nodejs

const fs = require('fs')
const csv = require('csv-parser')
const randomWords = require('random-words')
const users = [];
function generateUsername(firstname, surname) {
    return `${firstname[0]}-${surname}`.toLowerCase();
}
fs.createReadStream('input.csv')
  .pipe(csv())
  .on('data', function (row) {
    const username = generateUsername(row.Firstname, row.Surname);
    const password = randomWords(3).join("-");
    
    const user = {
        username,
        firstname: row.Firstname,
        surname: row.Surname,
        roles: row.Roles,
        password
    }
    users.push(user)
  })
  .on('end', function () {
      console.table(users)
      // TODO: SAVE users data to another file
    })
Comment

how to reade selected csv file data in node j s

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" 
    integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
  <link href="https://unpkg.com/tabulator-tables/dist/css/tabulator.min.css" rel="stylesheet">
  <title>Create CSV</title>
</head>
<body>
  <div class="container py-4">
    <h1>Create CSV</h1>
    <h2>Add Columns</h2>
    <input type="text" name="column" id="columnName" class="form-control" placeholder="Column Name" />
    <button class="btn btn-primary mt-1" id="addColumn">Add</button>
    <h2 class="mt-3">Column Data</h2>
    <button class="btn btn-primary mb-3" id="addRow">Add Row</button>
    <div id="csvTable"></div>
    <button class="btn btn-primary mt-3" id="submitForm">Create CSV</button>
  </div>
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" 
    integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
  <script type="text/javascript" src="https://unpkg.com/tabulator-tables/dist/js/tabulator.min.js"></script>
  <script>
    const columnNameInput = document.getElementById('columnName');
    const addColumnButton = document.getElementById('addColumn');
    const addRowButton = document.getElementById('addRow');
    const submitFormButton = document.getElementById('submitForm');

    const table = new Tabulator("#csvTable", {
      height:"300px",
      data: [], //assign data to table
      movableColumns: true, 
      addRowPos: "bottom",
    });

    addColumnButton.addEventListener('click', () => {
      const name = columnNameInput.value ? columnNameInput.value.trim() : '';
      if (!name) {
        alert("Please add a name");
        return;
      }

      table.addColumn({title: name, field: name.toLowerCase(), editableTitle: true, editor: true});
      columnNameInput.value = '';
    });

    addRowButton.addEventListener('click', () => {
      table.addRow({});
    });

    submitFormButton.addEventListener('click', () => {
      const data = table.getData();

      fetch('/create', {
        method: 'POST',
        body: JSON.stringify({
          data
        }),
        headers: {
          'Content-Type': 'application/json'
        }
      })
      .then((response) => response.blob())
      .then((blob) => {
        const fileURL = URL.createObjectURL(blob)
        const a = document.createElement('a')
        a.href = fileURL
        a.download = "file.csv"
        a.click()
      })
      .catch((e) => {
        console.error(e)
        alert(e.message)
      })
    })
  </script>
</body>
</html>
Comment

how to read a csv file in nodejs

$ npm i -s csv-parser
Comment

how to read a csv file in nodejs

$ npm i -s csv-parse
Comment

PREVIOUS NEXT
Code Example
Javascript :: contains duplicate leetcode solution javascript 
Javascript :: javascript date to string format dd mmm yyyy 
Javascript :: react native create apk 
Javascript :: discord.js checking channel permissions 
Javascript :: Moment.js: Date between dates 
Javascript :: date.parse string to javascript 
Javascript :: reducer in react example 
Javascript :: element clicked js 
Javascript :: how to push items in array in javascript 
Javascript :: javascript merge arrays of objects without duplicates 
Javascript :: how to make dynamic title for screen in react native 
Javascript :: how to hide ascending descending icons in datatable js 
Javascript :: reactjs dynamic route 
Javascript :: javascript create node from innerhtml 
Javascript :: node js currency format 
Javascript :: export gcp credentials json file 
Javascript :: discord.js start code 
Javascript :: get query params react 
Javascript :: delay in javascript without await 
Javascript :: how to append data to a field in mongoose model 
Javascript :: how can i convert object to an array javascript 
Javascript :: puppeteer set download path 
Javascript :: npm run build serve 
Javascript :: grepper extension firefox 
Javascript :: java json string to map 
Javascript :: milliseconds to date javascript 
Javascript :: javascript toggle button 
Javascript :: open folder node js 
Javascript :: jquery validator add method 
Javascript :: react native custom debounce input 
ADD CONTENT
Topic
Content
Source link
Name
3+4 =