GeneralException在承诺链中的多个编辑

在我的Excel加载项中,我想依次对文档执行多个编辑。 我正在使用诺言链来实现这一点。

不幸的是我得到一个GeneralException: An internal error has occurred. 从我的一些编辑。

以下示例执行250次编辑,每次运行时我都会得到20到30个GeneralException之间的内容。 (在Office 2016中,Office Online在线情况更糟)

例:

 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)" 

我find了一个解决scheme,但它不是很优雅,非常慢(特别是在办公室在线)

也许有人可以想出更好的东西? ;)

这里是固定的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); } } 
Interesting Posts