forked from mgcrea/node-xlsx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
32 lines (29 loc) · 1.16 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import XLSX from 'xlsx';
import bufferFrom from 'buffer-from';
import {buildSheetFromMatrix, isString} from './helpers';
import Workbook from './workbook';
export const parse = (mixed, options = {}) => {
const workSheet = XLSX[isString(mixed) ? 'readFile' : 'read'](mixed, options);
return Object.keys(workSheet.Sheets).map((name) => {
const sheet = workSheet.Sheets[name];
return {name, data: XLSX.utils.sheet_to_json(sheet, {header: 1, raw: options.raw !== false})};
});
};
export const build = (worksheets, options = {}) => {
const defaults = {
bookType: 'xlsx',
bookSST: false,
type: 'binary'
};
const workBook = new Workbook();
worksheets.forEach((worksheet) => {
const sheetName = worksheet.name || 'Sheet';
const sheetOptions = worksheet.options || {};
const sheetData = buildSheetFromMatrix(worksheet.data || [], {...options, ...sheetOptions});
workBook.SheetNames.push(sheetName);
workBook.Sheets[sheetName] = sheetData;
});
const excelData = XLSX.write(workBook, Object.assign({}, defaults, options));
return excelData instanceof Buffer ? excelData : bufferFrom(excelData, 'binary');
};
export default {parse, build};