如何从Word插件中的当前选择中获取parentContentControls

问题描述 投票:0回答:3

我正在使用word javascript api开发一个word插件,需要获取当前选择的contentControl,因此使用parentContentControl作为当前选择。

**Code:**    
var range = context.document.getSelection().parentContentControl;
context.load(range);

但是在控制台上显示错误: 错误:

{"name":"OfficeExtension.Error","code":"GeneralException","message":"GeneralException","traceMessages":[],"debugInfo":{"errorLocation":"Range.parentContentControl"},"stack":"GeneralException: GeneralException\n   at Anonymous function (https://appsforoffice.microsoft.com/lib/1/hosted/word-win32-16.01.js:19:189006)\n   at pi (https://appsforoffice.microsoft.com/lib/1/hosted/word-win32-16.01.js:19:211583)\n   at ht (https://appsforoffice.microsoft.com/lib/1/hosted/word-win32-16.01.js:19:211670)\n   at g (https://appsforoffice.microsoft.com/lib/1/hosted/word-win32-16.01.js:19:211490)\n   at l (https://appsforoffice.microsoft.com/lib/1/hosted/word-win32-16.01.js:19:210076)"}

Debug info: {"errorLocation":"Range.parentContentControl"}

如果当前选择不包含任何 contentControl 它应该返回 NULL,但它会给出错误。请指教。

谢谢。

ms-word office365 office-js word-addins javascript-api-for-office
3个回答
1
投票

这是一个非常好的问题,涉及到office.js技术的核心概念之一:我们如何处理null?长话短说,每当方法/属性有可能返回 null 时,我们都会提供该方法/属性的风格:

默认。没有特殊后缀。如果返回值为空(在这种情况下,没有围绕选择的内容控制),这种风格会立即引发异常,正如您的问题中所正确描述的那样。这是设计使然。
  1. 返回“空对象”。它们有一个 *OrNullObject 后缀。这种风格不会抛出异常,但会返回一种方法来验证对象是否为空。 (请注意,在这种情况下,这个“空对象”与 JavaScript null 不同,请不要对此感到困惑)
  2. 第二种风格从 11 月分叉(版本 16.0.7668+)开始可用,因此请确保更新您的客户以查看此功能。
  3. 因此,具体回答您的问题:这种行为是设计使然的。如果要验证所选内容中是否存在内容控件,则需要使用 range.parentContentControlOrNullObject 属性。然后你可以检查它是否为空。以下是如何实现此目的的示例:

var myCC = context.document.getSelection().parentContentControlOrNullObject; // this flavor will not throw an exception. context.load(myCC); return context.sync() .then(function () { if (myCC.isNullObject) // when using this flavor of the property you will get a isNullObject to check if its null or not and act accordingly. console.log("There is no content control sorrounding"); else app.showNotification("there is a content control wrapping the selection."); }) .catch(function (e) { console.log(e.message); }) })



希望这能澄清这个概念

这是Microsoft Office word 2016版本问题。 相同的代码在

16.0.7571.7095.

1
投票

function insideOfContentControlCheck() { Word.run(function (ctx) { var myCC = ctx.document.getSelection().parentContentControl; ctx.load(myCC); // I think this is the part you are missing! return ctx.sync() .then(function () { console.log(myCC.title);// if there is a content control we'll show the title }); }).catch(function (e) { //there is no ContentControl. console.log("Error", e.message); }); }


    

这是检查

parentSectionOrNullObject

0
投票


await Word.run(async (context) => { let html; const range = context.document.getSelection(); range.load('parentContentControlOrNullObject, parentTableOrNullObject'); await context.sync(); const contentControl = range.parentContentControlOrNullObject; if (!contentControl.isNull) { html = contentControl.getHtml(); }else{ console.log("there is not parentContentControlOrNullObject in current Selection") } await context.sync(); console.log(html.value); })


    

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.