(JavaScript API 1.3 for Office) Custom Properties GetItemOrNull

前端 未结 2 450
不思量自难忘°
不思量自难忘° 2021-01-22 13:44

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

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

2条回答
  •  别那么骄傲
    2021-01-22 14:44

    I use these function to get or set custom properties

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

    You use them like this:

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

提交回复
热议问题