Skip to content

Instantly share code, notes, and snippets.

@masatomix
Last active March 19, 2020 00:59
Show Gist options
  • Save masatomix/24d340ea25a2745a3ad8fe9f03d6a324 to your computer and use it in GitHub Desktop.
Save masatomix/24d340ea25a2745a3ad8fe9f03d6a324 to your computer and use it in GitHub Desktop.
Excel → CSV文字列 → JSON。any[][] を経て csv文字列を愚直に作成。csvtojson で先頭行をPropertyにJSON化。
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