I have a project in progress where I'm currently working on converting an Excel sheet to JSON. Once the data is converted, it will be displayed using jQuery Datatables on the browser. My code is functioning as expected, but I am encountering an issue when the Excel file is located in a different folder than the HTML file. How can I retrieve the path to the Excel file from another directory to use in my script?
Below is the snippet of the code:
function loadExcelData(callback) {
var excelUrl = "LICENCIAMENTOS PROJECTOS.xlsx";
var request = new XMLHttpRequest();
request.open("GET", excelUrl, true);
request.responseType = "arraybuffer";
request.onload = function(event) {
var arrayBuffer = request.response;
/* Convert data to binary string */
var data = new Uint8Array(arrayBuffer);
var dataArray = new Array();
for (var i = 0; i != data.length; ++i) dataArray[i] = String.fromCharCode(data[i]);
var binaryString = dataArray.join("");
/* Call XLSX library */
var workbook = XLSX.read(binaryString, { type: "binary" });
/* Perform actions with workbook here */
var sheetIndex = 0; // Change this index to get the desired sheet
var sheetName = workbook.SheetNames[sheetIndex];
/* Get worksheet */
var worksheet = workbook.Sheets[sheetName];
// Extracting specific range of data from the worksheet
var extractedData = XLSX.utils.sheet_to_json(worksheet, { range: 4 });
$("h4").remove();
callback(extractedData);
}
request.send();
}
loadExcelData(function(result) {
console.log(result);
$("#data-table").show();
// Initialize DataTable
$('#data-table').DataTable({
"language": {
"lengthMenu": "Display _MENU_ rows per page",
"search": "Search:",
"info": "Showing _START_ to _END_ of _TOTAL_ entries",
"next": "Next",
"previous": "Previous"
},
"dom": '<"pull-left"f><"pull-right"l>tip',
"aaData": result,
"aoColumns": [
....
]
});
});