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

'task done' #1

Open
wants to merge 2 commits 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
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# excel-sheet-generator

hello there, your task is to use sweetalert.js to display an alert when the user clicks "generate" while the fields are empty, and when the user clicks "export" but there is no generated table to be exported.
Problem:

fork the repository and start working on it.
I am developing a web application that allows users to generate tables based on data inputted in a form. The user can then export the table as a CSV file. However, I want to ensure that the user inputs data before generating the table, and that the table is generated before allowing the user to export it.

best of luck.
Solve:

I am using SweetAlert.js to display the appropriate alert messages
2 changes: 2 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
<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">
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@10"></script>

</head>
<body>
<div class="form">
Expand Down
43 changes: 28 additions & 15 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,38 @@ 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){
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
}
tableExists = true
} else {
Swal.fire({
title: 'Error',
text: 'Please enter the number of rows and columns.',
icon: 'error',
confirmButtonText: 'OK'
})
}
}

const ExportToExcel = (type, fn, dl) => {
if(!tableExists){
return
if(tableExists){
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')))
} else {
Swal.fire({
title: 'Error',
text: 'Please generate a table before exporting.',
icon: 'error',
confirmButtonText: 'OK'
})
}
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')))
}
}