(用于Office的JavaScript API 1.3)自定义属性GetItemOrNull [英] (JavaScript API 1.3 for Office) Custom Properties GetItemOrNull

查看:62
本文介绍了(用于Office的JavaScript API 1.3)自定义属性GetItemOrNull的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了几天前,我需要有关如何向所述文档添加自定义属性的帮助.

I've created this a couple days ago in which i needed help regarding how to add custom properties to a said document.

首先,我正在运行Word 1701(7766.2047).

First of all, I'm running Word 1701(7766.2047).

假设我有一个方法,其中我返回了一个自定义属性.首先,我将检查是否已经创建了自定义属性.我会用一个简单的getItemOrNullObject(key)和..

Let's say I have a method In which I return a said custom property. First I'd check if the custom property has been created already. I would do this with a simple getItemOrNullObject(key) and..

  • 如果返回null,则只需创建并返回
  • 否则退还

据我了解,我需要执行return context.sync().然后该对象实际上已加载了数据?我在做太多返回context.sync()不需要什么吗?

It is of my understanding that I need to do a return context.sync().then for the object get actually loaded with data? Am I doing too much return context.sync() calls for nothing?

Word.run(function(context) {
  var customDocProps = context.document.properties.customProperties;
  context.load(customDocProps);
  return context.sync()
    .then(function() {
      var temp = customDocProps.getItemOrNullObject("X");
      return context.sync()
        .then(function() {
          if (!temp) {
            context.document.properties.customProperties.add("X", 1234);
            temp = customDocProps.getItemOrNullObject("X");
            return context.sync()
              .then(function() {
                return temp;
              });
          } else {
            return temp;
          }
        });
    });
});

以下代码在启动时向我抛出"ReferenceError:'Word'未定义",但如果我对其进行调试,则它会在中断之前运行

The following code throws me an 'ReferenceError: 'Word' is undefined' at start but if I debug it it runs before it breaks

var customDocProps = context.document.properties.customProperties; context.load(customDocProps); return context.sync().{....}

var customDocProps = context.document.properties.customProperties; context.load(customDocProps); return context.sync().{....}

还有一个问题.假设我要更新自定义属性,将:

Also have one more question. Say I want to update my custom property, would :

Word.run(function (context) {
        context.document.properties.customProperties.add("X", 56789);
        return context.sync();
    });

用新值覆盖旧值吗?

如果您读了这么远,谢谢!任何帮助表示赞赏. 干杯!

If you read this far thank you! Any help is appreciated. Cheers!

推荐答案

感谢您提出这个问题.

Thanks for asking this question.

您的代码是正确的,除了一个小细节:所有* getItemOrNullObject方法都返回JavaScript空值,因此您的"if(!temp)"语句将无法正常工作.如果要验证存在性,则需要调用if(temp.isNullObject).

Your code is correct except for one minor detail: All the *getItemOrNullObject methods do NOT return a JavaScript null, so your "if (!temp)" statement will not work as you expect. If you want to validate existence you need to call if(temp.isNullObject) instead.

还有一些建议:

  1. customProperties.add()语义是,如果该属性确实存在,它将被替换.因此,如果要创建或更改属性,则无需检查其是否存在.如果要读取其当前值,请执行.这回答了您的第二个问题.
  2. 如果您有兴趣加载单个属性,我为您的代码提供了一个简化且更有效的建议.

  Word.run(function (context) {
    var myProperty = context.document.properties.customProperties.getItemOrNullObject("X");
    context.load(myProperty);
    return context.sync()
      .then(function () {
        if (myProperty.isNullObject) {
          //this means the Custom Property does not exist....
          context.document.properties.customProperties.add("X", 1234);
          console.log("Property Created");
          return context.sync();
        }
        else
          console.log("The property already exists,  value:" + myProperty.value);
      })
  })
  .catch(function (e) {
      console.log(e.message);
    })

我们将更新文档,因为这似乎令人困惑.

We will update the documentation as this seems to be confusing.

谢谢!

这篇关于(用于Office的JavaScript API 1.3)自定义属性GetItemOrNull的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆