68 lines
2.4 KiB
JavaScript
68 lines
2.4 KiB
JavaScript
|
|
// main.js
|
||
|
|
const { app, BrowserWindow, ipcMain, dialog } = require('electron');
|
||
|
|
const path = require('path');
|
||
|
|
const { runBypivot } = require('./bypivot.js');
|
||
|
|
const { runBydep } = require('./bydep.js');
|
||
|
|
const { runBywork } = require('./bywork.js');
|
||
|
|
|
||
|
|
const scriptRunners = {
|
||
|
|
'bypivot.js': { runner: runBypivot, defaultName: 'output_แผนงานในการขอซื้อของจ้าง.xlsx' },
|
||
|
|
'bydep.js': { runner: runBydep, defaultName: 'output_แผนกวิชา.xlsx' },
|
||
|
|
'bywork.js': { runner: runBywork, defaultName: 'output_งาน.xlsx' },
|
||
|
|
};
|
||
|
|
|
||
|
|
function createWindow() {
|
||
|
|
const win = new BrowserWindow({
|
||
|
|
width: 650,
|
||
|
|
height: 450,
|
||
|
|
webPreferences: {
|
||
|
|
nodeIntegration: true,
|
||
|
|
contextIsolation: false,
|
||
|
|
},
|
||
|
|
});
|
||
|
|
|
||
|
|
win.loadFile(path.join(__dirname, 'index.html'));
|
||
|
|
// win.webContents.openDevTools();
|
||
|
|
}
|
||
|
|
|
||
|
|
ipcMain.handle('select-file', async () => {
|
||
|
|
const result = await dialog.showOpenDialog({
|
||
|
|
title: 'เลือกไฟล์ Excel',
|
||
|
|
filters: [{ name: 'Excel Files', extensions: ['xlsx'] }],
|
||
|
|
properties: ['openFile']
|
||
|
|
});
|
||
|
|
|
||
|
|
if (result.canceled || result.filePaths.length === 0) {
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
return result.filePaths[0];
|
||
|
|
});
|
||
|
|
|
||
|
|
// ✅ ส่วนรันสคริปต์ที่ปรับปรุงใหม่ + เพิ่ม Save Dialog
|
||
|
|
ipcMain.handle('run-script', async (_, { script, filePath }) => {
|
||
|
|
const scriptInfo = scriptRunners[script];
|
||
|
|
if (!scriptInfo) {
|
||
|
|
throw new Error(`ไม่พบสคริปต์สำหรับ: ${script}`);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 🚀 แสดงหน้าต่าง Save Dialog
|
||
|
|
const saveResult = await dialog.showSaveDialog({
|
||
|
|
title: 'เลือกตำแหน่งที่จะบันทึกไฟล์',
|
||
|
|
defaultPath: scriptInfo.defaultName,
|
||
|
|
filters: [{ name: 'Excel Files', extensions: ['xlsx'] }]
|
||
|
|
});
|
||
|
|
|
||
|
|
// ถ้าผู้ใช้ไม่เลือกที่บันทึก ให้ยกเลิก
|
||
|
|
if (saveResult.canceled || !saveResult.filePath) {
|
||
|
|
return 'ยกเลิกการบันทึก'; // ส่งข้อความกลับไปบอกสถานะ
|
||
|
|
}
|
||
|
|
|
||
|
|
const outputPath = saveResult.filePath;
|
||
|
|
|
||
|
|
// เรียกใช้ฟังก์ชันโดยตรงและส่งทั้ง input และ output path
|
||
|
|
return scriptInfo.runner(filePath, outputPath);
|
||
|
|
});
|
||
|
|
|
||
|
|
app.whenReady().then(createWindow);
|