mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-17 22:36:01 +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();
|
||||
|
||||
|
||||
17
test/importUcet.js
Normal file
17
test/importUcet.js
Normal file
@@ -0,0 +1,17 @@
|
||||
const dbgateApi = require('@dbgate/api');
|
||||
|
||||
async function run() {
|
||||
const var3 = await dbgateApi.excelSheetReader({
|
||||
fileName: '/home/jena/google-drive/Metrostav/e2-ciselniky/Číselníky 2. etapa - v01.xlsx',
|
||||
sheetName: 'Ucet',
|
||||
});
|
||||
const var4 = await dbgateApi.tableWriter({
|
||||
connection: { server: 'localhost', engine: 'mssql', user: 'sa', password: 'Pwd2020Db', database: 'Importy' },
|
||||
pureName: 'Ucet',
|
||||
createIfNotExists: true,
|
||||
dropIfExists: true,
|
||||
});
|
||||
await dbgateApi.copyStream(var3, var4);
|
||||
}
|
||||
|
||||
dbgateApi.runScript(run);
|
||||
Reference in New Issue
Block a user