mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-19 04:56:00 +00:00
excel import fix
This commit is contained in:
@@ -2,10 +2,20 @@ const exceljs = require('exceljs');
|
||||
const stream = require('stream');
|
||||
const _ = require('lodash');
|
||||
|
||||
async function excelSheetReader({ fileName, sheetName }) {
|
||||
const loadedWorkbooks = {};
|
||||
|
||||
async function loadWorkbook(fileName) {
|
||||
let workbook = loadedWorkbooks[fileName];
|
||||
if (workbook) return workbook;
|
||||
console.log(`Loading excel ${fileName}`);
|
||||
const workbook = new exceljs.Workbook();
|
||||
workbook = new exceljs.Workbook();
|
||||
await workbook.xlsx.readFile(fileName);
|
||||
loadedWorkbooks[fileName] = workbook;
|
||||
return workbook;
|
||||
}
|
||||
|
||||
async function excelSheetReader({ fileName, sheetName }) {
|
||||
const workbook = await loadWorkbook(fileName);
|
||||
const sheet = workbook.getWorksheet(sheetName);
|
||||
|
||||
const pass = new stream.PassThrough({
|
||||
@@ -18,7 +28,9 @@ async function excelSheetReader({ fileName, sheetName }) {
|
||||
pass.write(structure);
|
||||
for (let rowIndex = 2; rowIndex <= sheet.rowCount; rowIndex++) {
|
||||
const row = sheet.getRow(rowIndex);
|
||||
pass.write(_.fromPairs(structure.columns.map((col, index) => [col.columnName, row.getCell(index + 1).value])));
|
||||
const rowData = _.fromPairs(structure.columns.map((col, index) => [col.columnName, row.getCell(index + 1).value]));
|
||||
if (_.isEmpty(_.omitBy(rowData, (v) => v == null || v.toString().trim().length == 0))) continue;
|
||||
pass.write(rowData);
|
||||
}
|
||||
pass.end();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user