小编Fel*_*ski的帖子

(适用于 Office 的 JavaScript API 1.3)自定义属性 GetItemOrNull

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

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


假设我有一个方法,在其中返回一个自定义属性。首先,我会检查自定义属性是否已创建。我会用一个简单的 getItemOrNullObject(key) 来完成此操作并且..

  • 如果返回 null 那么只需创建它并返回它
  • 否则退货

据我了解,我需要执行 return context.sync().then 才能使对象实际加载数据?我是否做了太多 return context.sync() 调用而没有任何作用?

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;
          }
        });
    });
});
Run Code Online (Sandbox Code Playgroud)

以下代码在开始时向我抛出“ReferenceError:'Word'未定义”,但如果我调试它,它会在中断之前运行

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


还有一个问题请教一下。假设我想更新我的自定义属性,会:

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

用新值覆盖旧值?

如果您读到这里,谢谢!任何帮助表示赞赏。干杯!

javascript ms-word ms-office office-addins office-js

4
推荐指数
1
解决办法
1532
查看次数