开始使用
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
按照以下步骤启动您的第一个脚本。
Google Ads 账号
- 登录您的 Google Ads 账号。
- 在左侧导航菜单的“工具”菜单中,依次选择批量操作和脚本。您也可以在 Google Ads 搜索栏中搜索“脚本”,前往此页面。
- 按“+”图标
添加脚本。您可以从下拉菜单中浏览脚本模板,但为了完成本练习,请选择“新建脚本”。
- 您也可以在“脚本名称”文本字段中为脚本提供名称。这有助于您保持井井有条,并记住脚本的作用。
复制以下代码并粘贴到编辑器区域中,替换 main 函数:
function main() {
// Get the campaign names from all the campaigns
const rows = AdsApp.search('SELECT campaign.name FROM campaign');
console.log('My campaigns:');
// Iterate through the campaigns and print the campaign names
for (const row of rows) {
console.log(row.campaign.name);
}
}
出现提示时,点击授权,以便脚本可以代表您访问账号。您必须为每个脚本执行一次此操作。
点击预览,以在预览模式下运行脚本。
结果将显示在更改 / 日志面板中。在此示例中,您应该会在日志中看到广告系列名称。
如果您对预览结果感到满意,请点击运行以运行脚本并打开脚本历史记录页面。此页面会显示最近运行的脚本,其中包含有关脚本状态和结果的信息。
如需详细查看更改和日志,请点击更改列中与您刚刚运行的脚本对应的链接。您应该会看到之前预览的相同日志输出。
经理账号
您必须先拥有 Google Ads 经理账号,才能运行 Ads Manager 脚本。
- 登录您的 Google Ads 经理账号。
- 在左侧导航菜单的“工具”菜单中,依次选择批量操作和脚本。您也可以在 Google Ads 搜索栏中搜索“脚本”,前往此页面。
- 按“+”图标
添加脚本。您可以从下拉菜单中浏览脚本模板,但为了完成本练习,请选择“新建脚本”。
- 您也可以在“脚本名称”文本字段中为脚本提供名称。这有助于您保持井井有条,并记住脚本的作用。
复制以下代码并粘贴到编辑器区域中,替换 main 函数:
function main() {
// Retrieve all children accounts.
const accountIterator = AdsManagerApp.accounts().get();
// Iterate through the account list.
for (const account of accountIterator) {
// Get stats for the child account.
const stats = account.getStatsFor("THIS_MONTH");
// And log it.
console.log(`${account.getCustomerId()},${stats.getClicks()},` +
`${stats.getImpressions()},${stats.getCost()}`);
}
}
出现提示时,点击授权,以便脚本可以代表您访问账号。您必须为每个脚本执行一次此操作。
点击预览,以在预览模式下运行脚本。
结果将显示在更改 / 日志面板中。在此示例中,您应该会在日志中看到子账号及其指标。
如果您对预览结果感到满意,请点击运行以运行脚本并打开脚本历史记录页面。此页面会显示最近运行的脚本,其中包含有关脚本状态和结果的信息。
如需详细查看更改和日志,请点击更改列中与您刚刚运行的脚本对应的链接。您应该会看到之前预览的相同日志输出。
如需查看更多脚本代码段示例,请参阅我们的示例页面。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-27。
[null,null,["最后更新时间 (UTC):2025-08-27。"],[[["\u003cp\u003eLaunching your first Google Ads script involves signing in, navigating to the Scripts section, and adding a new script.\u003c/p\u003e\n"],["\u003cp\u003eYou need to copy and paste provided code snippets into the script editor to define its functionality, such as analyzing keywords or account statistics.\u003c/p\u003e\n"],["\u003cp\u003eAuthorization is necessary for the script to access your account data, and preview mode allows for testing before full execution.\u003c/p\u003e\n"],["\u003cp\u003eGoogle Ads manager accounts are required to run Ads Manager scripts, which can access and process data from multiple child accounts.\u003c/p\u003e\n"],["\u003cp\u003eMore script examples and resources are available on the Google Ads Scripts examples page for further exploration and customization.\u003c/p\u003e\n"]]],[],null,["# Get started\n\nFollow these steps to launch your first script. \n\n### Google Ads accounts\n\n1. [Sign in](//ads.google.com/) to your Google Ads account.\n2. From the Tools menu in the left navigation, select **Bulk actions** , then **Scripts**. You can also navigate to this page by searching for \"Scripts\" in the Google Ads search bar.\n3. Press the + icon to add a script. You can browse script templates from the drop-down menu, but for the sake of this exercise, select \"New script\".\n4. You can optionally provide a name for your script in the \"Script name\" text field. This can help you stay organized and remember what the script does.\n5. Copy and paste the following code into the editor area, replacing the main\n function:\n\n function main() {\n // Get the campaign names from all the campaigns\n const rows = AdsApp.search('SELECT campaign.name FROM campaign');\n\n console.log('My campaigns:');\n // Iterate through the campaigns and print the campaign names\n for (const row of rows) {\n console.log(row.campaign.name);\n }\n }\n\n6. When prompted, click **Authorize** so the script can access the account on\n your behalf. This has to be done once for each script.\n\n7. Click **Preview** to run the script in [preview mode](/google-ads/scripts/docs/concepts/preview).\n Results will appear in the **Changes** / **Logs** panel. For this example,\n you should see your campaign names displayed in the Logs.\n\n8. When you're happy with the preview results, click **Run** to run your\n script and open the **Script history** page. This page displays the\n most recently run scripts, with information on their status and results.\n\n9. To get a detailed view of changes and logs, click the link in the\n **Changes** column that corresponds to the script you just ran. You should\n see the same log output that was previewed before.\n\n### Manager accounts\n\nYou must first have a\n[Google Ads manager account](//support.google.com/google-ads/answer/6139186)\nto run Ads Manager scripts.\n\n1. [Sign in](//ads.google.com/) to your Google Ads manager account.\n2. From the Tools menu in the left navigation, select **Bulk actions** , then **Scripts**. You can also navigate to this page by searching for \"Scripts\" in the Google Ads search bar.\n3. Press the + icon to add a script. You can browse script templates from the drop-down menu, but for the sake of this exercise, select \"New script\".\n4. You can optionally provide a name for your script in the \"Script name\" text field. This can help you stay organized and remember what the script does.\n5. Copy and paste the following code into the editor area, replacing the main\n function:\n\n function main() {\n // Retrieve all children accounts.\n const accountIterator = AdsManagerApp.accounts().get();\n\n // Iterate through the account list.\n for (const account of accountIterator) {\n // Get stats for the child account.\n const stats = account.getStatsFor(\"THIS_MONTH\");\n // And log it.\n console.log(`${account.getCustomerId()},${stats.getClicks()},` +\n `${stats.getImpressions()},${stats.getCost()}`);\n }\n }\n\n6. When prompted, click **Authorize** so the script can access the account on\n your behalf. This has to be done once for each script.\n\n7. Click **Preview** to run the script in [preview mode](/google-ads/scripts/docs/concepts/preview).\n Results will appear in the **Changes** / **Logs** panel. For this example,\n you should see your children accounts and their metrics displayed in the\n Logs.\n\n8. When you're happy with the preview results, click **Run** to run your\n script and open the **Script history** page. This page displays the\n most recently run scripts, with information on their status and results.\n\n9. To get a detailed view of changes and logs, click the link in the\n **Changes** column that corresponds to the script you just ran. You should\n see the same log output that was previewed before.\n\nFor more sample script snippets, check out our\n[examples page](/google-ads/scripts/docs/examples)."]]