Word.Page class
ドキュメント内のページを表します。
Page オブジェクトは、ページ レイアウトとコンテンツを管理します。
- Extends
注釈
使用元
例
// 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() | API オブジェクトが |
| 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
オブジェクトに関連付けられている要求コンテキスト。 これにより、アドインのプロセスが Office ホスト アプリケーションのプロセスに接続されます。
context: RequestContext;
プロパティ値
height
[ページ設定] ダイアログ ボックスで定義された用紙の高さをポイント単位で取得します。
readonly height: number;
プロパティ値
number
注釈
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
[ページ設定] ダイアログ ボックスで定義された用紙の幅をポイント単位で取得します。
readonly width: number;
プロパティ値
number
注釈
メソッドの詳細
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;
パラメーター
省略可能。 範囲の場所は、 whole、 start、または end である必要があります。
返品
注釈
例
// 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()
API オブジェクトが JSON.stringify() に渡されるときに、より有用な出力を提供するために、JavaScript toJSON() メソッドをオーバーライドします。 (次に、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;