在Word加载项中使用字段

Word文档中的字段是占位符。 它允许你为内容而不是内容本身提供说明。 可以使用字段创建Word模板并设置其格式。 Word文档支持多种字段类型,其中许多类型具有用于配置字段的关联参数。 但是,web 上的Word通常不支持通过 UI 添加或编辑字段。 有关详细信息,请参阅 web Word 中的域代码

从 WordApi 1.5 要求集开始,Word JavaScript API 允许你管理Word加载项中的字段。 在所有平台上,都可以获取现有字段。 可以在支持这些功能的平台中插入、更新和删除字段。

以下部分讨论几种最常用的字段类型:Addin、Date、Hyperlink 和 TOC (目录) 。

Addin 字段

Addin 字段用于存储从Word用户界面隐藏的外接程序数据,而不管文档中的字段是设置为显示还是隐藏其内容。 “加载项”字段在Word UI 的“字段”对话框中不可用。 使用 API 插入 Addin 字段类型并设置字段的数据。

下面的代码示例演示如何在Word文档中的光标位置或所选内容之前插入 Addin 字段。

// Inserts an Addin field before selection.
async function rangeInsertAddinField() {
  await Word.run(async (context) => {
    let range = context.document.getSelection().getRange();
    const field = range.insertField(Word.InsertLocation.before, Word.FieldType.addin);
    field.load("result,code");
    await context.sync();

    if (field.isNullObject) {
      console.log("There are no fields in this document.");
    } else {
      console.log("Code of the field: " + field.code);
      console.log("Result of the field: " + JSON.stringify(field.result));
    }
  });
}

下面的代码示例演示如何获取文档中找到的第一个 Addin 字段,然后设置该字段的数据属性。

// Gets the first Addin field in the document and sets its data.
async function getFirstAddinFieldAndSetData() {
  await Word.run(async (context) => {
    let myFieldTypes = new Array();
    myFieldTypes[0] = Word.FieldType.addin;
    const addinFields = context.document.body.fields.getByTypes(myFieldTypes);
    let fields = addinFields.load("items");
    await context.sync();

    if (fields.items.length === 0) {
      console.log("No Addin fields in this document.");
    } else {
      fields.load();
      await context.sync();

      const firstAddinField = fields.items[0];
      firstAddinField.load("code,result,data");
      await context.sync();

      console.log("The data of the Addin field before being set:", firstAddinField.data);
      const data = "Insert your data here";
      //const data = $("#input-reference").val(); // Or get user data from your add-in's UI.
      firstAddinField.data = data;
      firstAddinField.load("data");
      await context.sync();

      console.log("The data of the Addin field after being set:", firstAddinField.data);
    }
  });
}

日期字段

“日期”字段根据指定的格式插入当前日期。 通过将字段属性分别设置为 showCodesfalse 或 ,可以在显示日期或 true 字段代码之间切换。

下面的代码示例演示如何在Word文档中的光标位置或所选内容之前插入 Date 字段。

// Inserts a Date field before selection.
async function rangeInsertDateField() {
  await Word.run(async (context) => {
    let range = context.document.getSelection().getRange();
    const field = range.insertField(
      Word.InsertLocation.before,
      Word.FieldType.date,
     '\\@ "M/d/yyyy h:mm am/pm"',
     true
    );
    field.load("result,code");
    await context.sync();

    if (field.isNullObject) {
      console.warn("The field wasn't inserted as expected.");
    } else {
      console.log("Code of the field: " + field.code);
      console.log("Result of the field: " + JSON.stringify(field.result));
    }
  });
}

进一步阅读

“超链接”字段插入同一文档中某个位置或外部位置(如网页)的地址。 当用户选择它时,他们将导航到指定位置。 通过将字段属性分别设置为 showCodesfalse 或 ,可以在显示超链接地址或 true 字段代码之间切换。

下面的代码示例演示如何在Word文档中的光标位置或所选内容之前插入超链接字段。

// Inserts a Hyperlink field before selection.
async function rangeInsertHyperlinkField() {
  await Word.run(async (context) => {
    let range = context.document.getSelection().getRange();
    const field = range.insertField(
      Word.InsertLocation.before,
      Word.FieldType.hyperlink,
      "https://bing.com",
      true
    );
    field.load("result,code");
    await context.sync();

    if (field.isNullObject) {
      console.warn("The field wasn't inserted as expected.");
    } else {
      console.log("Code of the field: " + field.code);
      console.log("Result of the field: " + JSON.stringify(field.result));
    }
  });
}

进一步阅读

目录 (目录) 字段

TOC 字段插入目录,其中列出了文档的某些区域(如标题)。 通过将字段属性false分别设置为 showCodes 或 ,可以在显示目录或true字段代码之间切换。

下面的代码示例演示如何在光标位置插入 TOC 字段或替换Word文档中的当前选定内容。

/**
 1. Run setup.
 2. Select "[To place table of contents]" paragraph.
 3. Run rangeInsertTOCField.
 */

// Inserts a TOC (table of contents) field replacing selection.
async function rangeInsertTOCField() {
  await Word.run(async (context) => {
    let range = context.document.getSelection().getRange();
    const field = range.insertField(
      Word.InsertLocation.replace,
      Word.FieldType.toc
    );
    field.load("result,code");
    await context.sync();

    if (field.isNullObject) {
      console.warn("The field wasn't inserted as expected.");
    } else {
      console.log("Code of the field: " + field.code);
      console.log("Result of the field: " + JSON.stringify(field.result));
    }
  });
}

// Prep document so there'll be elements that could be included in a table of contents.
async function setup() {
  await Word.run(async (context) => {
    const body: Word.Body = context.document.body;
    body.clear();
    body.insertParagraph("Document title", "End").styleBuiltIn = Word.BuiltInStyleName.title;
    body.insertParagraph("[To place table of contents]", "End").styleBuiltIn = Word.BuiltInStyleName.normal;
    body.insertParagraph("Introduction", "End").styleBuiltIn = Word.BuiltInStyleName.heading1;
    body.insertParagraph("Paragraph 1", "End").styleBuiltIn = Word.BuiltInStyleName.normal;
    body.insertParagraph("Topic 1", "End").styleBuiltIn = Word.BuiltInStyleName.heading1;
    body.insertParagraph("Paragraph 2", "End").styleBuiltIn = Word.BuiltInStyleName.normal;
    body.insertParagraph("Topic 2", "End").styleBuiltIn = Word.BuiltInStyleName.heading1;
    body.insertParagraph("Paragraph 3", "End").styleBuiltIn = Word.BuiltInStyleName.normal;
  });
}

进一步阅读

另请参阅