PowerPoint.ShapeScopedCollection class
表示形状的集合。
注解
使用方
- PowerPoint.Graphic: convertToShape
- PowerPoint.Presentation: getSelectedShapes
- PowerPoint.ShapeGroup: 形状
示例
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/powerpoint/shapes/get-set-shapes.yaml
// Changes the selected shapes fill color to red.
await PowerPoint.run(async (context) => {
const shapes: PowerPoint.ShapeScopedCollection = context.presentation.getSelectedShapes();
const shapeCount = shapes.getCount();
shapes.load("items/fill/type");
await context.sync();
shapes.items.map((shape) => {
const shapeFillType = shape.fill.type as PowerPoint.ShapeFillType;
console.log(`Shape ID ${shape.id} original fill type: ${shapeFillType}`);
shape.fill.setSolidColor("red");
});
await context.sync();
});
方法
| get |
获取集合中的形状数。 |
| get |
使用形状的唯一 ID 获取形状。 如果形状不存在,则会引发错误。 |
| get |
使用集合中从零开始的索引获取形状。 如果索引超出范围,则会引发错误。 |
| get |
使用形状的唯一 ID 获取形状。 如果此类形状不存在,则返回属性 |
| group() | 将此集合中的所有形状分组为单个形状。 如果集合包含的形状少于两个,则此方法返回 |
| load(options) | 将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 |
| load(property |
将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 |
| load(property |
将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 |
| toJSON() | 覆盖 JavaScript |
属性详细信息
context
items
方法详细信息
getCount()
获取集合中的形状数。
getCount(): OfficeExtension.ClientResult<number>;
返回
OfficeExtension.ClientResult<number>
集合中的形状数。
注解
示例
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/powerpoint/shapes/get-set-shapes.yaml
// Gets the shapes you selected on the slide and displays their IDs on the task pane.
await PowerPoint.run(async (context) => {
let finalTable = "";
const shapes: PowerPoint.ShapeScopedCollection = context.presentation.getSelectedShapes();
const shapeCount = shapes.getCount();
await context.sync();
finalTable += "<br>getSelectedShapes.getCount returned:<b>" + shapeCount.value + "</b><br>";
finalTable +=
"<br><table border=1 cellpadding=3 cellspacing=0><tr><td bgcolor=#3333EE><font color=white>Index</font></td><td bgcolor=#3333EE><font color=white>Id</font></td></tr>";
shapes.load("items");
await context.sync();
shapes.items.map((shape, index) => {
finalTable += "<tr><td>" + index + "</td><td>" + shape.id + "</td></tr>";
});
finalTable += "</table>";
const outputSpan = document.getElementById("outputSpan");
outputSpan.innerHTML = "";
outputSpan.innerHTML += finalTable;
});
getItem(key)
使用形状的唯一 ID 获取形状。 如果形状不存在,则会引发错误。
getItem(key: string): PowerPoint.Shape;
参数
- key
-
string
形状的 ID。
返回
具有唯一 ID 的形状。 如果此类形状不存在,则会引发错误。
注解
getItemAt(index)
使用集合中从零开始的索引获取形状。 如果索引超出范围,则会引发错误。
getItemAt(index: number): PowerPoint.Shape;
参数
- index
-
number
集合中形状的索引。
返回
给定索引处的形状。 如果索引超出范围,则会引发错误。
注解
getItemOrNullObject(id)
使用形状的唯一 ID 获取形状。 如果此类形状不存在,则返回属性 isNullObject 设置为 true 的对象。 有关详细信息,请参阅 *OrNullObject 方法和属性。
getItemOrNullObject(id: string): PowerPoint.Shape;
参数
- id
-
string
形状的 ID。
返回
具有唯一 ID 的形状。 如果此类形状不存在,则返回属性 isNullObject 设置为 true 的对象。
注解
示例
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/powerpoint/shapes/get-shape-by-id.yaml
// Uses getItemOrNullObject to find a shape by ID within the selected shapes.
// If the ID is not found in the selected shapes, the returned object's isNullObject property is true.
await PowerPoint.run(async (context) => {
const shapeId = (document.getElementById("shape-id-input") as HTMLInputElement).value.trim();
if (!shapeId) {
console.log("Enter a shape ID in the text box first.");
return;
}
const selectedShapes: PowerPoint.ShapeScopedCollection = context.presentation.getSelectedShapes();
// getItemOrNullObject returns a Shape object without throwing if the ID isn't found.
const shape: PowerPoint.Shape = selectedShapes.getItemOrNullObject(shapeId);
shape.load("id,name,type");
await context.sync();
if (shape.isNullObject) {
console.log(`Shape ID "${shapeId}" was not found in the selected shapes.`);
} else {
console.log(`Shape found in the selected shapes.`);
console.log(` ID: ${shape.id}`);
console.log(` Name: "${shape.name}"`);
console.log(` Type: ${shape.type}`);
}
});
group()
将此集合中的所有形状分组为单个形状。 如果集合包含的形状少于两个,则此方法返回 GeneralException 错误。
group(): PowerPoint.Shape;
返回
作为 PowerPoint.Shape 的新创建的分组形状。
注解
load(options)
将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()。
load(options?: PowerPoint.Interfaces.ShapeScopedCollectionLoadOptions & PowerPoint.Interfaces.CollectionLoadOptions): PowerPoint.ShapeScopedCollection;
参数
- options
-
PowerPoint.Interfaces.ShapeScopedCollectionLoadOptions & PowerPoint.Interfaces.CollectionLoadOptions
为要加载的对象属性提供选项。
返回
load(propertyNames)
将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()。
load(propertyNames?: string | string[]): PowerPoint.ShapeScopedCollection;
参数
- propertyNames
-
string | string[]
指定要加载的属性的逗号分隔的字符串或字符串数组。
返回
load(propertyNamesAndPaths)
将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()。
load(propertyNamesAndPaths?: OfficeExtension.LoadOption): PowerPoint.ShapeScopedCollection;
参数
- propertyNamesAndPaths
- OfficeExtension.LoadOption
propertyNamesAndPaths.select 是指定要加载的属性的逗号分隔字符串,以及 propertyNamesAndPaths.expand 指定要加载的导航属性的逗号分隔字符串。
返回
toJSON()
覆盖 JavaScript toJSON() 方法,以便在将 API 对象传递给 JSON.stringify()时提供更有用的输出。 (JSON.stringify反过来调用 toJSON 传递给它的对象的方法。) 虽然原始 PowerPoint.ShapeScopedCollection 对象是 API 对象,但该 toJSON 方法返回一个纯 JavaScript 对象, (类型为 PowerPoint.Interfaces.ShapeScopedCollectionData) ,其中包含一个“items”数组,其中包含从集合的 items 中加载的任何属性的浅层副本。
toJSON(): PowerPoint.Interfaces.ShapeScopedCollectionData;