簡體   English   中英

Office加載項:插入ooxml后無法清除Word文檔(Office Online,適用於桌面)

[英]Office Add-in: Cannot clean Word document after inserting ooxml (Office Online, works on Desktop)

第一次插入后,嘗試替換Word文檔的內容。 當我運行此代碼時:

Word.run(async context => {
  let range = context.document.body.getRange();
  let myContentControl = range.insertContentControl();
  myContentControl.clear();
  myContentControl.insertOoxml(ooxml, Word.InsertLocation.replace);
  myContentControl.cannotEdit = false;
  myContentControl.cannotDelete = false;
  context.load(myContentControl, "id");
  await context.sync();
});

我收到此錯誤:

NotAllowed Word Online中不支持該操作。 檢查OfficeExtension.Error.debugInfo以獲取更多信息。

如果我使用文本,然后運行此代碼,它插入正常。

這可能是我的ooxml嗎?

如果沒有看到OOXML或OfficeExtension.Error.debugInfo的值,就很難知道問題出在哪里。

也就是說,你在這里做了很多不必要的步驟:

Word.run(async context => {
  let range = context.document.body.getRange();

  // Why insert a content control?
  let myContentControl = range.insertContentControl();

  // Since you just inserted this, there nothing to clear
  myContentControl.clear();

  // Similar, since this is new (and cleared) why "replace"
  myContentControl.insertOoxml(ooxml, Word.InsertLocation.replace);

  // Already the default value
  myContentControl.cannotEdit = false;
  myContentControl.cannotDelete = false;

  // Why load this when you're not doing anything
  // with the value you're loaded
  context.load(myContentControl, "id");

  // Little known tip/trick:
  // You don't need to sync here, Word.run() will automatically 
  // process anything in the queue when it completes
  await context.sync();
});

在嘗試診斷這是否是您的OOXML字符串之前,我首先要減少您所做的不需要的調用次數:

Word.run(async context => {
  let range = context.document.body.getRange();
  range.insertOoxml('Your OOXML', 'Replace');
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM