イテレータ
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
イテレータは、次のような場合にオブジェクトのリストを走査するために使用される一般的なプログラミング パターンです。
- 処理開始時にリストのサイズがわからない可能性がある。
- リスト全体を一度にメモリに読み込むと、リソースの使用量が過剰になる可能性があります。
イテレータは、boolean hasNext()
と Object next()
の 2 つのメソッドを公開します。Google 広告スクリプトでは、Google 広告エンティティの取得にイテレータ パターンが使用されます。
イテレータは機能的には通常の配列とあまり変わらず、コードを簡潔にできます。次の例は、配列を順次処理するコードです。
for (var i = 0; i < myArray.length; i++) {
let myObject = myArray[i];
}
イテレータを走査するコードに置き換えます。
while (myIterator.hasNext()) {
let myObject = myIterator.next();
}
次のコードは、アカウント内のすべてのキャンペーンに対してイテレータを使用する方法を示しています。
var campaignIterator = AdsApp.campaigns().get();
while (campaignIterator.hasNext()) {
let campaign = campaignIterator.next();
console.log(`${campaign.getName()}; active? ${campaign.isEnabled()}; ` +
`budget=${campaign.getBudget().getAmount()}`);
}
組み込みの JavaScript 反復処理を使用することもできます。
for (const campaign of AdsApp.campaigns()) {
console.log(`${campaign.getName()}; active? ${campaign.isEnabled()}; ` +
`budget=${campaign.getBudget().getAmount()}`);
}
セレクタに withLimit()
を適用しても、totalNumEntities()
の値は変更されません。次のスニペットの x
と y
は同じ値になります。
var x = AdsApp.keywords().get().totalNumEntities();
var y = AdsApp.keywords().withLimit(5).get().totalNumEntities();
Google 広告エンティティの Iterator を取得するには、まず セレクタを作成する必要があります。
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2025-06-04 UTC。
[[["わかりやすい","easyToUnderstand","thumb-up"],["問題の解決に役立った","solvedMyProblem","thumb-up"],["その他","otherUp","thumb-up"]],[["必要な情報がない","missingTheInformationINeed","thumb-down"],["複雑すぎる / 手順が多すぎる","tooComplicatedTooManySteps","thumb-down"],["最新ではない","outOfDate","thumb-down"],["翻訳に関する問題","translationIssue","thumb-down"],["サンプル / コードに問題がある","samplesCodeIssue","thumb-down"],["その他","otherDown","thumb-down"]],["最終更新日 2025-06-04 UTC。"],[[["Iterators in Google Ads scripts are used to efficiently process lists of objects, especially when dealing with large or unknown-sized datasets, by fetching entities one at a time."],["They offer two primary methods, `hasNext()` to check for more items and `next()` to retrieve the next item, similar to how arrays are traversed but without loading the entire list into memory."],["The Google Ads scripts utilize the Iterator pattern for accessing and manipulating various Google Ads entities like campaigns, allowing for streamlined processing and resource management."],["While applying `withLimit()` to a selector constrains the number of fetched entities, it doesn't affect the overall count obtained via `totalNumEntities()`."],["To retrieve an Iterator of Google Ads objects, you first need to define a Selector that specifies the desired entities and their properties."]]],[]]