Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

integrate sweetalert library to handle multiple cases #7

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 48 additions & 29 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,31 +1,50 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Excel Sheet Generator</title>
<link rel="stylesheet" href="./style.css">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Bitter:wght@500&display=swap" rel="stylesheet">
</head>
<body>
<div class="form">
<h1>Excel Sheet Generator</h1>
<input type="number" class="rows" placeholder="Enter number of rows">
<input type="number" class="columns" placeholder="Enter number of columns">
<div class="btns">
<button onclick="generateTable()">Generate</button>
<button onclick="ExportToExcel('xlsx')">Export</button>
</div>
</div>
<div class="table">
<table cellspacing="6px">
<tbody class="sheet-body"></tbody>
</table>
</div>
</body>
<script type="text/javascript" src="https://unpkg.com/[email protected]/dist/xlsx.full.min.js"></script>
<script src="./script.js"></script>
</html>
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0"
/>
<title>Excel Sheet Generator</title>
<link rel="stylesheet" href="./style.css" />
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link
href="https://fonts.googleapis.com/css2?family=Bitter:wght@500&display=swap"
rel="stylesheet"
/>
</head>
<body>
<div class="form">
<h1>Excel Sheet Generator</h1>
<input
type="number"
class="rows"
placeholder="Enter number of rows"
/>
<input
type="number"
class="columns"
placeholder="Enter number of columns"
/>
<div class="btns">
<button onclick="generateTable()">Generate</button>
<button onclick="ExportToExcel('xlsx')">Export</button>
</div>
</div>
<div class="table">
<table cellspacing="6px">
<tbody class="sheet-body"></tbody>
</table>
</div>
</body>
<script
type="text/javascript"
src="https://unpkg.com/[email protected]/dist/xlsx.full.min.js"
></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

<script src="./script.js"></script>
</html>
21 changes: 21 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

80 changes: 56 additions & 24 deletions script.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,61 @@
let table = document.getElementsByClassName("sheet-body")[0],
rows = document.getElementsByClassName("rows")[0],
columns = document.getElementsByClassName("columns")[0]
tableExists = false
rows = document.getElementsByClassName("rows")[0],
columns = document.getElementsByClassName("columns")[0];
tableExists = false;

const generateTable = () => {
let rowsNumber = parseInt(rows.value), columnsNumber = parseInt(columns.value)
table.innerHTML = ""
for(let i=0; i<rowsNumber; i++){
var tableRow = ""
for(let j=0; j<columnsNumber; j++){
tableRow += `<td contenteditable></td>`
}
table.innerHTML += tableRow
}
if(rowsNumber>0 && columnsNumber>0){
tableExists = true
}
}
let rowsNumber = parseInt(rows.value),
columnsNumber = parseInt(columns.value);

if (!rowsNumber || !columnsNumber) {
Swal.fire({
icon: "error",
title: "Oops...",
text: "Enter a valid number for both inputs",
});
return;
}

table.innerHTML = "";
for (let i = 0; i < rowsNumber; i++) {
var tableRow = "";
for (let j = 0; j < columnsNumber; j++) {
tableRow += `<td contenteditable></td>`;
}
table.innerHTML += tableRow;
}
if (rowsNumber > 0 && columnsNumber > 0) {
tableExists = true;
}
};

const ExportToExcel = (type, fn, dl) => {
if(!tableExists){
return
}
var elt = table
var wb = XLSX.utils.table_to_book(elt, { sheet: "sheet1" })
return dl ? XLSX.write(wb, { bookType: type, bookSST: true, type: 'base64' })
: XLSX.writeFile(wb, fn || ('MyNewSheet.' + (type || 'xlsx')))
}
if (!tableExists) {
Swal.fire({
icon: "error",
title: "Oops...",
text: "Generate the table before export!",
});
return;
}

var elt = table;
let exportable = true;
elt.querySelectorAll("td").forEach((it) => {
if (it.innerText === "") {
Swal.fire({
icon: "error",
title: "Oops...",
text: "Please fill all table's fields before export",
});
exportable = false;
return;
}
});
if (!exportable) return;
var wb = XLSX.utils.table_to_book(elt, { sheet: "sheet1" });
console.log("d");
return dl
? XLSX.write(wb, { bookType: type, bookSST: true, type: "base64" })
: XLSX.writeFile(wb, fn || "MyNewSheet." + (type || "xlsx"));
};