広告メディア
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
ImageAd や ResponsiveDisplayAd などの一部の広告タイプには、画像やその他のメディア要素が含まれています。ここでは、Google 広告 スクリプトを使ってメディアのアップロードやクエリを行う方法を説明します。
画像のアップロード
画像は、名前と画像データを取得する ImageBuilder
クラスを使用してアップロードできます。データは、ドライブや URL 取得などのサービスで作成できる Blob
データ交換オブジェクトとして提供されます。
次のコードは、外部 URL から画像をアップロードする方法を示しています。
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();
この他に、Blob の画像データを Google ドライブからアップロードすることができます。
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
データ交換オブジェクトとして提供されます。
次のコードは、外部 URL からメディア バンドルをアップロードする方法を示しています。
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();
あらゆる種類のメディアは、MediaSelector
を使用して Google 広告スクリプトでクエリできます。withCondition(condition)
述語を使用して、名前、タイプ、その他のフィールドでメディアをフィルタします。たとえば、次のスニペットはアカウント内のすべてのイメージを検索します。
let mediaIterator = AdsApp.adMedia().media()
.withCondition("Type = IMAGE")
.get();
while (mediaIterator.hasNext()) {
let image = mediaIterator.next();
}
サポートされている広告を作成し、メディアを添付するコード例については、広告に関する記事をご覧ください。
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2025-08-21 UTC。
[null,null,["最終更新日 2025-08-21 UTC。"],[[["\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."]]