office-js - 无法使用 OOXML 添加样式文本
问题描述
我正在为 Word 创建一个 Office 插件,并且我正在尝试为用户创建自定义样式来设置其文本的样式。我遇到了这个样式化文本的例子,然后自己去尝试了。
这是要点代码:https ://gist.github.com/thomas-idgis/286cd1526b2fbf5bf7359b0dd54464ac
它在单击运行时创建的文本应该设置样式,但没有设置样式,这是问题,因为我希望它被设置样式,因为它应该从 OOXML 代码中设置样式,我不知道为什么它是错误的。
我无法让它工作,是示例错误还是我的代码有问题?
// Create a proxy object for the document body.
var body = context.document.body;
// Queue a command to insert OOXML in to the beginning of the body.
body.insertOoxml(
`<pkg:package xmlns:pkg="http://schemas.microsoft.com/office/2006/xmlPackage">
<pkg:part pkg:name="/_rels/.rels" pkg:contentType="application/vnd.openxmlformats-package.relationships+xml" pkg:padding="512">
<pkg:xmlData>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/>
</Relationships>
</pkg:xmlData>
</pkg:part>
<pkg:part pkg:name="/word/_rels/document.xml.rels" pkg:contentType="application/vnd.openxmlformats-package.relationships+xml" pkg:padding="256">
<pkg:xmlData>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/>
</Relationships>
</pkg:xmlData>
</pkg:part>
<pkg:part pkg:name="/word/document.xml" pkg:contentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml">
<pkg:xmlData>
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:body>
<w:p>
<w:pPr>
<w:pStyle w:val="TestParagraphStyle"/>
</w:pPr>
<w:r>
<w:t xml:space="preserve">This text should be styled</w:t>
</w:r>
</w:p>
</w:body>
</w:document>
</pkg:xmlData>
</pkg:part>
<pkg:part pkg:name="/word/styles.xml" pkg:contentType="application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml">
<pkg:xmlData>
<w:styles xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" >
<w:style w:type="paragraph" w:styleId="TestParagraphStyle">
<w:name w:val="Test Paragraph Style"/>
<w:pPr>
<w:spacing w:line="480" w:lineRule="auto"/>
<w:ind w:firstLine="1440"/>
</w:pPr>
<w:rPr>
<w:rFonts w:ascii="Courier New" w:hAnsi="Courier New"/>
<w:color w:val="FFF200"/>
<w:sz w:val="40"/>
</w:rPr>
</w:style>
</w:styles>
</pkg:xmlData>
</pkg:part>
</pkg:package>
`,
Word.InsertLocation.start
);
// Synchronize the document state by executing the queued commands,
// and return a promise to indicate task completion.
return context.sync().then(function () {
console.log('OOXML added to the beginning of the document body.');
});
解决方案
我们没有在 Word.js 中创建新样式的 API,这是一个差距。然而,有几种方法可以实现这一点。
- 通过 insertFileFromBase64 API(插入包含您需要的所有自定义样式的文档)
- 您实际上也可以通过 OOXML 插入来实现它,但稍后会详细介绍。(您只需要正确的 OOOXML 并确保您不重复任何现有的样式名称(文档中当前存在的默认样式名称或自定义名称)
在这个答案上,我想展示第一种方法:)
以下是基本步骤。
- 创建一个 Word 文档并添加您需要的所有自定义样式。确保在此测试文档中使用所有样式非常重要,只需为每种样式添加一些文本。
- 将文档另存为 docx,我们稍后将使用它。
- 现在打开一个新文档并加载 这个脚本实验室片段。(选择插入文档选项)
- 点击按钮,会提示插入文件,选择插入上一步创建的文件即可(包含所有使用的样式)
- 瞧!你应该有你需要的所有样式。
该片段会发生什么?重要的指令是这个:
context.document.body.insertFileFromBase64(mybase64, "end").getRange().clear();
那行代码实际上是插入文件,然后通过从插入中获取范围并删除它来清除插入的内容。但是样式保留在文档中!现在你可以使用它们了:)
显然:
- 您不需要弹出对话框来插入此文档,您需要将文档编码为 base64 并发送 base-64 编码字符串。
- 如果您想对 INsertOOXML 方法执行相同的操作,则需要将 doc 保存为 OOXML 而不是 docx,它也应该可以工作。
- 最后,再次确保您不要使用现有名称,因为这会导致样式冲突并且将使用原始名称。
希望这可以帮助。
推荐阅读
- jspdf-autotable - 创建具有固定列宽确定表格宽度的 jsPDF-Autotable
- html - 如何在两个部分之间定位图像?
- javascript - 如何在AngularJs中的方法调用后绑定模型?
- jquery - 使用 Laravel 中控制器的 JSON 响应自定义错误
- c++ - 制作一个小于十的不同随机整数的列表
- android - 如何解决android studio 3.4以上版本中的清单合并文件?
- sql - 如何将变量连接到postgresql中的其他变量?
- c++ - 是否需要互斥锁才能将参数传递给 pthread?
- python - 以模式排序列表
- sql - 蜂巢查询以查找客户从当前交易日期起过去 10 天内的交易总数