选择器
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
选择器可帮助程序员构建用于提取所请求 Google Ads 实体的查询。通过使用选择器,编程人员可以精简实体检索结果列表,并为其排序。大多数选择器提供以下方法:
withCondition()
- 向选择器添加条件。如果使用多个条件,这些条件会通过 AND 运算符组合在一起,换句话说,选择器只会返回满足所有指定条件的实体。
withIds()
- 添加一组 ID 作为条件。基于 ID 的条件将与所有其他条件进行 AND 运算。
forDateRange()
当条件或排序子句引用 Stats 字段(例如 Ctr 或展示次数)时,需要使用 - 。如果您请求所有展示次数超过 100 的广告系列,Google Ads 脚本需要知道要查看的日期范围。
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 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-27。
[null,null,["最后更新时间 (UTC):2025-08-27。"],[[["\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."]]