28 lines
1.0 KiB
TypeScript
28 lines
1.0 KiB
TypeScript
import * as XLSX from 'xlsx'
|
|
|
|
const codeHeaders = new Set(['编码', '资产编码', '设备编码', '资产编号', '设备编号', '资产代码', '设备代码'])
|
|
|
|
export const parseDisposalImport = (source: ArrayBuffer | string): string[] => {
|
|
const workbook = XLSX.read(source, { type: typeof source === 'string' ? 'string' : 'array', raw: true })
|
|
const codes: string[] = []
|
|
|
|
for (const [index, name] of workbook.SheetNames.entries()) {
|
|
if (workbook.Workbook?.Sheets?.[index]?.Hidden) continue
|
|
const rows = XLSX.utils.sheet_to_json<string[]>(workbook.Sheets[name], { header: 1, raw: false, defval: '' })
|
|
for (const [rowIndex, row] of rows.entries()) {
|
|
const columns = row.flatMap((cell, index) => codeHeaders.has(cell.replace(/\s/g, '')) ? [index] : [])
|
|
if (!columns.length) continue
|
|
|
|
for (const dataRow of rows.slice(rowIndex + 1)) {
|
|
for (const column of columns) {
|
|
const code = dataRow[column]?.trim()
|
|
if (code) codes.push(code)
|
|
}
|
|
}
|
|
break
|
|
}
|
|
}
|
|
|
|
return codes
|
|
}
|