PowerPoint.TextRange class

包含附加到形状上的文本,以及用于操作文本的属性和方法。

Extends

注解

[ API 集:PowerPointApi 1.4 ]

属性

context

与 对象关联的请求上下文。 这会将加载项的进程连接到 Office 主机应用程序的进程。

font

返回一个 ShapeFont 对象,该对象代表文本区域的字体属性。

length

获取或设置此 TextRange 表示的范围的长度。 InvalidArgument使用负值设置时,或者如果值大于起始点的可用文本的长度,则引发异常。

paragraphFormat

表示文本范围的段落格式。 有关详细信息,请参阅 PowerPoint.ParagraphFormat

start

获取或设置相对于父文本框架的从零开始的索引,用于表示的区域 TextRange 的起始位置。 InvalidArgument当设置为负值或值大于文本长度时,将引发异常。

text

表示文本范围的纯文本内容。

方法

getParentTextFrame()

返回包含此 TextRange的父 PowerPoint.TextFrame 对象。

getSubstring(start, length)

返回 TextRange 给定区域中子字符串的 对象。

load(options)

将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()

load(propertyNames)

将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()

load(propertyNamesAndPaths)

将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()

setSelected()

在当前视图中选择此项 TextRange

toJSON()

重写 JavaScript toJSON() 方法,以便在将 API 对象传递给 JSON.stringify()时提供更有用的输出。 JSON.stringify (,反过来又调用toJSON传递给它的 对象的 方法。) 而原始 PowerPoint.TextRange 对象是 API 对象,toJSON该方法返回一个纯 JavaScript 对象 (类型为 PowerPoint.Interfaces.TextRangeData) ,该对象包含从原始对象加载的任何子属性的浅表副本。

属性详细信息

context

与 对象关联的请求上下文。 这会将加载项的进程连接到 Office 主机应用程序的进程。

context: RequestContext;

属性值

font

返回一个 ShapeFont 对象,该对象代表文本区域的字体属性。

readonly font: PowerPoint.ShapeFont;

属性值

注解

[ API 集:PowerPointApi 1.4 ]

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/powerpoint/text/get-set-textrange.yaml

// Sets the color of the selected text range to green.
await PowerPoint.run(async (context) => {
  const textRange = context.presentation.getSelectedTextRange();
  textRange.font.color = "green";
  await context.sync();
});

length

获取或设置此 TextRange 表示的范围的长度。 InvalidArgument使用负值设置时,或者如果值大于起始点的可用文本的长度,则引发异常。

length: number;

属性值

number

注解

[ API 集:PowerPointApi 1.5 ]

paragraphFormat

表示文本范围的段落格式。 有关详细信息,请参阅 PowerPoint.ParagraphFormat

readonly paragraphFormat: PowerPoint.ParagraphFormat;

属性值

注解

[ API 集:PowerPointApi 1.4 ]

start

获取或设置相对于父文本框架的从零开始的索引,用于表示的区域 TextRange 的起始位置。 InvalidArgument当设置为负值或值大于文本长度时,将引发异常。

start: number;

属性值

number

注解

[ API 集:PowerPointApi 1.5 ]

text

表示文本范围的纯文本内容。

text: string;

属性值

string

注解

[ API 集:PowerPointApi 1.4 ]

方法详细信息

getParentTextFrame()

返回包含此 TextRange的父 PowerPoint.TextFrame 对象。

getParentTextFrame(): PowerPoint.TextFrame;

返回

注解

[ API 集:PowerPointApi 1.5 ]

getSubstring(start, length)

返回 TextRange 给定区域中子字符串的 对象。

getSubstring(start: number, length?: number): PowerPoint.TextRange;

参数

start

number

要从文本范围获取的第一个字符的从零开始的索引。

length

number

可选。 在新文本区域中要返回的字符数。 如果省略 length,则将返回从文本范围最后一段的开头到末尾的所有字符。

返回

注解

[ API 集:PowerPointApi 1.4 ]

load(options)

将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()

load(options?: PowerPoint.Interfaces.TextRangeLoadOptions): PowerPoint.TextRange;

参数

options
PowerPoint.Interfaces.TextRangeLoadOptions

提供要加载对象的属性的选项。

返回

load(propertyNames)

将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()

load(propertyNames?: string | string[]): PowerPoint.TextRange;

参数

propertyNames

string | string[]

逗号分隔的字符串或指定要加载的属性的字符串数组。

返回

load(propertyNamesAndPaths)

将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()

load(propertyNamesAndPaths?: {
            select?: string;
            expand?: string;
        }): PowerPoint.TextRange;

参数

propertyNamesAndPaths

{ select?: string; expand?: string; }

propertyNamesAndPaths.select 是一个逗号分隔的字符串,指定要加载的属性,是 propertyNamesAndPaths.expand 一个逗号分隔的字符串,指定要加载的导航属性。

返回

setSelected()

在当前视图中选择此项 TextRange

setSelected(): void;

返回

void

注解

[ API 集:PowerPointApi 1.5 ]

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/powerpoint/text/get-set-textrange.yaml

// Selects the first 10 characters of the selected shape.
await PowerPoint.run(async (context) => {
  const shapes = context.presentation.getSelectedShapes();
  const shapeCount = shapes.getCount();
  await context.sync();
  if (shapeCount.value !== 1) {
    console.warn("You must select only one shape with text in it.");
    return;
  }
  const shape = shapes.getItemAt(0);
  const textFrame = shape.textFrame.load("textRange,hasText");
  await context.sync();
  if (textFrame.hasText != true) {
    console.warn("You must select only one shape with text in it.");
    return;
  }
  const textRange = textFrame.textRange;
  textRange.load("text");
  await context.sync();
  if (textRange.text.length < 10) {
    console.warn("You must select only one shape with at least 10 characters in it.");
    return;
  }
  const textRange10 = textRange.getSubstring(0, 10);
  textRange10.setSelected();
  await context.sync();
});

...

// Sets the range selection to the range that was saved previously.
await PowerPoint.run(async (context) => {
  const slide1 = context.presentation.slides.getItem(savedTextSlideSelection[0]);
  const shape1 = slide1.shapes.getItem(savedTextShapeSelection[0]);
  const textRange = shape1.textFrame.textRange.getSubstring(savedTextTextRangeStart, savedTextTextRangeLength);
  textRange.setSelected();
  await context.sync();
});

toJSON()

重写 JavaScript toJSON() 方法,以便在将 API 对象传递给 JSON.stringify()时提供更有用的输出。 JSON.stringify (,反过来又调用toJSON传递给它的 对象的 方法。) 而原始 PowerPoint.TextRange 对象是 API 对象,toJSON该方法返回一个纯 JavaScript 对象 (类型为 PowerPoint.Interfaces.TextRangeData) ,该对象包含从原始对象加载的任何子属性的浅表副本。

toJSON(): PowerPoint.Interfaces.TextRangeData;

返回