在我的Excel加载项中,我想按顺序对文档执行多次编辑。 我正在使用承诺链来实现这个目标。
不幸的是,我从某些编辑中获得了GeneralException: An internal error has occurred.
。
以下示例执行250次编辑,每次运行时我获得20到30个GeneralExceptions。 (使用Office 2016,在Office上,它更糟糕)
示例:
var promise;
Office.initialize = function (reason) {
// add awesome addin initialize code here
promise = new OfficeExtension.Promise(function (resolve, reject) { resolve(null); });
for (var i = 0; i < 200; i++) {
insertData("Data" + i);
}
}
function insertData(data) {
if (Office.context.requirements.isSetSupported("ExcelApi", "1.0")) {
//insert the data into the spreadsheet
promise = promise.then(function () {
Excel.run(function (ctx) {
var sheet = ctx.workbook.worksheets.getActiveWorksheet();
var range = ctx.workbook.getSelectedRange();
range.getCell(0, 0).values = data;
range.getCell(1, 0).select();
return ctx.sync()
}).catch(function (error) {
addLogEntry(error.message);
});
});
}
else if (Office.context.requirements.isSetSupported("WordApi", "1.0")) {
promise = promise.then(function () {
Word.run(function (ctx) {
var body = ctx.document.body;
var selectedRange = ctx.document.getSelection();
selectedRange.insertText(data + "\n", 'End');
selectedRange.select('End');
return ctx.sync();
}).catch(function (error) {
addLogEntry(error.message);
});
});
}
}
function addLogEntry(message) {
// log message here
}
我做错了什么?
这里是错误的堆栈跟踪:
"GeneralException: An internal error has occurred.
at Anonymous function (https://appsforoffice.microsoft.com/lib/1/hosted/word-win32-16.01.debug.js:9329:6)
at lib$es6$promise$$internal$$tryCatch (https://appsforoffice.microsoft.com/lib/1/hosted/word-win32-16.01.debug.js:11207:8)
at lib$es6$promise$$internal$$invokeCallback (https://appsforoffice.microsoft.com/lib/1/hosted/word-win32-16.01.debug.js:11217:8)
at lib$es6$promise$$internal$$publish (https://appsforoffice.microsoft.com/lib/1/hosted/word-win32-16.01.debug.js:11193:9)
at lib$es6$promise$asap$$flush (https://appsforoffice.microsoft.com/lib/1/hosted/word-win32-16.01.debug.js:11027:8)"
答案 0 :(得分:0)
我找到了一个解决方案,但它不是很优雅且非常慢(特别是在办公室在线)
也许有人能想出更好的东西? ;)
这里是固定的insertData
函数:
var isSending = false;
function insertData(data) {
if (!isSending) {
isSending = true;
if (Office.context.requirements.isSetSupported("ExcelApi", "1.0")) {
//insert the data into the spreadsheet
//promise = promise.then(function () {
Excel.run(function (ctx) {
var sheet = ctx.workbook.worksheets.getActiveWorksheet();
var range = ctx.workbook.getSelectedRange();
range.getCell(0, 0).values = data;
range.getCell(1, 0).select();
return ctx.sync()
}).then(function () {
isSending = false;
if (queue.length > 0) {
insertData(queue.splice(0, 1)[0]);
}
}).catch(function (error) {
addLogEntry(error.message);
});
//});
}
else if (Office.context.requirements.isSetSupported("WordApi", "1.0")) {
//promise = promise.then(function () {
Word.run(function (ctx) {
var body = ctx.document.body;
var selectedRange = ctx.document.getSelection();
selectedRange.insertText(data + "\n", 'End');
selectedRange.select('End');
return ctx.sync();
}).then(function () {
isSending = false;
if (queue.length > 0) {
insertData(queue[0]);
queue = queue.splice(0, 1);
}
}).catch(function (error) {
addLogEntry(error.message);
});
//});
}
}
else {
queue.push(data);
}
}