【发布时间】:2015-04-30 02:28:51
【问题描述】:
是否有用于 Office 365 的 Javascript API 来操作 Word 文档中的段落、表格或文本?
我想使用 API 搜索文档中的某些特定文本/字段并替换它们。
【问题讨论】:
-
在 Stack Overflow 中搜索 API / 库是题外话。试试谷歌/MSDN。
-
已经完成但效果不佳。@Raptor
标签: office365
是否有用于 Office 365 的 Javascript API 来操作 Word 文档中的段落、表格或文本?
我想使用 API 搜索文档中的某些特定文本/字段并替换它们。
【问题讨论】:
标签: office365
一种选择是获取文档为ooxml,然后使用openXml 遍历并替换对象。伪代码:
function getParas() {
var documentText = null;
Office.context.document.getFileAsync("compressed", function (result) {
if (result.status == "succeeded") {
//Get the file
var myFile = result.value;
myFile.getSliceAsync(0, function (resultSlice) {
if (result.status == "succeeded") {
//We got the file slice. Now we will encode the data and post to a service
documentText = OSF.OUtil.encodeBase64(resultSlice.value.data);
var doc = new openXml.OpenXmlPackage(documentText);
// Replace the first paragraph in the document with the new paragraph.
var allParas = doc.mainDocumentPart().getXDocument().descendants(W.p);
// .firstOrDefault().toString();
for (var i = 0; i < allParas.count(); i++)
{
var Para = allParas.elementAt(i);
var ParaText = Para.toString();
app.showNotification(ParaText);
}
}
});
myFile.closeAsync();
}
})
}
【讨论】: