Skip to content

Commit

Permalink
Add modify excel increase price and decrease price
Browse files Browse the repository at this point in the history
  • Loading branch information
StivenCodess committed Oct 30, 2023
1 parent b160116 commit 4ec8e1b
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 64 deletions.
137 changes: 81 additions & 56 deletions main.js
Original file line number Diff line number Diff line change
@@ -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();
});
58 changes: 50 additions & 8 deletions utils.js
Original file line number Diff line number Diff line change
@@ -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";
});
5 changes: 5 additions & 0 deletions utilsInfo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const homeIcon = document.getElementById("home-icon");

homeIcon.addEventListener("click", () => {
window.location.href = "index.html";
});

0 comments on commit 4ec8e1b

Please sign in to comment.