Created
November 15, 2016 10:09
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note: it usually fails with error 500 if the workbook is currently open and being edited by you or someone else.