我尝试在Excel中创建一个休息插入。 我可以在Chrome中调试所有这些,一切看起来都很好。 但它不在Office中运行。 http://jsonplaceholder.typicode.com/posts是公开的
我从其他服务获得了信息。 将它们放入标题和行(在Chrome调试器中,我可以看到它) 我调用setExcelData应该将数据写入Excel
!!!但是我看不到结果。
<link href="https://cdnjs.cloudflare.com/ajax/libs/winjs/4.4.0/css/ui-light.css" rel="stylesheet" />
<script src="https://appsforoffice.microsoft.com/lib/1/hosted/Office.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/winjs/4.4.0/js/base.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/winjs/4.4.0/js/ui.js"></script>
function setExcelData(officeTable) {
if (officeTable != null) {
Office.context.document.setSelectedDataAsync(officeTable, { coercionType: Office.CoercionType.Table }, function (asyncResult) {
if (asyncResult.status == Office.AsyncResultStatus.Failed) {
showMessage('Set Selected Data Failed');
}
else {
showMessage('Set Selected Data Success');
}
});
}
}
Office.TableData.prototype.addHeaders = function (obj) {
var h = new Array();
for (var prop in obj) {
if (typeof (obj[prop]) != 'object' &&
prop.trim().length > 0 &&
prop != '__type')
h.push(prop);
}
this.headers = h;
}
Office.TableData.prototype.addRange = function (array) {
for (i = 0; i < array.length; i++) {
var itemsTemp = new Array();
var arr = Object.keys(array[i]).map(function(k) { return array[i][k] });
this.rows.push(arr);
}
}
WinJS.UI.processAll().done(function () {
WinJS.xhr({
url: "http://jsonplaceholder.typicode.com/posts",
responseType: "json"
}).done(
function completed(request) {
var officeTable = new Office.TableData();
officeTable.addHeaders(request.response[0]);
officeTable.addRange(request.response);
setExcelData(officeTable);
Office.context.document.setSelectedDataAsync(officeTable, { coercionType: Office.CoercionType.Table }, function (asyncResult) {
if (asyncResult.status == Office.AsyncResultStatus.Failed) {
showMessage('Set Selected Data Failed');
}
else {
showMessage('Set Selected Data Success');
}
});
},
function error(request) {
},
function progress(request) {
});
});