セレクタ
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
セレクタは、リクエストされた Google 広告エンティティを取得するクエリをプログラマーが構築するのに役立ちます。セレクタを使用すると、取得したエンティティのリストを絞り込み、順序を付けることができます。ほとんどのセレクタには、次のようなメソッドがあります。
withCondition()
- セレクタに条件を追加します。複数の条件が使用されている場合、それらは AND 演算で結合されます。つまり、セレクタは、指定されたすべての条件を満たすエンティティのみを返します。
withIds()
- ID のコレクションを条件として追加します。ID ベースの条件は、他のすべての条件と AND で結合されます。
forDateRange()
- は、条件句または順序句が Ctr や Impressions などの Stats フィールドを参照する場合に必要です。100 回以上のインプレッションが発生したすべてのキャンペーンをリクエストする場合は、Google 広告スクリプトで検索する期間を指定する必要があります。
orderBy()
- 返されたエンティティの順序を指定します。
withLimit()
- 返されるエンティティの数を指定された値に制限します。これは、
orderBy()
と組み合わせて「昨日のインプレッション数が最も多い 10 個のキーワード」などの情報を取得する場合に特に便利です。デフォルトでは、すべてのセレクタで上限が 50,000 に設定されます。上限を手動で指定することで、上限を引き上げることができます。
これらのメソッドは任意の順序で呼び出すことができます。例外は orderBy()
です。このメソッドへの複数の呼び出しは複数の順序付け句を指定し、それらは順番に適用されます。次のスニペットを考えてみましょう。
selector = selector.forDateRange("LAST_14_DAYS")
.orderBy("metrics.clicks DESC")
.orderBy("metrics.ctr ASC");
この場合、結果は Clicks を基準にして、降順で並べ替えられます。クリック数が同じ結果は、クリック率の昇順で並べ替えられます。
セレクタのメソッドの呼び出しは連鎖させることができます。次のコードをご覧ください。
var campaignSelector = AdsApp.campaigns();
campaignSelector.withCondition("metrics.clicks > 10");
campaignSelector.withCondition("metrics.impressions > 1000");
campaignSelector.orderBy("metrics.impressions DESC");
campaignSelector.forDateRange("YESTERDAY");
は、次のように簡潔に書き直すことができます。
var campaignSelector = AdsApp.campaigns()
.withCondition("metrics.clicks > 10")
.withCondition("metrics.impressions > 1000")
.orderBy("metrics.impressions DESC")
.forDateRange("YESTERDAY");
セレクタを構築したら、selector.get()
を呼び出して Iterator を取得できます。
効率的なセレクタの使用に関するヒントとコツについては、ベスト プラクティスをご覧ください。
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2025-08-27 UTC。
[null,null,["最終更新日 2025-08-27 UTC。"],[[["\u003cp\u003eSelectors in Google Ads scripts are used to retrieve specific entities like campaigns or keywords by constructing queries.\u003c/p\u003e\n"],["\u003cp\u003eSelectors offer methods like \u003ccode\u003ewithCondition()\u003c/code\u003e, \u003ccode\u003ewithIds()\u003c/code\u003e, \u003ccode\u003eforDateRange()\u003c/code\u003e, \u003ccode\u003eorderBy()\u003c/code\u003e, and \u003ccode\u003ewithLimit()\u003c/code\u003e to refine and order results.\u003c/p\u003e\n"],["\u003cp\u003eMultiple conditions added using \u003ccode\u003ewithCondition()\u003c/code\u003e are treated as AND conditions, requiring entities to satisfy all of them.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eorderBy()\u003c/code\u003e allows sorting results based on specified criteria, and calling it multiple times adds further ordering levels.\u003c/p\u003e\n"],["\u003cp\u003eChaining selector methods simplifies code and enhances readability by applying multiple operations in a single statement.\u003c/p\u003e\n"]]],[],null,["# Selectors help the programmer construct the query that fetches requested\nGoogle Ads entities. With selectors, one can narrow down the list of\nretrieved entities and order it. Most selectors have the following\nmethods:\n\n`withCondition()`\n: Adds a condition to a selector. If multiple conditions are used, they are\n AND-ed together, in other words, the selector will only return entities that\n satisfy **all** of the specified conditions.\n\n`withIds()`\n: Adds a collection of [IDs](/google-ads/scripts/docs/concepts/ids) as a\n condition. An ID-based condition will be AND-ed together with all the others.\n\n`forDateRange()`\n: Is needed when a condition or ordering clause references a\n [Stats](/google-ads/scripts/docs/reference/adsapp/adsapp_stats) field, such as\n Ctr or Impressions. If you request all campaigns with over 100 impressions,\n Google Ads scripts will need to know the date range to look into.\n\n`orderBy()`\n: Specifies the ordering of the returned entities.\n\n`withLimit()`\n: Limits the number of returned entities to the specified value. It is\n particularly useful in conjunction with `orderBy()` in order to fetch things\n like \"10 keywords with most impressions yesterday\". By default, all selectors\n will set the limit to 50,000. You can increase the limit by manually specifying\n a limit.\n\nThese methods can be called in any order. One exception is `orderBy()`, where\norder of calls indeed matters: multiple calls to this method will specify\nmultiple ordering clauses, and they will apply in order. Consider the\nfollowing snippet: \n\n selector = selector.forDateRange(\"LAST_14_DAYS\")\n .orderBy(\"metrics.clicks DESC\")\n .orderBy(\"metrics.ctr ASC\");\n\nThe results will be ordered by Clicks in descending order. Results with equal\nClicks values will be ordered by Ctr in ascending order.\n\nCalls to a selector's methods can be chained together. The following code \n\n var campaignSelector = AdsApp.campaigns();\n campaignSelector.withCondition(\"metrics.clicks \u003e 10\");\n campaignSelector.withCondition(\"metrics.impressions \u003e 1000\");\n campaignSelector.orderBy(\"metrics.impressions DESC\");\n campaignSelector.forDateRange(\"YESTERDAY\");\n\ncan be re-written in a more compact fashion: \n\n var campaignSelector = AdsApp.campaigns()\n .withCondition(\"metrics.clicks \u003e 10\")\n .withCondition(\"metrics.impressions \u003e 1000\")\n .orderBy(\"metrics.impressions DESC\")\n .forDateRange(\"YESTERDAY\");\n\nOnce the selector is constructed, one can obtain an\n[Iterator](/google-ads/scripts/docs/concepts/iterators) from it by calling\n`selector.get()`.\n| **Note:** Some selectors (for example [`AdParamSelector`](/google-ads/scripts/docs/reference/adsapp/adsapp_adparamselector) and [`LabelSelector`](/google-ads/scripts/docs/reference/adsapp/adsapp_labelselector)) expose fewer methods since they operate on entities that are more restricted (don't have any stats or meaningful fields to order by).\n\nRead [Best Practices](/google-ads/scripts/docs/best-practices) for tips\nand tricks on efficient selector usage."]]