PowerPoint.TextRange class
包含附加到形状上的文本,以及用于操作文本的属性和方法。
注解
使用方
- PowerPoint.Hyperlink: getLinkedTextRangeOrNullObject
- PowerPoint.HyperlinkCollection: 添加
- PowerPoint.Presentation: getSelectedTextRange, getSelectedTextRangeOrNullObject
- PowerPoint.TextFrame: textRange
示例
// 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
返回存在于此 TextRange.
readonly hyperlinks: PowerPoint.HyperlinkScopedCollection;
属性值
注解
length
获取或设置此表示 TextRange 的范围的长度。 当使用负值设置或值大于起点的可用文本长度时引发 InvalidArgument 异常。
length: number;
属性值
number
注解
paragraphFormat
表示文本区域的段落格式。 有关详细信息,请参阅 PowerPoint.ParagraphFormat 。
readonly paragraphFormat: PowerPoint.ParagraphFormat;
属性值
注解
示例
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/powerpoint/text/get-set-textrange.yaml
// Gets navigational (complex) properties of the selected text range.
await PowerPoint.run(async (context) => {
const textRange: PowerPoint.TextRange = context.presentation.getSelectedTextRange();
textRange.load("font,paragraphFormat/bulletFormat,paragraphFormat/horizontalAlignment");
const parentTextFrame: PowerPoint.TextFrame = textRange.getParentTextFrame();
const parentShape: PowerPoint.Shape = parentTextFrame.getParentShape();
const parentSlide: PowerPoint.Slide = parentShape.getParentSlide();
parentShape.load("id");
parentSlide.load("id");
await context.sync();
console.log(`Selected text range found in parent shape with ID ${parentShape.id} on parentSlide with ID ${parentSlide.id}`);
console.log("Font properties of selected text range:");
console.log(`\tallCaps: ${textRange.font.allCaps}`);
console.log(`\tbold: ${textRange.font.bold}`);
console.log(`\tcolor: ${textRange.font.color}`);
console.log(`\tdoubleStrikethrough: ${textRange.font.doubleStrikethrough}`);
console.log(`\titalic: ${textRange.font.italic}`);
console.log(`\tname: ${textRange.font.name}`);
console.log(`\tsize: ${textRange.font.size}`);
console.log(`\tsmallCaps: ${textRange.font.smallCaps}`);
console.log(`\tstrikethrough: ${textRange.font.strikethrough}`);
console.log(`\tsubscript: ${textRange.font.subscript}`);
console.log(`\tsuperscript: ${textRange.font.superscript}`);
console.log(`\tunderline: ${textRange.font.underline}`);
console.log("Paragraph format properties of selected text range:");
console.log(`\tbulletFormat.visible: ${textRange.paragraphFormat.bulletFormat.visible}`);
console.log(`\thorizontalAlignment: ${textRange.paragraphFormat.horizontalAlignment}`);
});
start
对于它 TextRange 所表示的范围的起始位置,获取或设置相对于父文本框架从零开始的索引。 当使用负值设置或值大于文本长度时引发 InvalidArgument 异常。
start: number;
属性值
number
注解
text
方法详细信息
getParentTextFrame()
返回保存此对象TextRange的父 PowerPoint.TextFrame 对象。
getParentTextFrame(): PowerPoint.TextFrame;
返回
注解
示例
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/powerpoint/text/get-set-textrange.yaml
// Gets navigational (complex) properties of the selected text range.
await PowerPoint.run(async (context) => {
const textRange: PowerPoint.TextRange = context.presentation.getSelectedTextRange();
textRange.load("font,paragraphFormat/bulletFormat,paragraphFormat/horizontalAlignment");
const parentTextFrame: PowerPoint.TextFrame = textRange.getParentTextFrame();
const parentShape: PowerPoint.Shape = parentTextFrame.getParentShape();
const parentSlide: PowerPoint.Slide = parentShape.getParentSlide();
parentShape.load("id");
parentSlide.load("id");
await context.sync();
console.log(`Selected text range found in parent shape with ID ${parentShape.id} on parentSlide with ID ${parentSlide.id}`);
console.log("Font properties of selected text range:");
console.log(`\tallCaps: ${textRange.font.allCaps}`);
console.log(`\tbold: ${textRange.font.bold}`);
console.log(`\tcolor: ${textRange.font.color}`);
console.log(`\tdoubleStrikethrough: ${textRange.font.doubleStrikethrough}`);
console.log(`\titalic: ${textRange.font.italic}`);
console.log(`\tname: ${textRange.font.name}`);
console.log(`\tsize: ${textRange.font.size}`);
console.log(`\tsmallCaps: ${textRange.font.smallCaps}`);
console.log(`\tstrikethrough: ${textRange.font.strikethrough}`);
console.log(`\tsubscript: ${textRange.font.subscript}`);
console.log(`\tsuperscript: ${textRange.font.superscript}`);
console.log(`\tunderline: ${textRange.font.underline}`);
console.log("Paragraph format properties of selected text range:");
console.log(`\tbulletFormat.visible: ${textRange.paragraphFormat.bulletFormat.visible}`);
console.log(`\thorizontalAlignment: ${textRange.paragraphFormat.horizontalAlignment}`);
});
getSubstring(start, length)
返回 TextRange 给定范围内子字符串的对象。
getSubstring(start: number, length?: number): PowerPoint.TextRange;
参数
- start
-
number
要从文本区域中获取的第一个字符的从零开始的索引。
- length
-
number
可选。 要在新文本区域中返回的字符数。 如果省略长度,将返回从文本范围最后一段的开头到结尾的所有字符。
返回
注解
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)
使用指定的选项在此上 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;