Skip to content

Instantly share code, notes, and snippets.

@masatomix
Last active March 19, 2020 00:59
Show Gist options
  • Save masatomix/a802e1ae2b009c999c264f4d86478edf to your computer and use it in GitHub Desktop.
Save masatomix/a802e1ae2b009c999c264f4d86478edf to your computer and use it in GitHub Desktop.
Excel → JSON。(データ中心)any[][] を経て、ヘッダ部とデータ部を使ってJSON化。各行ごとに、データ部を左から順次走査して、同じIndexのヘッダ部を取得してプロパティにする
const XlsxPopulate = require('xlsx-populate')
// 指定された場合は、一行目の文字列群を使ってプロパティを作成する
const workbook: any = await XlsxPopulate.fromFileAsync(inputFullPath)
const headers: string[] = workbook
.sheet(sheetName)
.usedRange()
.value()
.shift()
const valuesArray: any[][] = workbook
.sheet(sheetName)
.usedRange()
.value()
valuesArray.shift() // 先頭除去
const instances = valuesArray.map((values: any[]) => {
return values.reduce((box: any, column: any, index: number) => {
// 列単位で処理してきて、ヘッダの名前で代入する。
box[headers[index]] = column
return box
}, {})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment