Office.Body interface

body 对象提供用于添加和更新邮件或约会内容的方法。 它将在所选项目的 body 属性中返回。

注解

[ API set: Mailbox 1.1 ]

HTML 表格边框颜色的已知问题

Windows 上的 Outlook:如果在撰写模式下将 HTML 表格中的各种单元格边框设置为不同的颜色,则单元格的边框可能不会反映预期的颜色。 有关已知行为,请访问 OfficeDev/office-js 问题 #1818

最低权限级别读取项

适用的 Outlook 模式:撰写或阅读

方法

getAsync(coercionType, options, callback)

使用指定的格式返回当前正文。

此方法以 指定的 coercionType格式返回整个当前正文。

getAsync(coercionType, callback)

使用指定的格式返回当前正文。

此方法以 指定的 coercionType格式返回整个当前正文。

getTypeAsync(options, callback)

获取一个值,该值指示内容采用 HTML 格式还是文本格式。

getTypeAsync(callback)

获取一个值,该值指示内容采用 HTML 格式还是文本格式。

prependAsync(data, options, callback)

将指定内容添加到项目正文开头。

prependAsync(data, callback)

将指定内容添加到项目正文开头。

setAsync(data, options, callback)

将整个正文替换为指定的文本。

setAsync(data, callback)

将整个正文替换为指定的文本。

setSelectedDataAsync(data, options, callback)

将正文中的所选内容更换为指定文本。

setSelectedDataAsync 方法将指定的字符串插入项目正文中的光标位置,或者,如果在编辑器中选定了文本,它就会替换所选文本。 如果光标从未出现在项目正文中,或者如果该项目的正文不关注 UI,该字符串将插入到正文内容的顶部。 插入后,光标将置于插入内容的末尾。

在 HTML 标记中包含链接时,可以通过将定位点上的 属性 (<>) 设置为id“LPNoLP”来禁用联机链接预览, (有关示例) ,请参阅示例部分。

建议:调用 getTypeAsync ,然后将返回的值传递给 options.coercionType 参数。

setSelectedDataAsync(data, callback)

将正文中的所选内容更换为指定文本。

setSelectedDataAsync 方法将指定的字符串插入项目正文中的光标位置,或者,如果在编辑器中选定了文本,它就会替换所选文本。 如果光标从未出现在项目正文中,或者如果该项目的正文不关注 UI,该字符串将插入到正文内容的顶部。 插入后,光标将置于插入内容的末尾。

在 HTML 标记中包含链接时,可以通过将定位点上的 属性 (<>) 设置为id“LPNoLP”来禁用联机链接预览, (有关示例) ,请参阅示例部分。

建议:调用 getTypeAsync ,然后将返回的值传递给 options.coercionType 参数。

方法详细信息

getAsync(coercionType, options, callback)

使用指定的格式返回当前正文。

此方法以 指定的 coercionType格式返回整个当前正文。

getAsync(coercionType: Office.CoercionType | string, options: Office.AsyncContextOptions, callback?: (asyncResult: Office.AsyncResult<string>) => void): void;

参数

coercionType

Office.CoercionType | string

用于返回的正文的格式。

options
Office.AsyncContextOptions

包含以下一个或多个属性的对象文本:- asyncContext:开发人员可以在回调函数中提供他们想要访问的任何对象。

callback

(asyncResult: Office.AsyncResult<string>) => void

可选。 方法完成后,使用 Office.AsyncResult 类型的单个参数调用在 参数中 callback 传递的函数。 正文在 属性中以请求的格式 asyncResult.value 提供。

返回

void

注解

[ API 集:邮箱 1.3 ]

最低权限级别读取项

适用的 Outlook 模式:撰写或阅读

重要提示:使用 HTML 格式的正文时,请务必注意,方法返回 Body.getAsync 的值不一定与之前在 方法中 Body.setAsync 传递的值完全相同。 客户端可以修改传递给 setAsync 的值,使其通过其呈现引擎高效呈现。

示例

// This example gets the body of the item as plain text.
Office.context.mailbox.item.body.getAsync(
    "text",
    { asyncContext: "This is passed to the callback" },
    function callback(result) {
        // Do something with the result.
    });

// The following is an example of the result parameter passed to the callback function.
{
    "value": "TEXT of whole body (including threads below)",
    "status": "succeeded",
    "asyncContext": "This is passed to the callback"
}

getAsync(coercionType, callback)

使用指定的格式返回当前正文。

此方法以 指定的 coercionType格式返回整个当前正文。

getAsync(coercionType: Office.CoercionType | string, callback?: (asyncResult: Office.AsyncResult<string>) => void): void;

参数

coercionType

Office.CoercionType | string

用于返回的正文的格式。

callback

(asyncResult: Office.AsyncResult<string>) => void

可选。 方法完成后,使用 Office.AsyncResult 类型的单个参数调用在 参数中 callback 传递的函数。 正文在 属性中以请求的格式 asyncResult.value 提供。

返回

void

注解

[ API 集:邮箱 1.3 ]

最低权限级别读取项

适用的 Outlook 模式:撰写或阅读

重要提示:使用 HTML 格式的正文时,请务必注意,方法返回 Body.getAsync 的值不一定与之前在 方法中 Body.setAsync 传递的值完全相同。 客户端可以修改传递给 setAsync 的值,使其通过其呈现引擎高效呈现。

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/outlook/20-item-body/add-inline-base64-image.yaml

const mailItem = Office.context.mailbox.item;
const base64String =
  "iVBORw0KGgoAAAANSUhEUgAAAGAAAABgCAMAAADVRocKAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAnUExURQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAN0S+bUAAAAMdFJOUwAQIDBAUI+fr7/P7yEupu8AAAAJcEhZcwAADsMAAA7DAcdvqGQAAAF8SURBVGhD7dfLdoMwDEVR6Cspzf9/b20QYOthS5Zn0Z2kVdY6O2WULrFYLBaLxd5ur4mDZD14b8ogWS/dtxV+dmx9ysA2QUj9TQRWv5D7HyKwuIW9n0vc8tkpHP0W4BOg3wQ8wtlvA+PC1e8Ao8Ld7wFjQtHvAiNC2e8DdqHqKwCrUPc1gE1AfRVgEXBfB+gF0lcCWoH2tYBOYPpqQCNwfT3QF9i+AegJfN8CtAWhbwJagtS3AbIg9o2AJMh9M5C+SVGBvx6zAfmT0r+Bv8JMwP4kyFPir+cswF5KL3WLv14zAFBCLf56Tw9cparFX4upgaJUtPhrOS1QlY5W+vWTXrGgBFB/b72ev3/0igUdQPppP/nfowfKUUEFcP207y/yxKmgAYQ+PywoAFOfCH3A2MdCFzD3kdADBvq10AGG+pXQBgb7pdAEhvuF0AIc/VtoAK7+JciAs38KIuDugyAC/v4hiMCE/i7IwLRBsh68N2WQjMVisVgs9i5bln8LGScNcCrONQAAAABJRU5ErkJggg==";

// Get the current body of the message or appointment.
mailItem.body.getAsync(Office.CoercionType.Html, (bodyResult) => {
  if (bodyResult.status === Office.AsyncResultStatus.Succeeded) {
    // Insert the Base64 image to the beginning of the body.
    const options = { isInline: true, asyncContext: bodyResult.value };
    mailItem.addFileAttachmentFromBase64Async(base64String, "sample.png", options, (attachResult) => {
      if (attachResult.status === Office.AsyncResultStatus.Succeeded) {
        let body = attachResult.asyncContext;
        body = body.replace("<p class=MsoNormal>", `<p class=MsoNormal><img src="cid:sample.png">`);

        mailItem.body.setAsync(body, { coercionType: Office.CoercionType.Html }, (setResult) => {
          if (setResult.status === Office.AsyncResultStatus.Succeeded) {
            console.log("Inline Base64 image added to the body.");
          } else {
            console.log(setResult.error.message);
          }
        });
      } else {
        console.log(attachResult.error.message);
      }
    });
  } else {
    console.log(bodyResult.error.message);
  }
});

getTypeAsync(options, callback)

获取一个值,该值指示内容采用 HTML 格式还是文本格式。

getTypeAsync(options: Office.AsyncContextOptions, callback?: (asyncResult: Office.AsyncResult<Office.CoercionType>) => void): void;

参数

options
Office.AsyncContextOptions

包含以下一个或多个属性的对象文本:- asyncContext:开发人员可以在回调函数中提供他们想要访问的任何对象。

callback

(asyncResult: Office.AsyncResult<Office.CoercionType>) => void

可选。 方法完成后,使用类型的Office.AsyncResult单个参数调用在 参数中callback传递的函数。 内容类型作为 属性中的CoercionTypeasyncResult.value值之一返回。

返回

void

注解

[ API set: Mailbox 1.1 ]

最低权限级别读取项

适用的 Outlook 模式:撰写

重要提示:在 Android 版 Outlook 和 iOS 版中,邮件撰写模式不支持此方法。 仅支持约会组织者模式。 有关 Outlook mobile 中支持的 API 的详细信息,请参阅移动设备上的 Outlook 中支持的 Outlook JavaScript API

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/outlook/20-item-body/get-body-format.yaml

// Get the mail item's body format (plain text or HTML) and log it to the console.
Office.context.mailbox.item.body.getTypeAsync((asyncResult) => {
  if (asyncResult.status === Office.AsyncResultStatus.Failed) {
    console.log("Action failed with error: " + asyncResult.error.message);
    return;
  }

  console.log("Body format: " + asyncResult.value);
});

getTypeAsync(callback)

获取一个值,该值指示内容采用 HTML 格式还是文本格式。

getTypeAsync(callback?: (asyncResult: Office.AsyncResult<Office.CoercionType>) => void): void;

参数

callback

(asyncResult: Office.AsyncResult<Office.CoercionType>) => void

可选。 方法完成后,使用类型的Office.AsyncResult单个参数调用在 参数中callback传递的函数。 内容类型作为 属性中的CoercionTypeasyncResult.value值之一返回。

返回

void

注解

[ API set: Mailbox 1.1 ]

最低权限级别读取项

适用的 Outlook 模式:撰写

重要提示:在 Android 版 Outlook 和 iOS 版中,邮件撰写模式不支持此方法。 仅支持约会组织者模式。 有关 Outlook mobile 中支持的 API 的详细信息,请参阅移动设备上的 Outlook 中支持的 Outlook JavaScript API

prependAsync(data, options, callback)

将指定内容添加到项目正文开头。

prependAsync(data: string, options: Office.AsyncContextOptions & CoercionTypeOptions, callback?: (asyncResult: Office.AsyncResult<void>) => void): void;

参数

data

string

将插入到正文开头的字符串。 字符串大小限制为 1,000,000 个字符。

options

Office.AsyncContextOptions & Office.CoercionTypeOptions

包含以下一个或多个属性的对象文本:- asyncContext:开发人员可以在回调函数中提供他们想要访问的任何对象。 coercionType:正文的所需格式。 参数中的字符串将转换为此格式。

callback

(asyncResult: Office.AsyncResult<void>) => void

可选。 方法完成后,使用类型的Office.AsyncResult单个参数调用在 参数中callback传递的函数。 asyncResult.error 属性中将提供遇到的所有错误。

返回

void

注解

[ API set: Mailbox 1.1 ]

最低权限级别读/写项

适用的 Outlook 模式:撰写

重要说明

  • 在内容预置后,游标的位置取决于加载项运行的客户端。 在 Outlook 网页版 和 Windows 中,光标位置在正文的预先存在内容中保持不变。 例如,如果光标在调用之前 prependAsync 定位在正文的开头,则它将在调用后预先附加的内容和预先存在的正文内容之间显示。 在 Outlook on Mac 中,不会保留光标位置。 光标在调用后消失, prependAsync 仅在用户选择邮件项目正文中的某些内容时重新出现。

  • 使用 HTML 格式的正文时,请务必注意,客户端可以修改传递给 prependAsync 的值,使其通过其呈现引擎高效呈现。 这意味着,在 Mailbox 1.3) 中引入的方法的后续调用 Body.getAsync (返回的值不一定包含上 prependAsync 一次调用中传递的确切值。

  • 在 HTML 标记中包含链接时,可以通过将定位点上的 属性 (<>) 设置为id“LPNoLP”来禁用联机链接预览, (有关示例) ,请参阅示例部分。

  • 在 Android 版 Outlook 和 iOS 版中,邮件撰写模式不支持此方法。 仅支持约会组织者模式。 有关 Outlook mobile 中支持的 API 的详细信息,请参阅移动设备上的 Outlook 中支持的 Outlook JavaScript API

建议:调用 getTypeAsync,然后将返回的值传递给 options.coercionType 参数。

错误

  • DataExceedsMaximumSize:数据参数长度超过 1,000,000 个字符。

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/outlook/20-item-body/prepend-text-to-item-body.yaml

/* This snippet adds text to the beginning of the message or appointment's body. 
  
  When prepending a link in HTML markup to the body, you can disable the online link preview by setting the anchor tag's id attribute to "LPNoLP". For example, '<a id="LPNoLP" href="http://www.contoso.com">Click here!</a>'.
*/
const text = $("#text-field").val();

// It's recommended to call getTypeAsync and pass its returned value to the options.coercionType parameter of the prependAsync call.
Office.context.mailbox.item.body.getTypeAsync((asyncResult) => {
  if (asyncResult.status === Office.AsyncResultStatus.Failed) {
    console.log("Action failed with error: " + asyncResult.error.message);
    return;
  }

  const bodyFormat = asyncResult.value;
  Office.context.mailbox.item.body.prependAsync(text, { coercionType: bodyFormat }, (asyncResult) => {
    if (asyncResult.status === Office.AsyncResultStatus.Failed) {
      console.log("Action failed with error: " + asyncResult.error.message);
      return;
    }

    console.log(`"${text}" prepended to the body.`);
  });
});

prependAsync(data, callback)

将指定内容添加到项目正文开头。

prependAsync(data: string, callback?: (asyncResult: Office.AsyncResult<void>) => void): void;

参数

data

string

将插入到正文开头的字符串。 字符串大小限制为 1,000,000 个字符。

callback

(asyncResult: Office.AsyncResult<void>) => void

可选。 方法完成后,使用类型的Office.AsyncResult单个参数调用在 参数中callback传递的函数。 asyncResult.error 属性中将提供遇到的所有错误。

返回

void

注解

[ API set: Mailbox 1.1 ]

最低权限级别读/写项

适用的 Outlook 模式:撰写

重要说明

  • 在内容预置后,游标的位置取决于加载项运行的客户端。 在 Outlook 网页版 和 Windows 中,光标位置在正文的预先存在内容中保持不变。 例如,如果光标在调用之前 prependAsync 定位在正文的开头,则它将在调用后预先附加的内容和预先存在的正文内容之间显示。 在 Outlook on Mac 中,不会保留光标位置。 光标在调用后消失, prependAsync 仅在用户选择邮件项目正文中的某些内容时重新出现。

  • 使用 HTML 格式的正文时,请务必注意,客户端可以修改传递给 prependAsync 的值,使其通过其呈现引擎高效呈现。 这意味着,在 Mailbox 1.3) 中引入的方法的后续调用 Body.getAsync (返回的值不一定包含上 prependAsync 一次调用中传递的确切值。

  • 在 HTML 标记中包含链接时,可以通过将定位点上的 属性 (<>) 设置为id“LPNoLP”来禁用联机链接预览, (有关示例) ,请参阅示例部分。

  • 在 Android 版 Outlook 和 iOS 版中,邮件撰写模式不支持此方法。 仅支持约会组织者模式。 有关 Outlook mobile 中支持的 API 的详细信息,请参阅移动设备上的 Outlook 中支持的 Outlook JavaScript API

建议:调用 getTypeAsync,然后将返回的值传递给 options.coercionType 参数。

错误

  • DataExceedsMaximumSize:数据参数长度超过 1,000,000 个字符。

setAsync(data, options, callback)

将整个正文替换为指定的文本。

setAsync(data: string, options: Office.AsyncContextOptions & CoercionTypeOptions, callback?: (asyncResult: Office.AsyncResult<void>) => void): void;

参数

data

string

将替换现有正文的字符串。 字符串限制为 1,000,000 个字符。

options

Office.AsyncContextOptions & Office.CoercionTypeOptions

包含以下一个或多个属性的对象文本:- asyncContext:开发人员可以在回调函数中提供他们想要访问的任何对象。 coercionType:正文的所需格式。 参数中的字符串将转换为此格式。

callback

(asyncResult: Office.AsyncResult<void>) => void

可选。 方法完成后,使用 Office.AsyncResult 类型的单个参数调用在 参数中 callback 传递的函数。 asyncResult.error 属性中将提供遇到的所有错误。

返回

void

注解

[ API 集:邮箱 1.3 ]

最低权限级别读/写项

适用的 Outlook 模式:撰写

重要说明

  • 将正文替换为指定内容后,光标的位置取决于加载项运行的客户端。 在 Outlook on Windows 中,光标显示在邮件项目正文的开头,而在Outlook 网页版中,光标显示在邮件项目正文的末尾。 在 Outlook on Mac 中,不会保留光标位置。 光标在调用后消失, prependAsync 仅在用户选择邮件项目正文中的某些内容时重新出现。

  • 使用 HTML 格式的正文时,请务必注意,方法返回 Body.getAsync 的值不一定与之前在 方法中 Body.setAsync 传递的值完全相同。 客户端可以修改传递给 setAsync 的值,使其通过其呈现引擎高效呈现。

  • 在 HTML 标记中包含链接时,可以通过将定位点上的 属性 (<>) 设置为id“LPNoLP”来禁用联机链接预览, (有关示例) ,请参阅示例部分。

  • 在 Outlook on Windows 和 Mac 中,加载项用户无法使用“撤消”命令还原此操作。

  • 在 Android 版 Outlook 和 iOS 版中,邮件撰写模式不支持此方法。 仅支持约会组织者模式。 有关 Outlook mobile 中支持的 API 的详细信息,请参阅移动设备上的 Outlook 中支持的 Outlook JavaScript API

建议:调用 getTypeAsync,然后将返回的值传递给 options.coercionType 参数。

错误

  • DataExceedsMaximumSize:数据参数长度超过 1,000,000 个字符。

  • InvalidFormatError:参数 options.coercionType 设置为 Office.CoercionType.Html ,消息正文为纯文本。

示例

// When including links in HTML markup, you can disable online link preview
// by setting the id attribute on the anchor (<a>) to "LPNoLP".
Office.context.mailbox.item.body.setAsync(
    '<a id="LPNoLP" href="http://www.contoso.com">Click here!</a>',
    {coercionType: Office.CoercionType.Html},
    callback);
Office.context.mailbox.item.body.setAsync(
    "<b>(replaces all body, including threads you are replying to that may be on the bottom)</b>",
    { coercionType: "html", asyncContext: "This is passed to the callback" },
    function callback(result) {
        // Process the result.
});

// The following is an example of the result parameter passed to the callback function.
{
    "value":null,
    "status": "succeeded",
    "asyncContext": "This is passed to the callback"
}
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/outlook/20-item-body/add-inline-base64-image.yaml

const mailItem = Office.context.mailbox.item;
const base64String =
  "iVBORw0KGgoAAAANSUhEUgAAAGAAAABgCAMAAADVRocKAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAnUExURQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAN0S+bUAAAAMdFJOUwAQIDBAUI+fr7/P7yEupu8AAAAJcEhZcwAADsMAAA7DAcdvqGQAAAF8SURBVGhD7dfLdoMwDEVR6Cspzf9/b20QYOthS5Zn0Z2kVdY6O2WULrFYLBaLxd5ur4mDZD14b8ogWS/dtxV+dmx9ysA2QUj9TQRWv5D7HyKwuIW9n0vc8tkpHP0W4BOg3wQ8wtlvA+PC1e8Ao8Ld7wFjQtHvAiNC2e8DdqHqKwCrUPc1gE1AfRVgEXBfB+gF0lcCWoH2tYBOYPpqQCNwfT3QF9i+AegJfN8CtAWhbwJagtS3AbIg9o2AJMh9M5C+SVGBvx6zAfmT0r+Bv8JMwP4kyFPir+cswF5KL3WLv14zAFBCLf56Tw9cparFX4upgaJUtPhrOS1QlY5W+vWTXrGgBFB/b72ev3/0igUdQPppP/nfowfKUUEFcP207y/yxKmgAYQ+PywoAFOfCH3A2MdCFzD3kdADBvq10AGG+pXQBgb7pdAEhvuF0AIc/VtoAK7+JciAs38KIuDugyAC/v4hiMCE/i7IwLRBsh68N2WQjMVisVgs9i5bln8LGScNcCrONQAAAABJRU5ErkJggg==";

// Get the current body of the message or appointment.
mailItem.body.getAsync(Office.CoercionType.Html, (bodyResult) => {
  if (bodyResult.status === Office.AsyncResultStatus.Succeeded) {
    // Insert the Base64 image to the beginning of the body.
    const options = { isInline: true, asyncContext: bodyResult.value };
    mailItem.addFileAttachmentFromBase64Async(base64String, "sample.png", options, (attachResult) => {
      if (attachResult.status === Office.AsyncResultStatus.Succeeded) {
        let body = attachResult.asyncContext;
        body = body.replace("<p class=MsoNormal>", `<p class=MsoNormal><img src="cid:sample.png">`);

        mailItem.body.setAsync(body, { coercionType: Office.CoercionType.Html }, (setResult) => {
          if (setResult.status === Office.AsyncResultStatus.Succeeded) {
            console.log("Inline Base64 image added to the body.");
          } else {
            console.log(setResult.error.message);
          }
        });
      } else {
        console.log(attachResult.error.message);
      }
    });
  } else {
    console.log(bodyResult.error.message);
  }
});

setAsync(data, callback)

将整个正文替换为指定的文本。

setAsync(data: string, callback?: (asyncResult: Office.AsyncResult<void>) => void): void;

参数

data

string

将替换现有正文的字符串。 字符串限制为 1,000,000 个字符。

callback

(asyncResult: Office.AsyncResult<void>) => void

可选。 方法完成后,使用 Office.AsyncResult 类型的单个参数调用在 参数中 callback 传递的函数。 asyncResult.error 属性中将提供遇到的所有错误。

返回

void

注解

[ API 集:邮箱 1.3 ]

最低权限级别读/写项

适用的 Outlook 模式:撰写

重要说明

  • 将正文替换为指定内容后,光标的位置取决于加载项运行的客户端。 在 Outlook on Windows 中,光标显示在邮件项目正文的开头,而在Outlook 网页版中,光标显示在邮件项目正文的末尾。 在 Outlook on Mac 中,不会保留光标位置。 光标在调用后消失, prependAsync 仅在用户选择邮件项目正文中的某些内容时重新出现。

  • 使用 HTML 格式的正文时,请务必注意,方法返回 Body.getAsync 的值不一定与之前在 方法中 Body.setAsync 传递的值完全相同。 客户端可以修改传递给 setAsync 的值,使其通过其呈现引擎高效呈现。

  • 在 HTML 标记中包含链接时,可以通过将定位点上的 属性 (<>) 设置为id“LPNoLP”来禁用联机链接预览, (有关示例) ,请参阅示例部分。

  • 在 Outlook on Windows 和 Mac 中,加载项用户无法使用“撤消”命令还原此操作。

  • 在 Android 版 Outlook 和 iOS 版中,邮件撰写模式不支持此方法。 仅支持约会组织者模式。 有关 Outlook mobile 中支持的 API 的详细信息,请参阅移动设备上的 Outlook 中支持的 Outlook JavaScript API

建议:调用 getTypeAsync,然后将返回的值传递给 options.coercionType 参数。

错误

  • DataExceedsMaximumSize:数据参数长度超过 1,000,000 个字符。

  • InvalidFormatError:参数 options.coercionType 设置为 Office.CoercionType.Html ,消息正文为纯文本。

setSelectedDataAsync(data, options, callback)

将正文中的所选内容更换为指定文本。

setSelectedDataAsync 方法将指定的字符串插入项目正文中的光标位置,或者,如果在编辑器中选定了文本,它就会替换所选文本。 如果光标从未出现在项目正文中,或者如果该项目的正文不关注 UI,该字符串将插入到正文内容的顶部。 插入后,光标将置于插入内容的末尾。

在 HTML 标记中包含链接时,可以通过将定位点上的 属性 (<>) 设置为id“LPNoLP”来禁用联机链接预览, (有关示例) ,请参阅示例部分。

建议:调用 getTypeAsync ,然后将返回的值传递给 options.coercionType 参数。

setSelectedDataAsync(data: string, options: Office.AsyncContextOptions & CoercionTypeOptions, callback?: (asyncResult: Office.AsyncResult<void>) => void): void;

参数

data

string

将替换现有正文的字符串。 字符串限制为 1,000,000 个字符。

options

Office.AsyncContextOptions & Office.CoercionTypeOptions

包含以下一个或多个属性的对象文本:- asyncContext:开发人员可以在回调函数中提供他们想要访问的任何对象。 coercionType:正文的所需格式。 参数中的字符串将转换为此格式。

callback

(asyncResult: Office.AsyncResult<void>) => void

可选。 方法完成后,使用类型的Office.AsyncResult单个参数调用在 参数中callback传递的函数。 asyncResult.error 属性中将提供遇到的所有错误。

返回

void

注解

[ API set: Mailbox 1.1 ]

最低权限级别读/写项

适用的 Outlook 模式:撰写

错误

  • DataExceedsMaximumSize:参数 data 长度超过 1,000,000 个字符。

  • InvalidFormatError:参数 options.coercionType 设置为 Office.CoercionType.Html ,消息正文为纯文本。

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/outlook/20-item-body/replace-selected-text.yaml

/* This snippet replaces selected text in a message or appointment's body with specified text.
  
  If you want to use a link in HTML markup as a value of the setSelectedDataAsync call's data parameter, you can disable online link preview by setting the anchor tag's id attribute to "LPNoLP". For example, '<a id="LPNoLP" href="http://www.contoso.com">Click here!</a>'.
*/
const text = $("#text-field").val();

// It's recommended to call getTypeAsync and pass its returned value to the options.coercionType parameter of the prependAsync call.
Office.context.mailbox.item.body.getTypeAsync((asyncResult) => {
  if (asyncResult.status === Office.AsyncResultStatus.Failed) {
    console.log("Action failed with error: " + asyncResult.error.message);
    return;
  }

  const bodyFormat = asyncResult.value;
  Office.context.mailbox.item.body.setSelectedDataAsync(text, { coercionType: bodyFormat }, (asyncResult) => {
    if (asyncResult.status === Office.AsyncResultStatus.Failed) {
      console.log("Action failed with error: " + asyncResult.error.message);
      return;
    }

    console.log(`Replaced selected text with "${text}".`);
  });
});

setSelectedDataAsync(data, callback)

将正文中的所选内容更换为指定文本。

setSelectedDataAsync 方法将指定的字符串插入项目正文中的光标位置,或者,如果在编辑器中选定了文本,它就会替换所选文本。 如果光标从未出现在项目正文中,或者如果该项目的正文不关注 UI,该字符串将插入到正文内容的顶部。 插入后,光标将置于插入内容的末尾。

在 HTML 标记中包含链接时,可以通过将定位点上的 属性 (<>) 设置为id“LPNoLP”来禁用联机链接预览, (有关示例) ,请参阅示例部分。

建议:调用 getTypeAsync ,然后将返回的值传递给 options.coercionType 参数。

setSelectedDataAsync(data: string, callback?: (asyncResult: Office.AsyncResult<void>) => void): void;

参数

data

string

将替换现有正文的字符串。 字符串限制为 1,000,000 个字符。

callback

(asyncResult: Office.AsyncResult<void>) => void

可选。 方法完成后,使用类型的Office.AsyncResult单个参数调用在 参数中callback传递的函数。 asyncResult.error 属性中将提供遇到的所有错误。

返回

void

注解

[ API set: Mailbox 1.1 ]

最低权限级别读/写项

适用的 Outlook 模式:撰写

错误

  • DataExceedsMaximumSize:参数 data 长度超过 1,000,000 个字符。

  • InvalidFormatError:参数 options.coercionType 设置为 Office.CoercionType.Html ,消息正文为纯文本。