广告媒体
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
某些广告类型(例如 ImageAd 和 ResponsiveDisplayAd)包含图片和其他媒体元素。本指南介绍如何使用 Google Ads 脚本上传和查询媒体。
上传映像
可以使用 ImageBuilder
类上传图片,该类接受名称和图片数据。数据以 Blob
数据交换对象的形式提供,该对象可由 Drive 或 网址 Fetch 等服务创建。
下面的代码段显示了如何从外部网址上传图片:
let imageUrl = "http://www.example.com/example.png";
let imageBlob = UrlFetchApp.fetch(imageUrl).getBlob();
let mediaOperation = AdsApp.adMedia().newImageBuilder()
.withName("IMAGE_NAME")
.withData(imageBlob)
.build();
还可以从 Google 云端硬盘加载图片 Blob:
let imageFileId = "IMAGE_FILE_ID";
let imageBlob = DriveApp.getFileById(imageFileId).getBlob();
let mediaOperation = AdsApp.adMedia().newImageBuilder()
.withName("IMAGE_NAME")
.withData(imageBlob)
.build();
媒体包是包含 HTML5 素材资源的 ZIP 归档文件,可用于制作 HTML5 广告。
使用 MediaBundleBuilder
类上传媒体包,该类接受名称和文件数据。与图片一样,数据以 Blob
数据交换对象的形式提供。
下面的代码段显示了如何从外部网址上传媒体捆绑包:
let mediaBundleUrl = "http://www.example.com/example.zip";
let mediaBundleBlob = UrlFetchApp.fetch(mediaBundleUrl).getBlob();
let mediaOperation = AdsApp.adMedia().newMediaBundleBuilder()
.withName("bundle name")
.withData(mediaBundleBlob)
.build();
在 Google Ads 脚本中,可以使用 MediaSelector
查询各种类型的媒体。
使用 withCondition(condition)
谓词按名称、类型或其他字段过滤媒体。例如,以下代码段会查找账号中的所有图片:
let mediaIterator = AdsApp.adMedia().media()
.withCondition("Type = IMAGE")
.get();
while (mediaIterator.hasNext()) {
let image = mediaIterator.next();
}
如需查看一些使用附加媒体创建受支持广告的代码示例,请参阅我们的广告文章。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-21。
[null,null,["最后更新时间 (UTC):2025-08-21。"],[[["\u003cp\u003eThis guide explains how to upload and query images and media bundles using Google Ads scripts for enhanced ad creatives.\u003c/p\u003e\n"],["\u003cp\u003eYou can upload images and media bundles from external URLs or Google Drive using the \u003ccode\u003eImageBuilder\u003c/code\u003e and \u003ccode\u003eMediaBundleBuilder\u003c/code\u003e classes.\u003c/p\u003e\n"],["\u003cp\u003eQuery existing media using the \u003ccode\u003eMediaSelector\u003c/code\u003e to filter by criteria such as name or type, enabling you to manage and reuse your assets.\u003c/p\u003e\n"],["\u003cp\u003eRefer to the ads article for practical examples of creating ads with attached media to leverage these functionalities.\u003c/p\u003e\n"]]],[],null,["Some ad types, such as\n[ImageAd](/google-ads/scripts/docs/reference/adsapp/adsapp_imagead) and\n[ResponsiveDisplayAd](/google-ads/scripts/docs/reference/adsapp/adsapp_responsivedisplayad),\ncontain images and other media elements. This guide describes how to upload and\nquery media using Google Ads scripts.\n\nUploading images\n\nImages can be uploaded using the\n[`ImageBuilder`](/google-ads/scripts/docs/reference/adsapp/adsapp_imagebuilder)\nclass, which takes a name and image data. The data is provided as a\n[`Blob`](/apps-script/reference/base/blob) data interchange object that can be\ncreated by services such as [Drive](/apps-script/reference/drive) or\n[URL fetch](/apps-script/reference/url-fetch).\n\nThe following snippet shows how to upload an image from an external URL: \n\n let imageUrl = \"http://www.example.com/example.png\";\n let imageBlob = UrlFetchApp.fetch(imageUrl).getBlob();\n let mediaOperation = AdsApp.adMedia().newImageBuilder()\n .withName(\"IMAGE_NAME\")\n .withData(imageBlob)\n .build();\n\nAlternatively, the image blob may be loaded from Google Drive: \n\n let imageFileId = \"IMAGE_FILE_ID\";\n let imageBlob = DriveApp.getFileById(imageFileId).getBlob();\n let mediaOperation = AdsApp.adMedia().newImageBuilder()\n .withName(\"IMAGE_NAME\")\n .withData(imageBlob)\n .build();\n\nUploading media bundles\n\nMedia bundles are ZIP archives that contain HTML5 assets, which can be used to\ncreate [HTML5 ads](//support.google.com/google-ads/answer/1722096#otherhtml5).\nUpload media bundles with the\n[`MediaBundleBuilder`](/google-ads/scripts/docs/reference/adsapp/adsapp_mediabundlebuilder)\nclass, which takes a name and file data. As with images, data is provided as\na [`Blob`](/apps-script/reference/base/blob) data interchange object.\n\nThe following snippet shows how to upload a media bundle from an external URL: \n\n let mediaBundleUrl = \"http://www.example.com/example.zip\";\n let mediaBundleBlob = UrlFetchApp.fetch(mediaBundleUrl).getBlob();\n let mediaOperation = AdsApp.adMedia().newMediaBundleBuilder()\n .withName(\"bundle name\")\n .withData(mediaBundleBlob)\n .build();\n\nQuerying media\n\nMedia of every type can be queried in Google Ads scripts using a\n[`MediaSelector`](/google-ads/scripts/docs/reference/adsapp/adsapp_mediaselector).\nUse the\n[`withCondition()`](/google-ads/scripts/docs/reference/adsapp/adsapp_mediaselector#withCondition_1)\npredicate to filter media by name, type, or other fields. For example, the\nfollowing snippet finds all images in an account: \n\n let mediaIterator = AdsApp.adMedia().media()\n .withCondition(\"Type = IMAGE\")\n .get();\n while (mediaIterator.hasNext()) {\n let image = mediaIterator.next();\n }\n\nCreating ads with media\n\nSee our [ads article](/google-ads/scripts/docs/examples/ads) for some code\nexamples of creating supported ads with attached media."]]