【问题标题】:(JavaScript API 1.3 for Office) Custom Properties GetItemOrNull(Office 的 JavaScript API 1.3)自定义属性 GetItemOrNull
【发布时间】:2017-02-22 12:05:25
【问题描述】:

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

首先,我正在运行 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;
          }
        });
    });
});

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

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

用新值覆盖旧值?

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

【问题讨论】:

    标签: javascript ms-word ms-office office-js office-addins


    【解决方案1】:

    感谢您提出这个问题。

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

    还有一些建议:

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

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

    谢谢!

    【讨论】:

    • 据我了解,Word.run() 是异步运行的,所以如果我想存储它的返回值,我应该尝试使用回调。 Word.run() 可以做到这一点吗?
    • 如果您想返回属性值,只需从我的示例中返回 myProperty.value 上的内容。
    • 使用您的样本。当标签为空时,我总是在 context.sync() 之后得到一个一般异常,正如你所写的那样,我看不出有什么问题。我应该打开一个新问题吗? imgur.com/a/TKujx
    • 抱歉,没听懂你在问什么……你在说什么标签?谢谢!
    • 我发现了我的问题!我正在尝试添加一个数组对象!这就是为什么这不起作用。你能给我一些关于如何解决这个问题的建议吗?我想添加一个 javascript 数组,这就是它破坏的原因
    【解决方案2】:

    我使用这些函数来获取或设置自定义属性

    // sets a custom property on the current Word file
    function setDocProperty (propName, propValue, callback) {
      Word.run(context => {
        context.document.properties.customProperties.add(propName, propValue)
        return context.sync()
          .then(() => {
            callback(null)
          })
          .catch(e => {
            callback(new Error(e))
          })
      })
    }
    
    // gets a custom property from the current Word file
    function getDocProperty (propName, callback) {
      Word.run(context => {
        var customDocProps = context.document.properties.customProperties
        // first, load custom properties object
        context.load(customDocProps)
        return context.sync()
          .then(function () {
            // now load actual property
            var filenameProp = customDocProps.getItemOrNullObject(propName)
            context.load(filenameProp)
            return context.sync()
              .then(() => {
                callback(null, filenameProp.value)
              })
              .catch(err => {
                callback(new Error(err))
              })
          })
          .catch(err => {
            callback(new Error(err))
          })
      })
    }
    

    你可以这样使用它们:

    setDocProperty('docId', 28, () => {
      console.log('property set') 
    })
    
    getDocProperty('docId', (err, value) => {
      if (err) {
        console.log('Error getting property', err)
      } else {
        console.log('the property is ' + value)
      }
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-08
      相关资源
      最近更新 更多