イテレータ
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
イテレータは、オブジェクトのリストをトラバースするときに使用される一般的なプログラミング パターンです。
- 処理開始時にリストのサイズがわからない可能性がある。
- リスト全体を一度にメモリに読み込むと、リソースの消費が過剰になる可能性があります。
イテレータは、boolean hasNext()
と Object next()
という 2 つのメソッドを公開します。Google 広告スクリプトは、Google 広告エンティティの取得に Iterator パターンを使用します。
機能的には、イテレータは通常の配列とあまり変わりませんが、コードをより簡潔にすることができます。次の例は、配列を順次処理するコードです。
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 を取得するには、まず Selector を構築する必要があります。
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2025-08-27 UTC。
[null,null,["最終更新日 2025-08-27 UTC。"],[[["\u003cp\u003eIterators 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.\u003c/p\u003e\n"],["\u003cp\u003eThey offer two primary methods, \u003ccode\u003ehasNext()\u003c/code\u003e to check for more items and \u003ccode\u003enext()\u003c/code\u003e to retrieve the next item, similar to how arrays are traversed but without loading the entire list into memory.\u003c/p\u003e\n"],["\u003cp\u003eThe Google Ads scripts utilize the Iterator pattern for accessing and manipulating various Google Ads entities like campaigns, allowing for streamlined processing and resource management.\u003c/p\u003e\n"],["\u003cp\u003eWhile applying \u003ccode\u003ewithLimit()\u003c/code\u003e to a selector constrains the number of fetched entities, it doesn't affect the overall count obtained via \u003ccode\u003etotalNumEntities()\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eTo retrieve an Iterator of Google Ads objects, you first need to define a Selector that specifies the desired entities and their properties.\u003c/p\u003e\n"]]],[],null,["# Iterators are a common programming pattern used for traversing a list of objects\nwhen\n\n- The size of the list may not be known from the start.\n- Loading the entire list into memory at once may prove overly resource intensive.\n\nIterators expose two methods: `boolean hasNext()` and `Object next()`.\nGoogle Ads scripts use the Iterator pattern for fetching Google Ads entities.\n\nFunctionally, iterators are not too different from regular arrays, and can make\nyour code more concise. Compare the code that traverses an array: \n\n for (var i = 0; i \u003c myArray.length; i++) {\n let myObject = myArray[i];\n }\n\nwith code that traverses an iterator: \n\n while (myIterator.hasNext()) {\n let myObject = myIterator.next();\n }\n\nThe following code demonstrates the usage of an iterator over all Search and\nDisplay campaigns in your account: \n\n var campaignIterator = AdsApp.campaigns().get();\n\n while (campaignIterator.hasNext()) {\n let campaign = campaignIterator.next();\n console.log(`${campaign.getName()}; active? ${campaign.isEnabled()}; ` +\n `budget=${campaign.getBudget().getAmount()}`);\n }\n\nYou can also use built-in JavaScript iteration: \n\n for (const campaign of AdsApp.campaigns()) {\n console.log(`${campaign.getName()}; active? ${campaign.isEnabled()}; ` +\n `budget=${campaign.getBudget().getAmount()}`);\n }\n\nApplication of `withLimit()` to a selector does not change the value of\n`totalNumEntities()`. `x` and `y` in the following snippet will have the same\nvalue: \n\n var x = AdsApp.keywords().get().totalNumEntities();\n var y = AdsApp.keywords().withLimit(5).get().totalNumEntities();\n\nIn order to obtain an Iterator of Google Ads entities, you must construct a\n[Selector](/google-ads/scripts/docs/concepts/selectors) first."]]