(JavaScript API 1.3 for Office)自定义属性 GetItemOrNull
(JavaScript API 1.3 for Office) Custom Properties GetItemOrNull
几天前我创建了 ,在其中我需要有关如何将自定义属性添加到所述文档的帮助。
首先,我是运行 Word 1701(7766.2047)。
假设我有一个方法,其中我 return 一个所述的自定义 属性。首先,我会检查自定义 属性 是否已经创建。我会用一个简单的 getItemOrNullObject(key) 和..
- If returns null Then simply create it AND return it
- 否则return它
据我了解,我需要执行 return context.sync()。然后为对象实际加载数据?我是不是做的太多了 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();
});
用新值覆盖旧值?
如果您读到这里,谢谢!任何帮助表示赞赏。
干杯!
感谢您提出这个问题。
除了一个小细节外,您的代码是正确的:所有 *getItemOrNullObject 方法都执行 NOT return a JavaScript null,因此您的 "if (!temp)" 语句不会像您预期的那样工作。如果你想验证存在,你需要调用 if(temp.isNullObject) 来代替。
还有一些建议:
- customProperties.add() 语义是如果 属性 确实存在,它将被替换。所以,如果你想创建或更改 属性 你不需要检查它是否存在。如果你想读取它的当前值,你可以这样做。这回答了你的第二个问题。
- 如果您有兴趣加载单个 属性。
,我对您的代码有一个简化且更有效的建议
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);
})
我们将更新文档,因为这似乎令人困惑。
谢谢!
我使用这些函数来获取或设置自定义属性
// 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)
}
})
几天前我创建了
首先,我是运行 Word 1701(7766.2047)。
假设我有一个方法,其中我 return 一个所述的自定义 属性。首先,我会检查自定义 属性 是否已经创建。我会用一个简单的 getItemOrNullObject(key) 和..
- If returns null Then simply create it AND return it
- 否则return它
据我了解,我需要执行 return context.sync()。然后为对象实际加载数据?我是不是做的太多了 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();
});
用新值覆盖旧值?
如果您读到这里,谢谢!任何帮助表示赞赏。 干杯!
感谢您提出这个问题。
除了一个小细节外,您的代码是正确的:所有 *getItemOrNullObject 方法都执行 NOT return a JavaScript null,因此您的 "if (!temp)" 语句不会像您预期的那样工作。如果你想验证存在,你需要调用 if(temp.isNullObject) 来代替。
还有一些建议:
- customProperties.add() 语义是如果 属性 确实存在,它将被替换。所以,如果你想创建或更改 属性 你不需要检查它是否存在。如果你想读取它的当前值,你可以这样做。这回答了你的第二个问题。
- 如果您有兴趣加载单个 属性。 ,我对您的代码有一个简化且更有效的建议
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);
})
我们将更新文档,因为这似乎令人困惑。
谢谢!
我使用这些函数来获取或设置自定义属性
// 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)
}
})