From 4ec8e1bacfa5e8adf87e1777ed07b6e49aa5cedd Mon Sep 17 00:00:00 2001 From: Tiven Date: Sun, 29 Oct 2023 22:07:24 -0300 Subject: [PATCH] Add modify excel increase price and decrease price --- main.js | 137 ++++++++++++++++++++++++++++++--------------------- utils.js | 58 +++++++++++++++++++--- utilsInfo.js | 5 ++ 3 files changed, 136 insertions(+), 64 deletions(-) create mode 100644 utilsInfo.js diff --git a/main.js b/main.js index 48b1dd2..733507b 100644 --- a/main.js +++ b/main.js @@ -1,63 +1,88 @@ -const { - app, - BrowserWindow, - ipcMain, - ipcRenderer, - dialog, -} = require("electron"); +const { app, BrowserWindow, ipcMain, ipcRenderer, dialog } = require("electron"); const XLSX = require("xlsx"); +const increasePrice = (dataExcel, percentage) => { + let price = null; + let priceModified = null; + + dataExcel.forEach((element) => { + price = element["Precio Venta"]; + priceModified = price + price * (percentage / 100); + element["Precio Venta"] = priceModified.toFixed(0); + element["Codigo"] = `${element["Codigo"]}`; + }); + return dataExcel; +}; + +const decreasePrice = (dataExcel, percentage) => { + let price = null; + let priceModified = null; + + dataExcel.forEach((element) => { + price = element["Precio Venta"]; + priceModified = price - price * (percentage / 100); + element["Precio Venta"] = priceModified.toFixed(0); + element["Codigo"] = `${element["Codigo"]}`; + }); + return dataExcel; +}; + const createWindow = () => { - const win = new BrowserWindow({ - width: 1200, - height: 800, - webPreferences: { - nodeIntegration: true, - contextIsolation: false, - }, - }); - - win.setMenu(null); - win.loadFile("index.html"); - // win.webContents.openDevTools(); - - // win.webContents.on("did-finish-load", () => { - // console.log("did-finish-load"); - // win.webContents.send("uploaded-server", "Hello!"); - // }); - - ipcMain.on("uploaded", async (e, data) => { - const workbook = XLSX.readFile(data); - const worksheetName = workbook.SheetNames[0]; - const worksheet = workbook.Sheets[worksheetName]; - - const dataExcel = XLSX.utils.sheet_to_json(worksheet); - - dataExcel.forEach((element) => { - element["Precio venta"] = element["Precio venta"].replace("$", "") * 2; - }); - - const newWorkbook = XLSX.utils.book_new(); - const newWorksheet = XLSX.utils.json_to_sheet(dataExcel); - XLSX.utils.book_append_sheet(newWorkbook, newWorksheet, "Hoja 1"); - - const result = await dialog.showSaveDialog({ - title: "Save file as", - filters: [ - { - name: "Spreadsheets", - extensions: ["xlsx", "xls", "xlsb"], - }, - ], - }); - console.log(result); - - const statusWrite = XLSX.writeFile(newWorkbook, result.filePath, { - compression: true, - }); - }); + const win = new BrowserWindow({ + width: 630, + height: 580, + resizable: false, + webPreferences: { + nodeIntegration: true, + contextIsolation: false, + }, + }); + + win.setMenu(null); + win.loadFile("index.html"); + //win.webContents.openDevTools(); + + // win.webContents.on("did-finish-load", () => { + // console.log("did-finish-load"); + // win.webContents.send("uploaded-server", "Hello!"); + // }); + + ipcMain.on("uploaded", async (e, data, percentage, action) => { + const workbook = XLSX.readFile(data); + const worksheetName = workbook.SheetNames[0]; + const worksheet = workbook.Sheets[worksheetName]; + + const dataExcel = XLSX.utils.sheet_to_json(worksheet); + let newDataExcel = null; + + if (action === "increase") newDataExcel = increasePrice(dataExcel, percentage); + else newDataExcel = decreasePrice(dataExcel, percentage); + + const newWorkbook = XLSX.utils.book_new(); + const newWorksheet = XLSX.utils.json_to_sheet(newDataExcel); + XLSX.utils.book_append_sheet(newWorkbook, newWorksheet, "Hoja 1"); + + const result = await dialog.showSaveDialog({ + title: "Save file as", + filters: [ + { + name: "Spreadsheets", + extensions: ["xls"], + }, + ], + }); + + try { + XLSX.writeFile(newWorkbook, result.filePath, { + WTF: true, + bookType: "biff8", + }); + } catch (error) { + console.log(error); + } + }); }; app.whenReady().then(() => { - createWindow(); + createWindow(); }); diff --git a/utils.js b/utils.js index 4b43d18..48827a7 100644 --- a/utils.js +++ b/utils.js @@ -1,19 +1,61 @@ const { ipcRenderer } = require("electron"); - const inputFile = document.getElementById("file"); +const filename = document.querySelector(".filename"); + +const increaseButton = document.getElementById("increase-price-button"); +const decreaseButton = document.getElementById("decrease-price-button"); + +const inputPercentage = document.getElementById("custom-percentage"); +const buttonsPercentages = document.querySelectorAll(".percentages-buttons button"); + +const infoIcon = document.getElementById("info-icon"); + +let selectedButton = null; +let percentage = null; // ipcRenderer.on("uploaded-server", (event, arg) => { // console.log("Message from server"); // }); -inputFile.addEventListener("change", async (e) => { - const file = e.target.files[0]; - const data = await file.arrayBuffer(); +buttonsPercentages.forEach((button) => { + button.addEventListener("click", (e) => { + if (selectedButton) selectedButton.classList.remove("selected"); + selectedButton = button; + selectedButton.classList.add("selected"); + percentage = e.currentTarget.getAttribute("data-percentage"); + }); +}); + +inputPercentage.addEventListener("input", (e) => { + if (selectedButton && inputPercentage !== selectedButton) + selectedButton.classList.remove("selected"); + + selectedButton = inputPercentage; - // const workbook = XLSX.read(data, { type: "array" }); - // const sheet = workbook.SheetNames[0]; + if (e.target.value) selectedButton.classList.add("selected"); + else selectedButton.classList.remove("selected"); - // const dataExcel = XLSX.utils.sheet_to_json(workbook.Sheets[sheet]); + if (e.target.value >= 0 && e.target.value <= 100) percentage = e.target.value; + else e.target.value = 0; +}); + +inputFile.addEventListener("change", (e) => { + const file = e.target.files[0]; + filename.innerHTML = `📄 ${file.name}`; +}); + +increaseButton.addEventListener("click", async () => { + const datafile = await inputFile.files[0].arrayBuffer(); + if (percentage && percentage > 0) + ipcRenderer.send("uploaded", datafile, percentage, "increase"); +}); + +decreaseButton.addEventListener("click", async () => { + const datafile = await inputFile.files[0].arrayBuffer(); + if (percentage && percentage > 0) + ipcRenderer.send("uploaded", datafile, percentage, "decrease"); +}); - ipcRenderer.send("uploaded", data); +infoIcon.addEventListener("click", () => { + window.location.href = "info.html"; }); diff --git a/utilsInfo.js b/utilsInfo.js new file mode 100644 index 0000000..66c3bdc --- /dev/null +++ b/utilsInfo.js @@ -0,0 +1,5 @@ +const homeIcon = document.getElementById("home-icon"); + +homeIcon.addEventListener("click", () => { + window.location.href = "index.html"; +});