Last active
March 19, 2020 00:59
-
-
Save masatomix/24d340ea25a2745a3ad8fe9f03d6a324 to your computer and use it in GitHub Desktop.
Excel → CSV文字列 → JSON。any[][] を経て csv文字列を愚直に作成。csvtojson で先頭行をPropertyにJSON化。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const XlsxPopulate = require('xlsx-populate') | |
const workbook: any = await XlsxPopulate.fromFileAsync(inputFullPath) | |
const headers: string[] = workbook | |
.sheet(sheetName) | |
.usedRange() | |
.value() | |
.shift() | |
// CSV | |
const valuesArray: any[][] = workbook | |
.sheet(sheetName) | |
.usedRange() | |
.value() | |
const delimiter = String.fromCharCode(31) | |
// const delimiter = ',' | |
const csvString = valuesArray | |
.map((values: any[]) => { | |
return values.reduce((box, column) => { | |
if (column) { | |
return box + delimiter + column | |
} else { | |
return box + delimiter | |
} | |
}) | |
}) | |
.reduce( | |
(box, current) => `${box} | |
${current}`, | |
) | |
const instances = await csv({ delimiter: delimiter }).fromString(csvString) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment