您可以提供可公開存取的網址,也可以提供 base64 編碼圖片字串。如要取得後者,您可以使用下列程式碼,從 Google 雲端硬碟中的圖片建立經過編碼的圖片字串,然後儲存該字串,以便日後搭配 setImageUrl(imageUrl) 使用。這個方法可避免外掛程式需要存取公開圖片網址:
// The following assumes you have the image to use in Google Drive and have its// ID.constimageBytes=DriveApp.getFileById('123abc').getBlob().getBytes();constencodedImageURL=`data:image/jpeg;base64,${Utilities.base64Encode(imageBytes)}`;// You can store encodeImageURL and use it as a parameter to// CardHeader.setImageUrl(imageUrl).
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-26 (世界標準時間)。"],[[["\u003cp\u003e\u003ccode\u003eCardHeader\u003c/code\u003e objects allow you to customize the header of a card, including title, subtitle, and image, within Google Workspace Add-ons and Google Chat apps.\u003c/p\u003e\n"],["\u003cp\u003eYou can set an image for the header using \u003ccode\u003esetImageUrl()\u003c/code\u003e by providing a public URL or a base64 encoded image string.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003esetTitle()\u003c/code\u003e and \u003ccode\u003esetSubtitle()\u003c/code\u003e allow you to define the main text and supporting text for the header, respectively.\u003c/p\u003e\n"],["\u003cp\u003eCustomize the image display with \u003ccode\u003esetImageStyle()\u003c/code\u003e to control cropping and use \u003ccode\u003esetImageAltText()\u003c/code\u003e to provide alternative text for accessibility.\u003c/p\u003e\n"]]],["The `CardHeader` object allows customization of a card's header in Google Workspace add-ons and Google Chat apps. Key actions include setting the header's title using `setTitle()`, a subtitle with `setSubtitle()`, and an image via `setImageUrl()`. The image's appearance can be modified with `setImageStyle()` for cropping and `setImageAltText()` for alternative text. Image URLs can be direct links or base64 encoded strings. Each method returns the `CardHeader` object, enabling method chaining.\n"],null,["CardHeader\n\nThe header of a [Card](/apps-script/reference/card-service/card).\n\nAvailable for Google Workspace add-ons and Google Chat apps.\n\n```javascript\nconst cardHeader = CardService.newCardHeader()\n .setTitle('Card header title')\n .setSubtitle('Card header subtitle')\n .setImageStyle(CardService.ImageStyle.CIRCLE)\n .setImageUrl('https://image.png');\n``` \n\nMethods\n\n| Method | Return type | Brief description |\n|-----------------------------------------------------------|-----------------|--------------------------------------------------------------------------|\n| [setImageAltText(imageAltText)](#setImageAltText(String)) | [CardHeader](#) | Sets the alternative text for the header image. |\n| [setImageStyle(imageStyle)](#setImageStyle(ImageStyle)) | [CardHeader](#) | Sets the cropping of the icon in the card header. |\n| [setImageUrl(imageUrl)](#setImageUrl(String)) | [CardHeader](#) | Sets the image to use in the header by providing its URL or data string. |\n| [setSubtitle(subtitle)](#setSubtitle(String)) | [CardHeader](#) | Sets the subtitle of the card header. |\n| [setTitle(title)](#setTitle(String)) | [CardHeader](#) | Sets the title of the card header. |\n\nDetailed documentation \n\n`set``Image``Alt``Text(imageAltText)` \nSets the alternative text for the header image.\n\nParameters\n\n| Name | Type | Description |\n|--------------------|----------|--------------------------------------------|\n| `image``Alt``Text` | `String` | The alternative text for the header image. |\n\nReturn\n\n\n[CardHeader](#) --- This object, for chaining.\n\n*** ** * ** ***\n\n`set``Image``Style(imageStyle)` \nSets the cropping of the icon in the card header. Defaults to no crop. Optional.\n\nParameters\n\n| Name | Type | Description |\n|----------------|---------------------------------------------------------------|--------------------|\n| `image``Style` | [ImageStyle](/apps-script/reference/card-service/image-style) | The style setting. |\n\nReturn\n\n\n[CardHeader](#) --- This object, for chaining.\n\n*** ** * ** ***\n\n`set``Image``Url(imageUrl)` \nSets the image to use in the header by providing its URL or data string.\n\nThe provided URL can either be a publicly accessible URL or a base64 encoded image string.\nTo obtain the latter, you can use the following code to create an encoded image string from an\nimage in your Google Drive, then store that string for later use with [setImageUrl(imageUrl)](#setImageUrl(String)). This method prevents the need for your add-on to access a publicly\navailable image URL:\n\n```javascript\n// The following assumes you have the image to use in Google Drive and have its\n// ID.\nconst imageBytes = DriveApp.getFileById('123abc').getBlob().getBytes();\nconst encodedImageURL =\n `data:image/jpeg;base64,${Utilities.base64Encode(imageBytes)}`;\n\n// You can store encodeImageURL and use it as a parameter to\n// CardHeader.setImageUrl(imageUrl).\n```\n\nParameters\n\n| Name | Type | Description |\n|--------------|----------|-----------------------------------------------------------------------|\n| `image``Url` | `String` | The URL address of a hosted image to use, or an encoded image string. |\n\nReturn\n\n\n[CardHeader](#) --- This object, for chaining.\n\n*** ** * ** ***\n\n`set``Subtitle(subtitle)` \nSets the subtitle of the card header. Optional.\n\nParameters\n\n| Name | Type | Description |\n|------------|----------|---------------------------|\n| `subtitle` | `String` | The header subtitle text. |\n\nReturn\n\n\n[CardHeader](#) --- This object, for chaining.\n\n*** ** * ** ***\n\n`set``Title(title)` \nSets the title of the card header. Required.\n\nParameters\n\n| Name | Type | Description |\n|---------|----------|------------------|\n| `title` | `String` | The header text. |\n\nReturn\n\n\n[CardHeader](#) --- This object, for chaining."]]