Word.Page class
表示文档中的页面。
Page 对象管理页面布局和内容。
注解
使用方
示例
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/36-pages/get-pages.yaml
// Reads the first page's paper dimensions in points and its 1-based index.
await Word.run(async (context) => {
const pages: Word.PageCollection = context.document.activeWindow.activePane.pages;
const firstPage: Word.Page = pages.getFirst();
firstPage.load("index,height,width");
await context.sync();
console.log("First page properties:", {
index: firstPage.index,
heightInPoints: firstPage.height,
widthInPoints: firstPage.width
});
});
属性
| breaks | 获取 |
| context | 与对象关联的请求上下文。 这将加载项的进程连接到 Office 主机应用程序的进程。 |
| height | 获取“页面设置”对话框中定义的纸张的高度(以磅为单位)。 |
| index | 获取页面的索引。 页索引是基于 1 的,独立于用户的自定义页码。 |
| width | 获取“页面设置”对话框中定义的纸张的宽度(以磅为单位)。 |
方法
| get |
获取窗格中的下一页。 如果此页面是最后一页,则引发 |
| get |
获取下一页。 如果此页面是最后一个页面,则此方法将返回其 |
| get |
获取整个页面或页面的起点或结束点作为范围。 |
| load(options) | 将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 |
| load(property |
将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 |
| load(property |
将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 |
| toJSON() | 覆盖 JavaScript |
| track() | 根据文档中的相应更改来跟踪对象,以便进行自动调整。 此调用是 context.trackedObjects.add (thisObject) 的简写。 如果在调用中 |
| untrack() | 释放与此对象关联的内存(如果先前已跟踪过)。 此调用是 context.trackedObjects.remove (thisObject) 的简写。 拥有许多跟踪对象会降低主机应用程序的速度,因此请在使用完毕后释放所添加的任何对象。 需要在内存释放生效之前调用 |
属性详细信息
breaks
获取 BreakCollection 表示页上分隔符的对象。
readonly breaks: Word.BreakCollection;
属性值
注解
示例
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/36-pages/get-pages.yaml
// Gets the page number of every break in the active document pane.
await Word.run(async (context) => {
const pages: Word.PageCollection = context.document.activeWindow.activePane.pages;
pages.load("items");
await context.sync();
const breakCollections: Word.BreakCollection[] = [];
pages.items.forEach((page: Word.Page) => {
const breaks: Word.BreakCollection = page.breaks;
breaks.load("items/pageIndex");
breakCollections.push(breaks);
});
await context.sync();
const documentBreaks: Word.Break[] = breakCollections.flatMap((breaks) => breaks.items);
console.log(`Breaks found: ${documentBreaks.length}`);
documentBreaks.forEach((documentBreak: Word.Break) => {
console.log(`Break on page ${documentBreak.pageIndex}.`);
});
});
context
height
index
获取页面的索引。 页索引是基于 1 的,独立于用户的自定义页码。
readonly index: number;
属性值
number
注解
示例
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/36-pages/get-pages.yaml
// Reads the page indexes for the third paragraph.
await Word.run(async (context) => {
const paragraphs: Word.ParagraphCollection = context.document.body.paragraphs;
paragraphs.load("items");
await context.sync();
if (paragraphs.items.length < 3) {
console.warn("Run Set up to create at least three paragraphs.");
return;
}
const thirdParagraph: Word.Paragraph = paragraphs.items[2];
const rangeOfParagraph: Word.Range = thirdParagraph.getRange();
const pages: Word.PageCollection = rangeOfParagraph.pages;
pages.load("items/index");
await context.sync();
console.log(`Pages containing the third paragraph: ${pages.items.length}`);
pages.items.forEach((page: Word.Page) => {
console.log(`Page index: ${page.index}`);
});
});
width
方法详细信息
getNext()
获取窗格中的下一页。 如果此页面是最后一页,则引发 ItemNotFound 错误。
getNext(): Word.Page;
返回
注解
示例
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/36-pages/get-pages.yaml
// Reads the page following the first page when the document has multiple pages.
await Word.run(async (context) => {
const pages: Word.PageCollection = context.document.activeWindow.activePane.pages;
pages.load("items");
await context.sync();
if (pages.items.length < 2) {
console.warn("Run Set up to create multiple pages before getting the next page.");
return;
}
const firstPage: Word.Page = pages.getFirst();
const nextPage: Word.Page = firstPage.getNext();
nextPage.load("index");
const nextPageRange: Word.Range = nextPage.getRange();
nextPageRange.load("text");
await context.sync();
console.log(`Next page index: ${nextPage.index}`);
console.log(`Next page text: ${nextPageRange.text}`);
});
getNextOrNullObject()
获取下一页。 如果此页面是最后一个页面,则此方法将返回其 isNullObject 属性设置为 true的对象。 有关详细信息,请参阅 *OrNullObject 方法和属性。
getNextOrNullObject(): Word.Page;
返回
注解
示例
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/36-pages/iterate-pages.yaml
// Visits each page and stops safely when there is no next page.
await Word.run(async (context) => {
const pages: Word.PageCollection = context.document.activeWindow.activePane.pages;
let currentPage: Word.Page = pages.getFirstOrNullObject();
await context.sync();
while (!currentPage.isNullObject) {
currentPage.load("index");
const pageRange: Word.Range = currentPage.getRange();
pageRange.load("text");
await context.sync();
console.log(`Page ${currentPage.index} text: ${pageRange.text}`);
currentPage = currentPage.getNextOrNullObject();
await context.sync();
}
console.log("Reached the end of the pages in the active pane.");
});
getRange(rangeLocation)
获取整个页面或页面的起点或结束点作为范围。
getRange(rangeLocation?: Word.RangeLocation.whole | Word.RangeLocation.start | Word.RangeLocation.end | "Whole" | "Start" | "End"): Word.Range;
参数
返回
注解
示例
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/36-pages/get-pages.yaml
// Reads the index and text of each page intersecting the selection.
await Word.run(async (context) => {
const selectedRange: Word.Range = context.document.getSelection();
const pages: Word.PageCollection = selectedRange.pages;
pages.load("items/index");
await context.sync();
console.log(`Pages in the selection: ${pages.items.length}`);
const pageRanges: Word.Range[] = [];
pages.items.forEach((page: Word.Page) => {
const pageRange: Word.Range = page.getRange(Word.RangeLocation.whole);
pageRange.load("text");
pageRanges.push(pageRange);
});
await context.sync();
for (let i = 0; i < pages.items.length; i++) {
console.log(`Page ${pages.items[i].index} text: ${pageRanges[i].text}`);
}
});
load(options)
将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()。
load(options?: Word.Interfaces.PageLoadOptions): Word.Page;
参数
- options
- Word.Interfaces.PageLoadOptions
为要加载的对象属性提供选项。
返回
load(propertyNames)
将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()。
load(propertyNames?: string | string[]): Word.Page;
参数
- propertyNames
-
string | string[]
指定要加载的属性的逗号分隔的字符串或字符串数组。
返回
load(propertyNamesAndPaths)
将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()。
load(propertyNamesAndPaths?: {
select?: string;
expand?: string;
}): Word.Page;
参数
- propertyNamesAndPaths
-
{ select?: string; expand?: string; }
propertyNamesAndPaths.select 是指定要加载的属性的逗号分隔字符串,以及 propertyNamesAndPaths.expand 指定要加载的导航属性的逗号分隔字符串。
返回
toJSON()
覆盖 JavaScript toJSON() 方法,以便在将 API 对象传递给 JSON.stringify()时提供更有用的输出。 (JSON.stringify反过来调用 toJSON 传递给它的对象的方法。) 虽然原始 Word.Page 对象是 API 对象, toJSON 但该方法返回一个纯 JavaScript 对象, (类型化为 Word.Interfaces.PageData) ,其中包含从原始对象加载的任何子属性的浅层副本。
toJSON(): Word.Interfaces.PageData;
返回
示例
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/36-pages/get-pages.yaml
// Copies the first page's loaded properties into a plain JavaScript object.
await Word.run(async (context) => {
const firstPage: Word.Page = context.document.activeWindow.activePane.pages.getFirst();
firstPage.load("index,height,width");
await context.sync();
const pageData: Word.Interfaces.PageData = firstPage.toJSON();
console.log("First page data:", pageData);
});
track()
根据文档中的相应更改来跟踪对象,以便进行自动调整。 此调用是 context.trackedObjects.add (thisObject) 的简写。 如果在调用中 .sync 使用此对象,并且在“.run”批处理的顺序执行之外使用此对象,并且在设置属性或对对象调用方法时收到“InvalidObjectPath”错误,则需要在首次创建对象时将对象添加到跟踪对象集合。 如果此对象是集合的一部分,您还应跟踪父集合。
track(): Word.Page;
返回
untrack()
释放与此对象关联的内存(如果先前已跟踪过)。 此调用是 context.trackedObjects.remove (thisObject) 的简写。 拥有许多跟踪对象会降低主机应用程序的速度,因此请在使用完毕后释放所添加的任何对象。 需要在内存释放生效之前调用 context.sync() 。
untrack(): Word.Page;