PowerPoint.TextRange class
包含附加到形状上的文本,以及用于操作文本的属性和方法。
注解
示例
// 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: PowerPoint.TextRange = context.presentation.getSelectedTextRange();
textRange.font.color = "green";
await context.sync();
});
属性
| context | 与 对象关联的请求上下文。 这会将加载项的进程连接到 Office 主机应用程序的进程。 |
| font | 返回一个 |
| hyperlinks | 返回此 |
| length | 获取或设置此 |
| paragraph |
表示文本范围的段落格式。 有关详细信息,请参阅 PowerPoint.ParagraphFormat 。 |
| start | 获取或设置相对于父文本框架的从零开始的索引,用于表示的区域 |
| text | 表示文本范围的纯文本内容。 |
方法
| get |
返回包含此 |
| get |
返回 |
| load(options) | 将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 |
| load(property |
将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 |
| load(property |
将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 |
| set |
为此设置具有指定选项的超链接 |
| set |
在当前视图中选择此项 |
| toJSON() | 重写 JavaScript |
属性详细信息
context
font
返回一个 ShapeFont 对象,该对象代表文本区域的字体属性。
readonly font: PowerPoint.ShapeFont;
属性值
注解
示例
// 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: PowerPoint.TextRange = context.presentation.getSelectedTextRange();
textRange.font.color = "green";
await context.sync();
});
hyperlinks
注意
此 API 以预览状态提供给开发者,可能根据我们收到的反馈更改。 请勿在生产环境中使用此 API。
返回此 TextRange上存在的超链接的集合。
readonly hyperlinks: PowerPoint.HyperlinkScopedCollection;
属性值
注解
length
获取或设置此 TextRange 表示的范围的长度。
InvalidArgument使用负值设置时,或者如果值大于起始点的可用文本的长度,则引发异常。
length: number;
属性值
number
注解
paragraphFormat
表示文本范围的段落格式。 有关详细信息,请参阅 PowerPoint.ParagraphFormat 。
readonly paragraphFormat: PowerPoint.ParagraphFormat;
属性值
注解
start
获取或设置相对于父文本框架的从零开始的索引,用于表示的区域 TextRange 的起始位置。
InvalidArgument当设置为负值或值大于文本长度时,将引发异常。
start: number;
属性值
number
注解
text
方法详细信息
getParentTextFrame()
getSubstring(start, length)
返回 TextRange 给定区域中子字符串的 对象。
getSubstring(start: number, length?: number): PowerPoint.TextRange;
参数
- start
-
number
要从文本范围获取的第一个字符的从零开始的索引。
- length
-
number
可选。 在新文本区域中要返回的字符数。 如果省略 length,则将返回从文本范围最后一段的开头到末尾的所有字符。
返回
注解
load(options)
将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()。
load(options?: PowerPoint.Interfaces.TextRangeLoadOptions): PowerPoint.TextRange;
参数
提供要加载对象的属性的选项。
返回
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 一个逗号分隔的字符串,指定要加载的导航属性。
返回
setHyperlink(options)
注意
此 API 以预览状态提供给开发者,可能根据我们收到的反馈更改。 请勿在生产环境中使用此 API。
为此设置具有指定选项的超链接 TextRange 。 这将删除此 TextRange上的所有现有超链接。
setHyperlink(options?: PowerPoint.HyperlinkAddOptions): PowerPoint.Hyperlink;
参数
- options
- PowerPoint.HyperlinkAddOptions
可选。 超链接的选项。
返回
新创建的 PowerPoint.Hyperlink 对象。
注解
setSelected()
在当前视图中选择此项 TextRange 。
setSelected(): void;
返回
void
注解
示例
// 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: PowerPoint.ShapeScopedCollection = 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: PowerPoint.Shape = shapes.getItemAt(0);
const textFrame: PowerPoint.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: PowerPoint.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: PowerPoint.Slide = context.presentation.slides.getItem(savedTextSlideSelection[0]);
const shape1: PowerPoint.Shape = slide1.shapes.getItem(savedTextShapeSelection[0]);
const textRange: PowerPoint.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;