excel

Docs › Functions and Node

Using it in Node

Check files and read rows from a Buffer on a server or in a script.

The 82excel entry uses no DOM, so it imports in Node as is. A Buffer from fs.readFile is a Uint8Array and goes straight in. File path strings are not accepted.

import { readFile } from 'node:fs/promises';
import { check, readRows } from '82excel';

const bytes = await readFile('./customers.xlsx');          // a Buffer is a Uint8Array
const result = await check(bytes, { headers: ['이름', '전화번호', '금액'], rows: { min: 1 } });
console.log(result.ok, result.errors);                      // true []

const { rows } = await readRows(bytes);
console.log(rows[0]);                                       // { 이름: '홍길동', 전화번호: '010-1234-5678', 금액: 5000 }

const wrong = await check(bytes, { headers: ['A', 'B'] });
console.log(wrong.errors.map((e) => `${e.code}: ${e.message}`));
// [ 'COLUMN_COUNT: Expected 2 columns, found 3',
//   'HEADER_MISMATCH: Column A: expected "A", found "이름"',
//   'HEADER_MISMATCH: Column B: expected "B", found "전화번호"' ]

Check again on the upload server

Even when <excel-82> passed a file in the browser, check it once more on the server. The same rules give the same result.

// Check again on the server that receives the upload (e.g. Express + multer)
import '82excel/locales/ko';
import { check } from '82excel';

app.post('/api/upload', upload.single('file'), async (req, res) => {
  const r = await check(req.file.buffer, { headers: ['이름', '전화번호', '금액'], locale: 'ko' });
  if (!r.ok) return res.status(422).json(r.errors);
  res.send('ok');
});

Options and limits are in Function API.