Office JS - 将自定义属性添加到新文档
Office JS - Add customProperty to new document
我正在为 office (word) 开发一个插件,但我遇到了这个问题。我需要将自定义属性分配给将在新 window/instance.
中打开的新文档
我已经在为已经以这种方式打开的文档使用自定义属性:
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));
}
});
}
我怎样才能做到这一点?
我找到了解决办法,很简单。我将我的功能更改为:
getFileNew(attach, file) {
self.getDocumentAsBase64(attach.id).then(data => {
Word.run(context => {
var myNewDoc = context.application.createDocument(data);
myNewDoc.properties.load();
myNewDoc.properties.customProperties.add("fileId", file.id);
myNewDoc.properties.customProperties.add("fileName", file.name);
myNewDoc.properties.customProperties.add("attachId", attach.id);
myNewDoc.properties.customProperties.add("attachName", attach.name);
myNewDoc.open();
return context.sync()
}).catch(error => {
if (error instanceof OfficeExtension.Error) {
console.log(
"Error code and message: " + JSON.stringify(error.debugInfo)
);
}
});
});
}
旁注:这仅适用于桌面版。如果您想在 Office Online 的新 window 中打开文档,您必须省略 customProperties 否则它会抛出异常
我正在为 office (word) 开发一个插件,但我遇到了这个问题。我需要将自定义属性分配给将在新 window/instance.
中打开的新文档我已经在为已经以这种方式打开的文档使用自定义属性:
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));
}
});
}
我怎样才能做到这一点?
我找到了解决办法,很简单。我将我的功能更改为:
getFileNew(attach, file) {
self.getDocumentAsBase64(attach.id).then(data => {
Word.run(context => {
var myNewDoc = context.application.createDocument(data);
myNewDoc.properties.load();
myNewDoc.properties.customProperties.add("fileId", file.id);
myNewDoc.properties.customProperties.add("fileName", file.name);
myNewDoc.properties.customProperties.add("attachId", attach.id);
myNewDoc.properties.customProperties.add("attachName", attach.name);
myNewDoc.open();
return context.sync()
}).catch(error => {
if (error instanceof OfficeExtension.Error) {
console.log(
"Error code and message: " + JSON.stringify(error.debugInfo)
);
}
});
});
}
旁注:这仅适用于桌面版。如果您想在 Office Online 的新 window 中打开文档,您必须省略 customProperties 否则它会抛出异常