我正在使用 XMLHttpRequest 和 sheetjs ( https://github.com/SheetJS/js-xlsx ) 从 excel 电子表格创建 json 对象。一切都运行良好,除了我正在努力访问外部创建的对象oReq.onload
。创建 json 对象后,我想将其用作 XMLHttpRequest 下面更多代码的输入对象。
我已经尝试return json
在最后oReq.onload
并尝试在其中包含我的附加代码,function (e) {}
但两个选项都没有奏效。任何建议,将不胜感激。
/* set up XMLHttpRequest */
var url = "graph.xlsx";
var oReq = new XMLHttpRequest();
oReq.open("GET", url, true);
oReq.responseType = "arraybuffer";
oReq.onload = function(e) {
var arraybuffer = oReq.response;
/* convert data to binary string */
var data = new Uint8Array(arraybuffer);
var arr = new Array();
for(var i = 0; i != data.length; ++i) arr[i] = String.fromCharCode(data[i]);
var bstr = arr.join("");
/* Call XLSX */
var workbook = XLSX.read(bstr, {type:"binary"});
/* Create the json object */
var json = to_json(workbook);
/*Converts excel format to JSON*/
function to_json(workbook) {
var result = {};
workbook.SheetNames.forEach(function(sheetName) {
var roa = XLSX.utils.sheet_to_row_object_array(workbook.Sheets[sheetName]);
if(roa.length > 0){
result[sheetName] = roa;
}
});
return result;
}
};
oReq.send();
/*I WOULD LIKE TO USE THE CREATED JSON OBJECT HERE*/