使用 .insertOoxml 插入 KeepNext

问题描述 投票:0回答:1

这里是初学者。我正在开发一个项目,通过使用 Script Lab 使用 Office Javascript API 创建 Word 加载项。

我在使用 .insertOoxml 编辑内容时遇到问题。特别是,我想创建一个函数来将我选择的所有段落保留在同一页面上。

由于(据我所知)段落对象中没有特定的方法,因此我尝试使用“.insertOoxml”方法直接在 XML 文件中插入 部分。问题是,无论我在哪里插入 部分,它都不会将其插入到我的文档中。

首先,我使用 MS 为 .insertOoxml 方法提供的示例,但我愿意接受任何其他解决方案来实现此目的。

非常感谢,

塞巴斯蒂安

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/50-document/manage-body.yaml
// Inserts OOXML at the beginning of this document.
// Run a batch operation against the Word object model.
await Word.run(async (context) => {

  // Create a proxy object for the document body.
  const body = context.document.body;

  // Queue a command to insert OOXML at 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/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:keepNext/>**<w:spacing w:before='360' w:after='0' w:line='480' w:lineRule='auto'/><w:rPr><w:color w:val='70AD47' w:themeColor='accent6'/><w:sz w:val='28'/></w:rPr></w:pPr><w:r><w:rPr><w:color w:val='70AD47' w:themeColor='accent6'/><w:sz w:val='28'/></w:rPr><w:t>This text has formatting directly applied to achieve its font size, color, line spacing, and paragraph spacing.</w:t></w:r></w:p></w:body></w:document></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.
  await context.sync();

  console.log("Added OOXML to the beginning of the document body.");
});

// Read "Understand when and how to use Office Open XML in your Word add-in" for guidance on working with OOXML.
// https://learn.microsoft.com/office/dev/add-ins/word/create-better-add-ins-for-word-with-office-open-xml

// The Word-Add-in-DocumentAssembly sample shows how you can use this API to assemble a document.
// https://github.com/OfficeDev/Word-Add-in-DocumentAssembly
javascript ms-word office-addins
1个回答
0
投票

AFAICS 发生这种情况的原因可能是 Word 将一个文档插入另一个文档时发生的情况(这实际上是您的代码正在执行的操作)。

假设您要插入 XML 的文档是空的。在这种情况下,即使它是“空”,但已经有一个“段落”(因为每个Word文档在至少一个Word部分中至少有一个段落),并且Word尝试不创建额外的一个。因此,实际上 Word 只是修改文档现有最后段落的文本,因此放弃“段落运行”属性,例如“与下一个保持一致”。

如果您修改 XML,以便在此段落后添加一个额外的空段落(例如,您在

 标记之前放置一个 
<w:p/> 元素),Word 会正确保留 Keep with next (
<w:keepNext/>
) 元素。

但随后它添加了一个您可能不想要的段落。

© www.soinside.com 2019 - 2024. All rights reserved.