廣告媒體
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
部分廣告類型 (例如 ImageAd 和 ResponsiveDisplayAd) 包含圖片和其他媒體元素。本指南說明如何使用 Google Ads 腳本上傳及查詢媒體。
上傳映像檔
您可以使用 ImageBuilder
類別上傳圖片,這個類別會採用名稱和圖片資料。資料會以 Blob
資料交換物件的形式提供,可由 Google 雲端硬碟或網址擷取等服務建立。
以下程式碼片段說明如何從外部網址上傳圖片:
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();
您可以使用 MediaSelector
,在 Google Ads 指令碼中查詢各種媒體。使用 withCondition(condition)
述詞,依名稱、類型或其他欄位篩選媒體。舉例來說,以下程式碼片段會找出帳戶中的所有圖片:
let mediaIterator = AdsApp.adMedia().media()
.withCondition("Type = IMAGE")
.get();
while (mediaIterator.hasNext()) {
let image = mediaIterator.next();
}
如需建立支援的廣告並附加媒體的程式碼範例,請參閱廣告文章。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-21 (世界標準時間)。
[null,null,["上次更新時間: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."]]