PowerPoint.BindingCollection class
表示属于演示文稿的所有绑定对象的集合。
注解
使用方
示例
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/powerpoint/shapes/binding-to-shapes.yaml
// Loads bindings.
await PowerPoint.run(async (context) => {
const bindings = context.presentation.bindings;
bindings.load("items");
await context.sync();
const bindingCount = bindings.items.length;
if (bindingCount === 0) {
console.log(`There are no bindings.`);
} else if (bindingCount === 1) {
console.log("There's 1 binding.");
} else {
console.log(`There are ${bindingCount} bindings.`);
}
bindings.items.forEach((binding) => {
getShapeForBindingId(binding.id).then((shape) => {
if (shape) {
console.log(`Binding ID: ${binding.id} refers to shape ID ${shape.id}`);
} else {
console.log(`Binding ID: ${binding.id} doesn't refers to shape.`);
}
});
});
populateBindingsDropdown(bindings.items);
});
方法
| add(shape, binding |
向特定形状添加新绑定。 如果提供的 ID 已被绑定使用,则将覆盖现有绑定。 |
| add(shape, binding |
向特定形状添加新绑定。 如果提供的 ID 已被绑定使用,则将覆盖现有绑定。 |
| add |
基于当前选择添加新绑定。 如果所选区域有多个区域, |
| add |
基于当前选择添加新绑定。 如果所选区域有多个区域, |
| get |
获取集合中的绑定数量。 |
| get |
按 ID 获取绑定对象。 如果没有与该 ID 绑定,则引发 ItemNotFoundException。 |
| get |
根据其在项目数组中的位置获取绑定对象。 如果索引小于 0,或大于或等于集合中的项目计数,则引发 InvalidArgumentException。 |
| get |
按 ID 获取绑定对象。 如果绑定对象不存在,则此方法将返回其 |
| load(options) | 将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 |
| load(property |
将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 |
| load(property |
将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 |
| toJSON() | 覆盖 JavaScript |
属性详细信息
context
items
方法详细信息
add(shape, bindingType, id)
向特定形状添加新绑定。 如果提供的 ID 已被绑定使用,则将覆盖现有绑定。
add(shape: PowerPoint.Shape, bindingType: PowerPoint.BindingType, id: string): PowerPoint.Binding;
参数
- shape
- PowerPoint.Shape
要添加绑定的形状。
- bindingType
- PowerPoint.BindingType
绑定的类型。 请参阅 BindingType。
- id
-
string
绑定的 ID。
返回
注解
示例
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/powerpoint/shapes/binding-to-shapes.yaml
// Inserts an image with binding.
await PowerPoint.run(async (context) => {
const bindingId = (document.getElementById("temp-binding-id") as HTMLInputElement).value;
const slide = context.presentation.getSelectedSlides().getItemAt(0);
const myShape = slide.shapes.addGeometricShape(PowerPoint.GeometricShapeType.rectangle, {
top: 100,
left: 30,
width: 200,
height: 200
});
myShape.fill.setImage(flowerImage);
context.presentation.bindings.add(myShape, PowerPoint.BindingType.shape, bindingId);
await context.sync();
const bindingsDropdown = document.getElementById("bindings-dropdown") as HTMLSelectElement;
const option = new Option(`Binding ${bindingId}`, bindingId);
// When a binding ID already exists, the binding is updated to refer to the new shape
// so select the existing item rather than add a new one.
const foundIndex = findDropdownItem(bindingsDropdown, option.text);
if (foundIndex < 0) {
bindingsDropdown.add(option);
bindingsDropdown.selectedIndex = bindingsDropdown.options.length - 1;
} else {
bindingsDropdown.selectedIndex = foundIndex;
}
});
add(shape, bindingType, id)
向特定形状添加新绑定。 如果提供的 ID 已被绑定使用,则将覆盖现有绑定。
add(shape: PowerPoint.Shape, bindingType: "Shape", id: string): PowerPoint.Binding;
参数
- shape
- PowerPoint.Shape
要添加绑定的形状。
- bindingType
-
"Shape"
绑定的类型。 请参阅 BindingType。
- id
-
string
绑定的 ID。
返回
注解
addFromSelection(bindingType, id)
基于当前选择添加新绑定。 如果所选区域有多个区域, InvalidReference 则将返回错误。
addFromSelection(bindingType: PowerPoint.BindingType, id: string): PowerPoint.Binding;
参数
- bindingType
- PowerPoint.BindingType
绑定的类型。 请参阅 BindingType。
- id
-
string
绑定的 ID。
返回
注解
addFromSelection(bindingType, id)
基于当前选择添加新绑定。 如果所选区域有多个区域, InvalidReference 则将返回错误。
addFromSelection(bindingType: "Shape", id: string): PowerPoint.Binding;
参数
- bindingType
-
"Shape"
绑定的类型。 请参阅 BindingType。
- id
-
string
绑定的 ID。
返回
注解
getCount()
获取集合中的绑定数量。
getCount(): OfficeExtension.ClientResult<number>;
返回
OfficeExtension.ClientResult<number>
注解
getItem(key)
按 ID 获取绑定对象。 如果没有与该 ID 绑定,则引发 ItemNotFoundException。
getItem(key: string): PowerPoint.Binding;
参数
- key
-
string
要检索的绑定对象的 ID。
返回
注解
示例
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/powerpoint/shapes/binding-to-shapes.yaml
async function getShapeForBindingId(bindingId: string): Promise<PowerPoint.Shape | undefined> {
// Gets shape associated with binding ID.
return PowerPoint.run(async (context) => {
const binding = context.presentation.bindings.getItem(bindingId);
const shape = binding.getShape();
return shape;
});
}
getItemAt(index)
根据其在项目数组中的位置获取绑定对象。 如果索引小于 0,或大于或等于集合中的项目计数,则引发 InvalidArgumentException。
getItemAt(index: number): PowerPoint.Binding;
参数
- index
-
number
要检索的对象的索引值。 从零开始编制索引。
返回
注解
getItemOrNullObject(id)
按 ID 获取绑定对象。 如果绑定对象不存在,则此方法将返回其 isNullObject 属性设置为 true的对象。 有关详细信息,请参阅 *OrNullObject 方法和属性。
getItemOrNullObject(id: string): PowerPoint.Binding;
参数
- id
-
string
要检索的绑定对象的 ID。
返回
注解
load(options)
将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()。
load(options?: PowerPoint.Interfaces.BindingCollectionLoadOptions & PowerPoint.Interfaces.CollectionLoadOptions): PowerPoint.BindingCollection;
参数
- options
-
PowerPoint.Interfaces.BindingCollectionLoadOptions & PowerPoint.Interfaces.CollectionLoadOptions
为要加载的对象属性提供选项。
返回
load(propertyNames)
将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()。
load(propertyNames?: string | string[]): PowerPoint.BindingCollection;
参数
- propertyNames
-
string | string[]
指定要加载的属性的逗号分隔的字符串或字符串数组。
返回
load(propertyNamesAndPaths)
将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()。
load(propertyNamesAndPaths?: OfficeExtension.LoadOption): PowerPoint.BindingCollection;
参数
- propertyNamesAndPaths
- OfficeExtension.LoadOption
propertyNamesAndPaths.select 是指定要加载的属性的逗号分隔字符串,以及 propertyNamesAndPaths.expand 指定要加载的导航属性的逗号分隔字符串。
返回
toJSON()
覆盖 JavaScript toJSON() 方法,以便在将 API 对象传递给 JSON.stringify()时提供更有用的输出。 (JSON.stringify反过来调用 toJSON 传递给它的对象的方法。) 虽然原始 PowerPoint.BindingCollection 对象是 API 对象,但该 toJSON 方法返回一个纯 JavaScript 对象, (类型为 PowerPoint.Interfaces.BindingCollectionData) ,其中包含一个“items”数组,其中包含从集合的 items 中加载的任何属性的浅层副本。
toJSON(): PowerPoint.Interfaces.BindingCollectionData;