2017-10-24 10 views
1

私のExcelアドインでは、ドキュメントに対して複数の編集を順番に実行したいと思います。 私はこれを達成するために約束を使用しています。プロミスチェーンの複数の編集で一般的なエラー

残念ながら私はGeneralException: An internal error has occurred.を編集しています。

次の例では250回の編集を行い、毎回20〜30のGeneralExceptionsを取得します。私は間違って何をやっている

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 
} 

例(オフィス2016で、オフィスでのオンラインそれははるかに悪いですか)?

ここでは、エラーのスタックトレース:

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

まず、Office.jsのデバッグバージョンに切り替えてみてください。 –

+0

チップをありがとう。これが存在することを知らなかった。スタックトレースが更新されました。 – ndreisg

答えて

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); 
    } 
} 
関連する問題