问题
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