Excel.PageBreakCollection class
注解
使用方
示例
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/54-worksheet/manage-page-breaks.yaml
// Load and inspect the page breaks in both worksheet collections.
await Excel.run(async (context) => {
const sheet: Excel.Worksheet = context.workbook.worksheets.getItem("Sample");
const horizontalBreaks: Excel.PageBreakCollection = sheet.horizontalPageBreaks;
const verticalBreaks: Excel.PageBreakCollection = sheet.verticalPageBreaks;
horizontalBreaks.load("items/rowIndex, items/columnIndex");
verticalBreaks.load("items/rowIndex, items/columnIndex");
await context.sync();
console.log(`Horizontal page breaks: ${horizontalBreaks.items.length}`);
horizontalBreaks.items.forEach((pageBreak: Excel.PageBreak) => {
console.log("Horizontal break (zero-based indexes):", {
rowIndex: pageBreak.rowIndex,
columnIndex: pageBreak.columnIndex
});
});
console.log(`Vertical page breaks: ${verticalBreaks.items.length}`);
verticalBreaks.items.forEach((pageBreak: Excel.PageBreak) => {
console.log("Vertical break (zero-based indexes):", {
rowIndex: pageBreak.rowIndex,
columnIndex: pageBreak.columnIndex
});
});
});
方法
| add(page |
在指定区域的左上角单元格之前添加分页符。 |
| get |
获取集合中的分页符数量。 |
| get |
通过索引获取分页符对象。 |
| load(options) | 将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 |
| load(property |
将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 |
| load(property |
将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 |
| remove |
重置集合中的所有手动分页符。 |
| toJSON() | 覆盖 JavaScript |
属性详细信息
context
items
方法详细信息
add(pageBreakRange)
在指定区域的左上角单元格之前添加分页符。
add(pageBreakRange: Range | string): Excel.PageBreak;
参数
- pageBreakRange
-
Excel.Range | string
紧接要添加的分页符之后的区域。
返回
注解
示例
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/54-worksheet/worksheet-page-layout.yaml
// Add a horizontal page break before the next group of farm sales.
await Excel.run(async (context) => {
const farmSheet: Excel.Worksheet = context.workbook.worksheets.getItem("Print");
const horizontalBreaks: Excel.PageBreakCollection = farmSheet.horizontalPageBreaks;
horizontalBreaks.add("A21:E21");
await context.sync();
console.log("Added a horizontal page break before row 21.");
});
getCount()
获取集合中的分页符数量。
getCount(): OfficeExtension.ClientResult<number>;
返回
OfficeExtension.ClientResult<number>
注解
示例
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/54-worksheet/manage-page-breaks.yaml
// Count page breaks without loading the collection's items.
await Excel.run(async (context) => {
const sheet: Excel.Worksheet = context.workbook.worksheets.getItem("Sample");
const horizontalBreaks: Excel.PageBreakCollection = sheet.horizontalPageBreaks;
const verticalBreaks: Excel.PageBreakCollection = sheet.verticalPageBreaks;
const horizontalCount: OfficeExtension.ClientResult<number> = horizontalBreaks.getCount();
const verticalCount: OfficeExtension.ClientResult<number> = verticalBreaks.getCount();
await context.sync();
console.log(`Horizontal page breaks: ${horizontalCount.value}`);
console.log(`Vertical page breaks: ${verticalCount.value}`);
});
getItem(index)
通过索引获取分页符对象。
getItem(index: number): Excel.PageBreak;
参数
- index
-
number
分页符的索引。
返回
注解
示例
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/54-worksheet/manage-page-breaks.yaml
// Inspect the first horizontal break and locate the cell immediately after it.
await Excel.run(async (context) => {
const sheet: Excel.Worksheet = context.workbook.worksheets.getItem("Sample");
const horizontalBreaks: Excel.PageBreakCollection = sheet.horizontalPageBreaks;
const horizontalCount: OfficeExtension.ClientResult<number> = horizontalBreaks.getCount();
await context.sync();
if (horizontalCount.value === 0) {
console.warn("No horizontal page breaks. Choose Add page breaks first.");
return;
}
const firstBreak: Excel.PageBreak = horizontalBreaks.getItem(0);
const cellAfterBreak: Excel.Range = firstBreak.getCellAfterBreak();
firstBreak.load("rowIndex, columnIndex");
cellAfterBreak.load("address");
await context.sync();
console.log("First horizontal page break (zero-based indexes):", {
rowIndex: firstBreak.rowIndex,
columnIndex: firstBreak.columnIndex,
cellAfterBreak: cellAfterBreak.address
});
});
load(options)
将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()。
load(options?: Excel.Interfaces.PageBreakCollectionLoadOptions & Excel.Interfaces.CollectionLoadOptions): Excel.PageBreakCollection;
参数
为要加载的对象属性提供选项。
返回
load(propertyNames)
将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()。
load(propertyNames?: string | string[]): Excel.PageBreakCollection;
参数
- propertyNames
-
string | string[]
指定要加载的属性的逗号分隔的字符串或字符串数组。
返回
load(propertyNamesAndPaths)
将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()。
load(propertyNamesAndPaths?: OfficeExtension.LoadOption): Excel.PageBreakCollection;
参数
- propertyNamesAndPaths
- OfficeExtension.LoadOption
propertyNamesAndPaths.select 是指定要加载的属性的逗号分隔字符串,以及 propertyNamesAndPaths.expand 指定要加载的导航属性的逗号分隔字符串。
返回
removePageBreaks()
重置集合中的所有手动分页符。
removePageBreaks(): void;
返回
void
注解
示例
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/54-worksheet/manage-page-breaks.yaml
// Reset all manual page breaks in both worksheet collections.
await Excel.run(async (context) => {
const sheet: Excel.Worksheet = context.workbook.worksheets.getItem("Sample");
const horizontalBreaks: Excel.PageBreakCollection = sheet.horizontalPageBreaks;
const verticalBreaks: Excel.PageBreakCollection = sheet.verticalPageBreaks;
horizontalBreaks.removePageBreaks();
verticalBreaks.removePageBreaks();
await context.sync();
console.log("Removed all manual page breaks. Choose Count page breaks to verify the collections.");
});
toJSON()
覆盖 JavaScript toJSON() 方法,以便在将 API 对象传递给 JSON.stringify()时提供更有用的输出。 (JSON.stringify反过来调用 toJSON 传递给它的对象的方法。) 虽然原始 Excel.PageBreakCollection 对象是 API 对象,但该 toJSON 方法返回一个纯 JavaScript 对象, (类型为 Excel.Interfaces.PageBreakCollectionData) ,其中包含一个“items”数组,其中包含从集合的 items 中加载的任何属性的浅层副本。
toJSON(): Excel.Interfaces.PageBreakCollectionData;