-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathscript.js
38 lines (36 loc) · 1.15 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
let table = document.getElementsByClassName("sheet-body")[0],
rows = document.getElementsByClassName("rows")[0],
columns = document.getElementsByClassName("columns")[0];
tableExists = false;
const generateTable = () => {
let rowsNumber = parseInt(rows.value),
columnsNumber = parseInt(columns.value);
if (isNaN(rowsNumber) || isNaN(columnsNumber)) {
swal("Error", "Please enter valid numbers for rows and columns!", "error", {
button: "OK",
});
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) {
swal("Error", "Please generate a table before exporting!", "error");
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"));
};