|
| 1 | +// Modules to control application life and create native browser window |
| 2 | +const {app, BrowserWindow, Menu, ipcMain, dialog} = require('electron') |
| 3 | +const path = require('path'); |
| 4 | +let fs = require('fs-extra'); |
| 5 | +var xl = require('excel4node'); |
| 6 | +let mainWindow; |
| 7 | + |
| 8 | +function MultiplyMatrix(A, B){ |
| 9 | + let rowsA = A.length, |
| 10 | + colsA = A[0].length, |
| 11 | + rowsB = B.length, |
| 12 | + colsB = B[0].length, |
| 13 | + C = []; |
| 14 | + if(colsA != rowsB) return false; |
| 15 | + for(let i = 0; i < rowsA; i++) C[i] = []; |
| 16 | + for(let k = 0; k < colsB; k++){ |
| 17 | + for(let i = 0; i < rowsA; i++){ |
| 18 | + let t = 0; |
| 19 | + for(let j = 0; j < rowsB; j++) t += A[i][j] * B[j][k]; |
| 20 | + C[i][k] = t; |
| 21 | + } |
| 22 | + } |
| 23 | + return C; |
| 24 | +} |
| 25 | + |
| 26 | +ipcMain.handle('export', async (event, {a, b, result}) => { |
| 27 | + let dir = dialog.showOpenDialogSync(mainWindow, { |
| 28 | + properties: ['openDirectory'] |
| 29 | + }); |
| 30 | + |
| 31 | + if(dir !== undefined) { |
| 32 | + await fs.stat(dir[0]).then(async () => { |
| 33 | + |
| 34 | + //Export A matrix |
| 35 | + const wbA = new xl.Workbook(); |
| 36 | + const wsA = wbA.addWorksheet('Sheet'); |
| 37 | + const styleCenterA = wbA.createStyle({ |
| 38 | + alignment: { // §18.8.1 |
| 39 | + horizontal: ['center'], |
| 40 | + }, |
| 41 | + }); |
| 42 | + let rowIndexA = 1; |
| 43 | + let colIndexA = 1; |
| 44 | + for(let row of a){ |
| 45 | + for(let col of row){ |
| 46 | + wsA.cell(rowIndexA, colIndexA) |
| 47 | + .number(Math.round((col + Number.EPSILON) * 1000) / 1000) |
| 48 | + .style(styleCenterA); |
| 49 | + colIndexA++; |
| 50 | + } |
| 51 | + colIndexA = 1; |
| 52 | + rowIndexA++; |
| 53 | + } |
| 54 | + |
| 55 | + wbA.write(path.join(dir[0], 'MatrixA.xlsx')); |
| 56 | + |
| 57 | + //Export B matrix |
| 58 | + const wbB = new xl.Workbook(); |
| 59 | + const wsB = wbB.addWorksheet('Sheet'); |
| 60 | + const styleCenterB = wbB.createStyle({ |
| 61 | + alignment: { // §18.8.1 |
| 62 | + horizontal: ['center'], |
| 63 | + }, |
| 64 | + }); |
| 65 | + let rowIndexB = 1; |
| 66 | + let colIndexB = 1; |
| 67 | + for(let row of b){ |
| 68 | + for(let col of row){ |
| 69 | + wsB.cell(rowIndexB, colIndexB) |
| 70 | + .number(Math.round((col + Number.EPSILON) * 1000) / 1000) |
| 71 | + .style(styleCenterB); |
| 72 | + colIndexB++; |
| 73 | + } |
| 74 | + colIndexB = 1; |
| 75 | + rowIndexB++; |
| 76 | + } |
| 77 | + |
| 78 | + wbB.write(path.join(dir[0], 'MatrixB.xlsx')); |
| 79 | + |
| 80 | + //Export result |
| 81 | + const wbResult = new xl.Workbook(); |
| 82 | + const wsResult = wbResult.addWorksheet('Sheet'); |
| 83 | + const styleCenterResult = wbResult.createStyle({ |
| 84 | + alignment: { // §18.8.1 |
| 85 | + horizontal: ['center'], |
| 86 | + }, |
| 87 | + }); |
| 88 | + |
| 89 | + let rowIndexResult = 1; |
| 90 | + let colIndexResult = 1; |
| 91 | + for(let row of result){ |
| 92 | + for(let col of row){ |
| 93 | + wsResult.cell(rowIndexResult, colIndexResult) |
| 94 | + .number(Math.round((col + Number.EPSILON) * 1000) / 1000) |
| 95 | + .style(styleCenterResult); |
| 96 | + colIndexResult++; |
| 97 | + } |
| 98 | + colIndexResult = 1; |
| 99 | + rowIndexResult++; |
| 100 | + } |
| 101 | + |
| 102 | + wbResult.write(path.join(dir[0], 'MatrixResult.xlsx')); |
| 103 | + |
| 104 | + await new Promise((resolve) => { |
| 105 | + setTimeout(() => { |
| 106 | + dialog.showMessageBoxSync(mainWindow, { |
| 107 | + title: 'Успех!', |
| 108 | + type: 'info', |
| 109 | + message: 'Данные успешно экспортированы' |
| 110 | + }); |
| 111 | + resolve(); |
| 112 | + }, 500) |
| 113 | + }) |
| 114 | + }).catch((error) => { |
| 115 | + dialog.showMessageBoxSync(mainWindow, { |
| 116 | + title: 'Ошибка!', |
| 117 | + type: 'error', |
| 118 | + message: 'Выбранная директория не существует, экспорт отменен', |
| 119 | + detail: 'Код ошибки: '+ error.code |
| 120 | + }); |
| 121 | + }); |
| 122 | + } else dialog.showMessageBoxSync(mainWindow, { |
| 123 | + title: 'Внимание!', |
| 124 | + type: 'warning', |
| 125 | + message: 'Экспорт отменен, папка небыла выбрана' |
| 126 | + }); |
| 127 | +}); |
| 128 | + |
| 129 | +ipcMain.handle('calc', async (event, {a, b}) => { |
| 130 | + return MultiplyMatrix(a, b) |
| 131 | +}); |
| 132 | + |
| 133 | +function createWindow () { |
| 134 | + // Create the browser window. |
| 135 | + mainWindow = new BrowserWindow({ |
| 136 | + width: 800, |
| 137 | + height: 600, |
| 138 | + webPreferences: { |
| 139 | + preload: path.join(__dirname, 'preload.js'), |
| 140 | + nodeIntegration: true, |
| 141 | + contextIsolation: false, |
| 142 | + enableRemoteModule: true |
| 143 | + } |
| 144 | + }) |
| 145 | + |
| 146 | + // and load the index.html of the app. |
| 147 | + mainWindow.loadFile('index.html') |
| 148 | + Menu.setApplicationMenu(null) |
| 149 | + // Open the DevTools |
| 150 | + // mainWindow.webContents.openDevTools() |
| 151 | +} |
| 152 | + |
| 153 | +// This method will be called when Electron has finished |
| 154 | +// initialization and is ready to create browser windows. |
| 155 | +// Some APIs can only be used after this event occurs. |
| 156 | +app.whenReady().then(() => { |
| 157 | + createWindow() |
| 158 | + |
| 159 | + app.on('activate', function () { |
| 160 | + // On macOS it's common to re-create a window in the app when the |
| 161 | + // dock icon is clicked and there are no other windows open. |
| 162 | + if (BrowserWindow.getAllWindows().length === 0) createWindow() |
| 163 | + }) |
| 164 | +}) |
| 165 | + |
| 166 | +// Quit when all windows are closed, except on macOS. There, it's common |
| 167 | +// for applications and their menu bar to stay active until the user quits |
| 168 | +// explicitly with Cmd + Q. |
| 169 | +app.on('window-all-closed', function () { |
| 170 | + if (process.platform !== 'darwin') app.quit() |
| 171 | +}) |
| 172 | + |
| 173 | +// In this file you can include the rest of your app's specific main process |
| 174 | +// code. You can also put them in separate files and require them here. |
0 commit comments