fix poor naming formats and typo in UploadExcel component (#1047)

This commit is contained in:
Shaw 2018-09-04 18:09:01 +08:00 committed by 花裤衩
parent a5dd905b46
commit 6ecf8b820a
1 changed files with 11 additions and 9 deletions

View File

@ -26,7 +26,7 @@ export default {
} }
}, },
methods: { methods: {
generateDate({ header, results }) { generateData({ header, results }) {
this.excelData.header = header this.excelData.header = header
this.excelData.results = results this.excelData.results = results
this.onSuccess && this.onSuccess(this.excelData) this.onSuccess && this.onSuccess(this.excelData)
@ -82,20 +82,20 @@ export default {
const reader = new FileReader() const reader = new FileReader()
reader.onload = e => { reader.onload = e => {
const data = e.target.result const data = e.target.result
const fixedData = this.fixdata(data) const fixedData = this.fixData(data)
const workbook = XLSX.read(btoa(fixedData), { type: 'base64' }) const workbook = XLSX.read(btoa(fixedData), { type: 'base64' })
const firstSheetName = workbook.SheetNames[0] const firstSheetName = workbook.SheetNames[0]
const worksheet = workbook.Sheets[firstSheetName] const worksheet = workbook.Sheets[firstSheetName]
const header = this.get_header_row(worksheet) const header = this.getHeaderRow(worksheet)
const results = XLSX.utils.sheet_to_json(worksheet) const results = XLSX.utils.sheet_to_json(worksheet)
this.generateDate({ header, results }) this.generateData({ header, results })
this.loading = false this.loading = false
resolve() resolve()
} }
reader.readAsArrayBuffer(rawFile) reader.readAsArrayBuffer(rawFile)
}) })
}, },
fixdata(data) { fixData(data) {
let o = '' let o = ''
let l = 0 let l = 0
const w = 10240 const w = 10240
@ -103,14 +103,16 @@ export default {
o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w))) o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w)))
return o return o
}, },
get_header_row(sheet) { getHeaderRow(sheet) {
const headers = [] const headers = []
const range = XLSX.utils.decode_range(sheet['!ref']) const range = XLSX.utils.decode_range(sheet['!ref'])
let C let C
const R = range.s.r /* start in the first row */ const R = range.s.r
/* start in the first row */
for (C = range.s.c; C <= range.e.c; ++C) { /* walk every column in the range */ for (C = range.s.c; C <= range.e.c; ++C) { /* walk every column in the range */
var cell = sheet[XLSX.utils.encode_cell({ c: C, r: R })] /* find the cell in the first row */ const cell = sheet[XLSX.utils.encode_cell({ c: C, r: R })]
var hdr = 'UNKNOWN ' + C // <-- replace with your desired default /* find the cell in the first row */
let hdr = 'UNKNOWN ' + C // <-- replace with your desired default
if (cell && cell.t) hdr = XLSX.utils.format_cell(cell) if (cell && cell.t) hdr = XLSX.utils.format_cell(cell)
headers.push(hdr) headers.push(hdr)
} }