Skip to content

Instantly share code, notes, and snippets.

@andrei-markeev
Created November 15, 2016 10:09
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrei-markeev/9006c2985ca84534c826813eaf86de9f to your computer and use it in GitHub Desktop.
Save andrei-markeev/9006c2985ca84534c826813eaf86de9f to your computer and use it in GitHub Desktop.
Get data from Excel file stored in SharePoint using Excel REST API
var fileRelativeUrl = "Shared%20Documents/book1.xlsx";
var sheetName = "Sheet1";
var range = "A1|J45";
var url = _spPageContextInfo.webServerRelativeUrl.replace(/\/$/, '') + "/";
loadXMLDoc(url + "_vti_bin/ExcelRest.aspx/" + fileRelativeUrl + "/model/Ranges('" + sheetName + "!" + range + "')?$format=atom", function (text) {
var parser = new DOMParser();
var excelRestNS = 'http://schemas.microsoft.com/office/2008/07/excelservices/rest';
var xmlDoc = parser.parseFromString(text, "text/xml");
var rows = xmlDoc.getElementsByTagNameNS(excelRestNS, 'row');
// Get a text from cell A1
var row = 0;
var column = 0;
var cell = rows[row].getElementsByTagNameNS(excelRestNS, 'c')[column];
var formattedValueElement = cell.getElementsByTagNameNS(excelRestNS, 'fv')[0];
var cellText = formattedValueElement.childNodes[0].wholeText;
console.log(cellText);
});
// super simple implementation of XHR, you can alternatively use anything else e.g. $.ajax
function loadXMLDoc(url, callback) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == XMLHttpRequest.DONE) {
if (xmlhttp.status == 200) {
callback(xmlhttp.responseText);
}
else {
console.log('Error ' + xmlhttp.status, xmlhttp);
}
}
};
xmlhttp.open("GET", url, true);
xmlhttp.send();
}
@andrei-markeev
Copy link
Author

andrei-markeev commented Nov 15, 2016

excel_example

Note: it usually fails with error 500 if the workbook is currently open and being edited by you or someone else.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment