我正在为办公室(word)开发一个插件,但我遇到了这个问题。我需要将自定义属性分配给将在新窗口/实例中打开的新文档。
我已经为已经以这种方式打开的文档使用了自定义属性:
setProperty(propName, propValue) {
Word.run(context => {
context.document.properties.customProperties.add(propName, propValue);
return context.sync();
}).catch(error => {
if (error instanceof OfficeExtension.Error) {
console.log(
"Error code and message: " + JSON.stringify(error.debugInfo)
);
}
});
}
getFileOverride(attach, file) {
self.getDocumentAsBase64(attach.id).then(data => {
Word.run(context => {
let body = context.document.body;
body.insertFileFromBase64(data, Word.InsertLocation.replace);
return context
.sync()
.then(() => {
self.setProperty("fileId", file.id);
self.setProperty("attachId", attach.id);
})
.then(context.sync)
.catch(error => {
self.updateStatus("Error al obtener el archivo");
if (error instanceof OfficeExtension.Error) {
console.log(
"Error code and message: " + JSON.stringify(error.debugInfo)
);
}
});
}).catch(error => {
if (error instanceof OfficeExtension.Error) {
console.log(
"Error code and message: " + JSON.stringify(error.debugInfo)
);
}
});
});
}
但是当我创建一个新文档时,我不知道如何实现这一点。我尝试了以下方法,但给出了一般异常错误:
getDocumentAsBase64(function(data) {
Word.run(function(context) {
var myNewDoc = context.application.createDocument(data);
context.load(myNewDoc);
return context
.sync()
.then(function() {
myNewDoc.properties.load();
myNewDoc.properties.customProperties.add("custom", "prop");
myNewDoc.open();
})
.then(context.sync)
.catch(function(myError) {
//otherwise we handle the exception here!
updateStatus(myError.message);
});
}).catch(function(myError) {
updateStatus(myError.message);
});
});
我试过制作一个类似于setProperty的函数,但它没有添加属性:
function setExternalProperty(document, propName, propValue) {
Word.run(context => {
document.properties.load();
document.properties.customProperties.add("custom", "prop");
return context.sync();
}).catch(error => {
if (error instanceof OfficeExtension.Error) {
console.log("Error code and message: " + JSON.stringify(error.debugInfo));
}
});
}
我怎样才能做到这一点?