【问题标题】:Not able to add styled text with OOXML无法使用 OOXML 添加样式文本
【发布时间】: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


【解决方案1】:

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

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

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

以下是基本步骤。

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

那个sn-p会发生什么?重要的指令是这个:

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

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

显然:

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

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2022-10-14
    • 2016-12-26
    • 2019-02-04
    • 2021-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多