【发布时间】:2021-08-23 12:26:44
【问题描述】:
我正在为 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.');
});
【问题讨论】:
-
请用Script Lab重现问题。然后将要点导出到 GitHub,并将要点的链接添加到您的问题中。这使人们能够尝试重现该问题。此外,您需要提供更多“无法正常工作”的细节。究竟出了什么问题?
-
我添加了要点链接和“详细信息”
-
我可以复制。我正在 Microsoft 内部寻求帮助。
-
n同时,您是否熟悉这篇文章,尤其是关于样式的部分? Create better add-ins for Word with Office Open XML
-
是的,我通过那篇文章获得了示例。在文章中,有一个带有 OOXML 代码的归档 git repo 的链接,其中包含我尝试过但没有用的样式。 github.com/OfficeDev/Word-Add-in-Load-and-write-Open-XML
标签: office-js openxml office-addins word office-js-helpers