Office.js throws exception when tried to read cell address if Excel is kept Open for 20-30 min and then try to read cells value

故事扮演 提交于 2019-12-11 23:45:24

问题


I am using office.js i am trying to read the cell value by passing the address of cell to a function. it works all fine but sometime it just throws exception and exception messages are too general that you do not know what the issue is.

look at the function below its all good but some time i get error when ctx is tried to sync at return ctx.sync().then(function () {

public getDataAtRange(rangeAddress: string): JQueryPromise<any[]> {
            let defer = $.Deferred();

            if (rangeAddress !== null) {
                Excel.run(function (ctx: Excel.RequestContext) {
                    let range = rangeAddress.split("!");
                    let sheet = ctx.workbook.worksheets.getItem(range[0]);
                    let selectedRange = sheet.getRange(rangeAddress);
                    selectedRange.load(["values", "text"]);

                    return ctx.sync().then(function () {
                        let data: any[] = [];
                        for (let i = 0; i < selectedRange.text.length; i++) {
                            for (let j = 0; j < selectedRange.text[i].length; j++) {
                                let text = (selectedRange.text[i][j]).trim();
                                let value = selectedRange.values[i][j];
                                if (text !== "") {
                                    let cellValue: any = { value: "", text: "" };
                                    cellValue.value = value;
                                    cellValue.text = text;
                                    data.push(cellValue);
                                }
                            }
                        }

                        defer.resolve(data);
                    });
                }).catch(function (error: any) {
                    console.log("Error in getDataAtRange(): " + error);
                    if (error instanceof OfficeExtension.Error) {
                        console.log("Debug info: " + JSON.stringify(error.debugInfo));
                    }
                    defer.reject("Error in reading cell value, make sure the selected cell is not in Edit mode.");
                });
            } else { defer.resolve(); }

            return defer.promise();
        }

Most of the time this error can be consistently reproduce if excel is kept open for 20-30 min and then tried to read its cell value.

this is the error message i get. Error in getDataAtRange(): GeneralException: An internal error has occurred. Debug info: {}

error message from code

来源:https://stackoverflow.com/questions/40210749/office-js-throws-exception-when-tried-to-read-cell-address-if-excel-is-kept-open

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!