【问题标题】:How to get parentContentControls from current selection in word add-in如何从 word 加载项中的当前选择中获取 parentContentControls
【发布时间】:2017-01-25 10:01:43
【问题描述】:

我正在使用 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


    【解决方案1】:

    这是 Microsoft Office word 2016 版问题。 相同的代码在 16.0.7571.7095 中运行良好。 但在 2016 版本中无法正常运行。

     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);
    
            });
    
        }

    【讨论】:

    • 我们找到了“一般异常”的解决方案。您可以使用 parentContentControlOrNullObject 代替 parentContentControl。它在最新的 word 版本中运行良好。
    【解决方案2】:

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

    1. 默认。无特殊后缀。如果返回值为 null (在这种情况下,选择周围没有内容控件),则此风味会立即引发异常,正如您在问题中正确描述的那样。这是设计使然。
    2. 返回一个“空对象”。它们有一个 *OrNullObject 后缀。这种风格确实 NOT 抛出异常,但返回一种验证对象是否为 null 的方法。 (注意在这种情况下,这个“空对象”与 JavaScript 的空对象不同,请不要被这个混淆)

    第二个版本从 11 月的分叉(内部版本 16.0.7668+)开始提供,因此请确保更新您的客户以查看此功能。

    所以要具体回答您的问题:这种行为是设计使然。如果要验证选择中是否存在内容控件,则需要使用 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);
    
                           })
    
    
    
            })

    希望这能澄清这个概念

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多