首页 > 解决方案 > 无法使用 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.');
    });

标签: office-jsopenxmloffice-addinswordoffice-js-helpers

解决方案


我们没有在 Word.js 中创建新样式的 API,这是一个差距。然而,有几种方法可以实现这一点。

  1. 通过 insertFileFromBase64 API(插入包含您需要的所有自定义样式的文档)
  2. 您实际上也可以通过 OOXML 插入来实现它,但稍后会详细介绍。(您只需要正确的 OOOXML 并确保您不重复任何现有的样式名称(文档中当前存在的默认样式名称或自定义名称)

在这个答案上,我想展示第一种方法:)

以下是基本步骤。

  1. 创建一个 Word 文档并添加您需要的所有自定义样式。确保在此测试文档中使用所有样式非常重要,只需为每种样式添加一些文本
  2. 将文档另存为 docx,我们稍后将使用它。
  3. 现在打开一个新文档并加载 这个脚本实验室片段。(选择插入文档选项)
  4. 点击按钮,会提示插入文件,选择插入上一步创建的文件即可(包含所有使用的样式)
  5. 瞧!你应该有你需要的所有样式。

该片段会发生什么?重要的指令是这个:

context.document.body.insertFileFromBase64(mybase64, "end").getRange().clear();

那行代码实际上是插入文件,然后通过从插入中获取范围并删除它来清除插入的内容。但是样式保留在文档中!现在你可以使用它们了:)

显然:

  1. 您不需要弹出对话框来插入此文档,您需要将文档编码为 base64 并发送 base-64 编码字符串。
  2. 如果您想对 INsertOOXML 方法执行相同的操作,则需要将 doc 保存为 OOXML 而不是 docx,它也应该可以工作。
  3. 最后,再次确保您不要使用现有名称,因为这会导致样式冲突并且将使用原始名称。

希望这可以帮助。


推荐阅读