UNPKG

read-excel-file

Version:

Read small to medium `*.xlsx` files in a browser or Node.js. Parse to JSON with a strict schema.

45 lines (42 loc) 1.39 kB
import { unzipSync, strFromU8 } from 'fflate'; /** * Reads XLSX file in a browser. * @param {(File|Blob|ArrayBuffer)} input - A `File` or an `ArrayBuffer`. * @return {Promise} Resolves to an object holding XLSX file entries. */ export default function unpackXlsxFile(input) { if (input instanceof File) { return input.arrayBuffer().then(unpackXlsxArrayBuffer); } if (input instanceof Blob) { return input.arrayBuffer().then(unpackXlsxArrayBuffer); } return unpackXlsxArrayBuffer(input); } /** * Reads XLSX file in a browser from an `ArrayBuffer`. * @param {ArrayBuffer} input * @return {Promise} Resolves to an object holding XLSX file entries. */ function unpackXlsxArrayBuffer(arrayBuffer) { var archive = new Uint8Array(arrayBuffer); var contents = unzipSync(archive); return Promise.resolve(getContents(contents)); // return new Promise((resolve, reject) => { // unzip(archive, (error, contents) => { // if (error) { // return reject(error) // } // return resolve(getContents(contents)) // }) // }) } function getContents(contents) { var unzippedFiles = []; for (var _i = 0, _Object$keys = Object.keys(contents); _i < _Object$keys.length; _i++) { var key = _Object$keys[_i]; unzippedFiles[key] = strFromU8(contents[key]); } return unzippedFiles; } //# sourceMappingURL=unpackXlsxFileBrowser.js.map