Trying to get an Excel document through ajax, running into async issues
It looks like the answer was simply that I didn't fully understand what various methods related to async functions were doing. I restructured the function in the following way and got it to work as expected:
$("#updateClusterDetails").click(function() {
console.log("Update initiated");
var baseFile = "AIF%20Test.xlsx";
$.when(readFileRow(libraryUrl, baseFile, "Sheet1", 1, "E", new Array())).then(function(results) {
console.log(results);
});
});
/**
* Reads a file in SharePoint Library via ExcelRest and returns the document
*
* @params string libraryUrl
* @params string fileLocation
*
* @returns Document/HTML
**/
function readFile(libraryUrl, fileLocation) {
// The base url for the SharePoint document library
var fileUrl = libraryUrl + fileLocation;
return $.ajax({
url: fileUrl,
type: "GET",
error: function (request, status, error) {
console.log(error);
},
success: function(data) {
return $.when(data);
}
});
}
/**
* Reads a series of rows in an Excel file in SharePoint Library via ExcelRest and returns the row data
*
* @params string libraryUrl
* @params string fileName
* @params string sheetName
* @params int rowNum
* @params string lastColumn
* @params string[][] currentResults
*
* @returns string[][]
**/
function readFileRows(libraryUrl, fileName, sheetName, rowNum, lastColumn, currentResults) {
var fileUrl = "/" + fileName + "/Model/Ranges('" + sheetName + "!A" + rowNum + "|" + lastColumn + rowNum + "')?$format=atom";
return readFile(libraryUrl, fileUrl)
.then(function(data) {
jsonData = xmlToJson(data.documentElement);
cells = serializeRange(jsonData);
if(!cells) {
return $.when(currentResults);
}
else {
currentResults.push(cells);
return $.when(readFileRow(libraryUrl, fileName, sheetName, rowNum + 1, lastColumn, currentResults));
}
});
}