Last active
March 19, 2020 00:58
-
-
Save masatomix/f985ab4a3236070f2fea87a971939b5b to your computer and use it in GitHub Desktop.
Excel → JSON。(ヘッダ中心)。any[][] を経て、ヘッダ部とデータ部を使ってJSON化。各行ごとに、ヘッダ部を左から順次走査して、同じIndexのデータ部を取得してプロパティにする
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() | |
const valuesArray: any[][] = workbook | |
.sheet(sheetName) | |
.usedRange() | |
.value() | |
valuesArray.shift() // 先頭除去 | |
const instances = valuesArray.map((values: any[]) => { | |
// return headers.reduce((box: any, header, index) => { | |
// console.log(header) | |
// console.log(values[index]) | |
// box[header] = values[index] | |
// return box | |
// }, {}) | |
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