I'm working on a project that is supposed to take an uploaded excel file, convert it to JSON, and POST that parsed info into a SharePoint list. The POST request is working---when I reload the SharePoint list the uploaded item is there, but it's coming back as blank.
Long story short, the data that's supposed to be parsed is shown to be an empty object.
I also noticed that in the excel parsing function, the majority of the code is getting skipped over and I'm not sure why. In the code below I've listed where it happens.
What parameters do I need to pass into xl2String (see below)?
HTML:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script lang="javascript" src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.15.5/xlsx.full.min.js"></script>
<input class="getFile" id="getFile" type="file" name="files[]" multiple="multiple"/><br />
JS:
<script>
const ExcelToJSON = function(files) {
this.parseExcel = function(files) {
let reader = new FileReader();
reader.onload = function(e) {
let data = e.target.result; // this code is NOT reached
console.log("data: ", data)
let workbook = XLSX.read(data, { type: 'binary' });
workbook.SheetNames.forEach((sheetName) => {
let XL_row_object = XLSX.utils.sheet_to_json(workbook.Sheets[sheetName]);
let json_object = JSON.stringify(XL_row_object);
console.log(JSON.parse(json_object))
$('#jsonData').val(json_object);
})
} // reader.onload // debugger skips over
reader.onerror = function(err) {
console.log(err);
}
// reader.readAsBinaryString(files); // tells JS to start reading the file
} // this.parseExcel
} // ExcelToJSON
function handleFileSelect(evt) {
let files = evt.target.files; // FileList object
let xl2json = new ExcelToJSON(files);
xl2json.parseExcel(files[0]);
let xl2String = JSON.stringify(xl2json);
let addlistItem = new AddItemToSPList();
AddItemToSPList(xl2String[0].Title, xl2String[0].Location);
console.log(xl2String) // empty obj...
// let filteredByLocation = xl2json.map(e => e.Location == "New York");
console.log(xl2String.Title); // empty obj
}
const AddItemToSPList = function(title, location) {
let holidaysColumns = {
"__metadata":{"type": "SP.Data.holidaysListItem"},
"Title": title,
"Location": location
};
let listName = "Test%20Holidays";
return fetch("www.url.com/gc/_api/web/lists/getbytitle('" + listName + "')/items", {
method: "POST",
credentials: 'same-origin',
body: JSON.stringify(holidaysColumns),
headers: {
// etc
}
}).then((response) => {
$(".uploadSuccessFail").html("File was upload successfully.")
console.log(response)
}).catch((err) => {
$(".uploadSuccessFail").html("Error: File upload unsuccessful.")
console.log("Error: " + err);
})
}
</script>
<script>
document.getElementById('getFile').addEventListener('change', handleFileSelect, false);
</script>