1

我尝试在 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) {
                    });

            });
4

0 回答 0