const xlsx = require('xlsx'); //const mysql = require('mysql2/promise'); const DATAPATH = "/home/smnc/Documents/egovc/xlsx_snippet/data" const OUTPUTPATH = "/home/smnc/Documents/egovc/xlsx_snippet/output/" const fs = require('fs'); const path = require('path'); function findExcelFiles(directoryPath) { fs.readdir(directoryPath, function(err, files) { if (err) { console.error("Error reading directory:", err); return; } files.forEach(function(file) { const filePath = path.join(directoryPath, file); fs.stat(filePath, function(err, stats) { if (err) { console.error("Error reading file stats:", err); return; } if (stats.isDirectory()) { findExcelFiles(filePath); // Recursively search subdirectories } else { if (path.extname(filePath).toLowerCase() === '.xlsx' || path.extname(filePath).toLowerCase() === '.xls') { console.log('<----------------------------------------NEW ORGANIZATION---------------------------------------->') console.log("Found Excel file:", filePath); const parentDirName = path.basename(path.dirname(filePath)); console.log("Parent directory:", parentDirName); // Do whatever you want with the file path here let database = {} updateAll(database, filePath); writeObjectToJson(database, parentDirName, OUTPUTPATH); } } }); }); }); } function main(directoryPath) { findExcelFiles(directoryPath); } main(DATAPATH); function updateDatabaseFromExcel(filePath, sheetName, startRow, startCol, endCol, endRow = undefined) { // Read Excel file const workbook = xlsx.readFile(filePath); const worksheet = workbook.Sheets[sheetName]; // Determine the last row with data in the specified columns if (endRow === undefined){ endRow = startRow; while (worksheet[xlsx.utils.encode_cell({ r: endRow + 1, c: startCol })] !== undefined) { endRow++; } } // Extract data from the determined range in Excel sheet const data = []; for (let row = startRow; row <= endRow; row++) { const rowData = []; for (let col = startCol; col <= endCol; col++) { const cellAddress = xlsx.utils.encode_cell({ r: row, c: col }); rowData.push(worksheet[cellAddress]?.v); } data.push(rowData); } return data; } // Replace with your filepath //const excelFilePath = '/home/smnc/Documents/egovc/xlsx_snippet/HWPAdigital - OMNIA - Abfrage der Onboarding-Daten.xlsx'; //async function updateDatabaseFromExcel(filePath, sheetName, startRow, startCol, endCol, endRow = undefined) function updateDatabaseSachbearbeiter(database, excelFilePath) { console.log('<--------------------Sachbearbeiter-------------------->') sheetName = 'Sachbearbeiter'; startRow = 12; startCol = 2; endCol = 7; data = updateDatabaseFromExcel(excelFilePath, sheetName, startRow, startCol, endCol); database.users = data; console.log(data) } function updateDatabaseOrganisatsionsdaten(database, excelFilePath) { console.log('<--------------------Organisationsdaten-------------------->') const sheetName = 'Organisationsdaten'; const startRow = 12; const startCol = 2; const endCol = 4; const endRow = 30; const data = updateDatabaseFromExcel(excelFilePath, sheetName, startRow, startCol, endCol, endRow); const Organisationsname = data[0][2]; const Bezeichnung = data[2][2]; const Postleitzahl = data[4][2]; const address = data[6][2]; const phoneNumber = data[8][2]; const email = data[10][2]; const demail = data[12][2]; const internetadresse = data[14][2]; const datenschutzURL = data[16][2]; const impressumURL = data[18][2]; console.log('Updated database with the following group info: ') console.log('Organisationsname: ', Organisationsname) console.log('Bezeichnung: ', Bezeichnung); console.log('Postleitzahl: ', Postleitzahl); console.log('address: ', address); console.log('phoneNumber: ', phoneNumber); console.log('email: ', email); console.log('demail: ', demail); console.log('internetadresse', internetadresse); console.log('datenschutzURL', datenschutzURL); console.log('impressumURL', impressumURL); // Add extracted variables to the database object database.Organisationsname = Organisationsname; database.Bezeichnung = Bezeichnung; database.Postleitzahl = Postleitzahl; database.address = address; database.phoneNumber = phoneNumber; database.email = email; database.demail = demail; database.internetadresse = internetadresse; database.datenschutzURL = datenschutzURL; database.impressumURL = impressumURL; } function updateDatabaseKeyUser(database, excelFilePath) { console.log('<--------------------KeyUser-------------------->') const sheetName = 'Key-User'; const startRow = 12; const startCol = 2; const endCol = 4; const endRow = 24; const data = updateDatabaseFromExcel(excelFilePath, sheetName, startRow, startCol, endCol, endRow); const Vorname = data[0][2]; const Name = data[2][2]; const Titel = data[4][2]; const email = data[6][2]; const phoneNumber = data[8][2]; const Abteilung = data[10][2]; const Stellenbezeichnung = data[12][2]; console.log('Updated database with the following Key User info: ') console.log('Vorname: ', Vorname) console.log('Name: ', Name); console.log('Titel: ', Titel); console.log('email: ', email); console.log('phoneNumber: ', phoneNumber); console.log('Abteilung: ', Abteilung); console.log('Stellenbezeichnung', Stellenbezeichnung); console.log(''); // Add extracted variables to the database object database.Vorname = Vorname; database.Name = Name; database.Titel = Titel; database.email = email; database.phoneNumber = phoneNumber; database.Abteilung = Abteilung; database.Stellenbezeichnung = Stellenbezeichnung; } function updateDatabaseBankverbindung(database, excelFilePath) { console.log('<--------------------BankVerbindung-------------------->') const sheetName = 'Bankverbindung'; const startRow = 12; const startCol = 2; const endCol = 4; const endRow = 20; const data = updateDatabaseFromExcel(excelFilePath, sheetName, startRow, startCol, endCol, endRow); const IBAN = data[0][2]; const BIC = data[2][2]; const bankName = data[4][2]; const Steuernummer = data[6][2]; const schlussel = data[8][2]; console.log('Updated database with the following BankVerbindung info: ') console.log('IBAN: ', IBAN); console.log('BIC: ', BIC); console.log('bankName: ', bankName); console.log('Steuernummer: ', Steuernummer); console.log('Amtlicher Gemeindeschlüssel: ', schlussel); console.log(''); // Add extracted variables to the database object database.IBAN = IBAN; database.BIC = BIC; database.bankName = bankName; database.Steuernummer = Steuernummer; database.Amtlicher_Gemeindeschlüssel = schlussel; // Adjusted the variable name to match the console log } function updateDatabaseCosts(database, excelFilePath) { console.log('<--------------------Kostenträger-stelle-------------------->') const sheetName = 'Kostenträger-stelle'; const startRow = 12; const startCol = 2; const endCol = 4; const endRow = 14; const data = updateDatabaseFromExcel(excelFilePath, sheetName, startRow, startCol, endCol, endRow); const Kostentraeger = data[0][2]; const Kostenstelle = data[2][2]; console.log('Updated database with the following Kostenträger-stelle info: '); console.log('Kostentraeger: ', Kostentraeger); console.log('Kostenstelle: ', Kostenstelle); console.log(''); // Add extracted variables to the database object database.Kostentraeger = Kostentraeger; database.Kostenstelle = Kostenstelle; } function writeObjectToJson(object, filename, outputPath) { const jsonContent = JSON.stringify(object, null, 4); fs.writeFile(outputPath + filename, jsonContent, 'utf8', function(err) { if (err) { console.error("Error writing to JSON file:", err); } else { console.log(`Object has been written to ${outputPath}${filename}`); } }); } function updateAll(database, excelFilePath) { //let database = {}; // Create an empty object to store the database updateDatabaseSachbearbeiter(database, excelFilePath); updateDatabaseOrganisatsionsdaten(database, excelFilePath); updateDatabaseKeyUser(database, excelFilePath); updateDatabaseBankverbindung(database, excelFilePath); updateDatabaseCosts(database, excelFilePath); console.log(database) }