Class CellImage
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
属性
属性 | 类型 | 说明 |
valueType | ValueType | 单元格图片的值类型,即 ValueType.IMAGE 。 |
详细文档
getAltTextDescription()
返回此图片的替代文本说明。
返回
String
- 替代文本说明。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getAltTextTitle()
返回此图片的替代文本标题。
返回
String
- 替代文本标题。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getContentUrl()
返回 Google 托管的图片网址。此网址会标记为请求者的账号,因此知道此网址的任何人都可以以原始请求者的身份有效访问图片。如果电子表格的共享设置发生变化,您可能会失去对图片的访问权限。返回的网址会在短时间后过期。
const range = SpreadsheetApp.getActiveSpreadsheet().getRange("Sheet1!A1");
const value = range.getValue();
if (value.valueType == SpreadsheetApp.ValueType.IMAGE) {
console.log(value.getContentUrl());
}
返回
String
- 图片在 Google 上的托管网址。
toBuilder()
根据当前图片属性创建单元格图片构建器。使用 CellImageBuilder.setSourceUrl(url)
设置新图片的来源网址。然后,您可以使用 Range.setValue(value)
或 Range.setValues(values)
将其添加到单元格中。
const ss = SpreadsheetApp.getActiveSpreadsheet();
const range = ss.getRange("Sheet1!A1");
const value = range.getValue();
if (value.valueType == SpreadsheetApp.ValueType.IMAGE) {
const newImage =
value.toBuilder()
.setSourceUrl(
'https://www.gstatic.com/images/branding/productlogos/apps_script/v10/web-64dp/logo_apps_script_color_1x_web_64dp.png',
)
.build();
const newRange = ss.getRange("Sheet1!A2");
newRange.setValue(newImage);
}
返回
CellImageBuilder
- 一个构建器,用于根据给定的图片属性创建图片值类型。
已弃用的方法
getUrl()
已弃用。对于大多数新插入的图片,来源网址不可用。
获取图片的来源网址。如果网址不可用,则返回空字符串。对于大多数新插入的图片,无论图片是如何插入的,源网址都不可用。
返回
String
- 图片的来源网址(如果有);否则返回空字符串。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-26。
[null,null,["最后更新时间 (UTC):2025-07-26。"],[[["\u003cp\u003e\u003ccode\u003eCellImage\u003c/code\u003e allows you to add images to spreadsheet cells using \u003ccode\u003eSpreadsheetApp.newCellImage()\u003c/code\u003e and \u003ccode\u003eCellImageBuilder\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eYou can set an image to a cell using \u003ccode\u003eRange.setValue()\u003c/code\u003e or \u003ccode\u003eRange.setValues()\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eCellImage\u003c/code\u003e provides methods to access image properties like alt text, content URL, and source URL.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003etoBuilder()\u003c/code\u003e creates an image value type, necessary for placing images into cells.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eCellImage\u003c/code\u003e has a single property, \u003ccode\u003evalueType\u003c/code\u003e, which is always \u003ccode\u003eValueType.IMAGE\u003c/code\u003e.\u003c/p\u003e\n"]]],["To incorporate images into spreadsheet cells, create a new image value using `SpreadsheetApp.newCellImage()` and `CellImageBuilder`. Add the image to a cell via `Range.setValue()` or `Range.setValues()`. Key methods include: `getAltTextDescription()`, `getAltTextTitle()`, `getContentUrl()`, and `toBuilder()`. The method `getUrl()` is deprecated. `getContentUrl()` returns a Google-hosted URL to the image. `toBuilder()` creates a `CellImageBuilder` to modify image properties. `valueType` property is `ValueType.IMAGE`.\n"],null,["# Class CellImage\n\nCellImage\n\nRepresents an image value in a cell. To add an image to a cell, you must create a new image value\nfor the image using [SpreadsheetApp.newCellImage()](/apps-script/reference/spreadsheet/spreadsheet-app#newCellImage()) and [CellImageBuilder](/apps-script/reference/spreadsheet/cell-image-builder). Then you can use [Range.setValue(value)](/apps-script/reference/spreadsheet/range#setValue(Object)) or [Range.setValues(values)](/apps-script/reference/spreadsheet/range#setValues(Object)) to\nadd the image value to the cell. \n\n### Properties\n\n| Property | Type | Description |\n|---------------|------------------------------------------------------------|-----------------------------------------------------------------|\n| `value``Type` | [ValueType](/apps-script/reference/spreadsheet/value-type) | The value type of the cell image, which is `Value``Type.IMAGE`. |\n\n### Methods\n\n| Method | Return type | Brief description |\n|-----------------------------------------------------|---------------------------------------------------------------------------|---------------------------------------------------------------------|\n| [getAltTextDescription()](#getAltTextDescription()) | `String` | Returns the alt text description for this image. |\n| [getAltTextTitle()](#getAltTextTitle()) | `String` | Returns the alt text title for this image. |\n| [getContentUrl()](#getContentUrl()) | `String` | Returns a Google-hosted URL to the image. |\n| [toBuilder()](#toBuilder()) | [CellImageBuilder](/apps-script/reference/spreadsheet/cell-image-builder) | Creates a cell image builder based on the current image properties. |\n\n### Deprecated methods\n\n| Method | Return type | Brief description |\n|-----------------------|-------------|------------------------------|\n| [getUrl()](#getUrl()) | `String` | Gets the image's source URL. |\n\nDetailed documentation\n----------------------\n\n### `get``Alt``Text``Description()`\n\nReturns the alt text description for this image.\n\n#### Return\n\n\n`String` --- The alt text description.\n\n#### Authorization\n\nScripts that use this method require authorization with one or more of the following [scopes](/apps-script/concepts/scopes#setting_explicit_scopes):\n\n- `https://www.googleapis.com/auth/spreadsheets.currentonly`\n- `https://www.googleapis.com/auth/spreadsheets`\n\n*** ** * ** ***\n\n### `get``Alt``Text``Title()`\n\nReturns the alt text title for this image.\n\n#### Return\n\n\n`String` --- The alt text title.\n\n#### Authorization\n\nScripts that use this method require authorization with one or more of the following [scopes](/apps-script/concepts/scopes#setting_explicit_scopes):\n\n- `https://www.googleapis.com/auth/spreadsheets.currentonly`\n- `https://www.googleapis.com/auth/spreadsheets`\n\n*** ** * ** ***\n\n### `get``Content``Url()`\n\nReturns a Google-hosted URL to the image. This URL is tagged with the account of the requester,\nso anyone with the URL effectively accesses the image as the original requester. Access to the\nimage might be lost if the spreadsheet's sharing settings change. The returned URL expires\nafter a short period of time.\n\n```javascript\nconst range = SpreadsheetApp.getActiveSpreadsheet().getRange(\"Sheet1!A1\");\nconst value = range.getValue();\nif (value.valueType == SpreadsheetApp.ValueType.IMAGE) {\n console.log(value.getContentUrl());\n}\n```\n\n#### Return\n\n\n`String` --- The Google-hosted URL of the image.\n\n*** ** * ** ***\n\n### `to``Builder()`\n\nCreates a cell image builder based on the current image properties. Use [CellImageBuilder.setSourceUrl(url)](/apps-script/reference/spreadsheet/cell-image-builder#setSourceUrl(String)) to set the source URL of the new image. Then you can add it to a\ncell using [Range.setValue(value)](/apps-script/reference/spreadsheet/range#setValue(Object)) or [Range.setValues(values)](/apps-script/reference/spreadsheet/range#setValues(Object)).\n\n```javascript\nconst ss = SpreadsheetApp.getActiveSpreadsheet();\nconst range = ss.getRange(\"Sheet1!A1\");\nconst value = range.getValue();\nif (value.valueType == SpreadsheetApp.ValueType.IMAGE) {\n const newImage =\n value.toBuilder()\n .setSourceUrl(\n 'https://www.gstatic.com/images/branding/productlogos/apps_script/v10/web-64dp/logo_apps_script_color_1x_web_64dp.png',\n )\n .build();\n const newRange = ss.getRange(\"Sheet1!A2\");\n newRange.setValue(newImage);\n}\n```\n\n#### Return\n\n\n[CellImageBuilder](/apps-script/reference/spreadsheet/cell-image-builder) --- A builder that creates an image value type based on the given image properties.\n\nDeprecated methods\n------------------\n\n### `get``Url()`\n\n\n**Deprecated.** For most newly inserted images, the source URL is unavailable.\n\nGets the image's source URL. Returns an empty string if the URL is unavailable. For most newly\ninserted images, the source URL is unavailable regardless how the image is inserted.\n\n#### Return\n\n\n`String` --- The image's source URL if available; returns an empty string otherwise.\n\n#### Authorization\n\nScripts that use this method require authorization with one or more of the following [scopes](/apps-script/concepts/scopes#setting_explicit_scopes):\n\n- `https://www.googleapis.com/auth/spreadsheets.currentonly`\n- `https://www.googleapis.com/auth/spreadsheets`"]]