Class EmbeddedChart Stay organized with collections Save and categorize content based on your preferences.
Page Summary
The
EmbeddedChartclass represents a chart embedded in a spreadsheet and provides methods for accessing and modifying chart properties.You can modify an existing chart by getting it from the sheet, using the
modify()method, making changes with the builder, and then updating the chart on the sheet.New charts can be created using a
chartBuilderobtained from the sheet, setting properties like range and chart type, and then inserting the built chart into the sheet.Methods like
getChartId(),getContainerInfo(),getRanges(), andgetOptions()allow retrieval of various chart attributes.The
modify()method returns anEmbeddedChartBuilderfor making changes, and these changes must be saved usingsheet.updateChart().
Represents a chart that has been embedded into a spreadsheet.
This example shows how to modify an existing chart:
constsheet=SpreadsheetApp.getActiveSheet();constrange=sheet.getRange('A2:B8');letchart=sheet.getCharts()[0];chart=chart.modify().addRange(range).setOption('title','Updated!').setOption('animation.duration',500).setPosition(2,2,0,0).build();sheet.updateChart(chart);
This example shows how to create a new chart:
functionnewChart(range){constsheet=SpreadsheetApp.getActiveSheet();constchartBuilder=sheet.newChart();chartBuilder.addRange(range).setChartType(Charts.ChartType.LINE).setOption('title','My Line Chart!');sheet.insertChart(chartBuilder.build());}
Methods
| Method | Return type | Brief description |
|---|---|---|
as | Data | Casts to a data source chart instance if the chart is a data source chart, ornullotherwise. |
get | Blob | Return the data inside this object as a blob converted to the specified content type. |
get | Blob | Return the data inside this object as a blob. |
get | Integer|null | Returns a stable identifier for the chart that is unique across the spreadsheet containing thechart ornull if the chart is not in a spreadsheet. |
get | Container | Returns information about where the chart is positioned within a sheet. |
get | Chart | Returns the strategy to use for handling hidden rows and columns. |
get | Chart | Returns the merge strategy used when more than one range exists. |
get | Integer | Returns the number of rows or columns the range that are treated as headers. |
get | Chart | Returns the options for this chart, such as height, colors, and axes. |
get | Range[] | Returns the ranges that this chart uses as a data source. |
get | Boolean | Iftrue, the rows and columns used to populate the chart are switched. |
modify() | Embedded | Returns anEmbedded that can be used to modify this chart. |
Detailed documentation
asDataSourceChart()
Casts to a data source chart instance if the chart is a data source chart, ornullotherwise.
Return
Data — The data source chart.
getAs(contentType)
Return the data inside this object as a blob converted to the specified content type. Thismethod adds the appropriate extension to the filename—for example, "myfile.pdf". However, itassumes that the part of the filename that follows the last period (if any) is an existingextension that should be replaced. Consequently, "ShoppingList.12.25.2014" becomes"ShoppingList.12.25.pdf".
To view the daily quotas for conversions, seeQuotas for GoogleServices. Newly created Google Workspace domains might be temporarily subject to stricterquotas.
Parameters
| Name | Type | Description |
|---|---|---|
content | String | The MIME type to convert to. For most blobs,'application/pdf' is the only valid option. For images in BMP, GIF, JPEG, or PNG format, any of'image/bmp','image/gif','image/jpeg', or'image/png' are also valid. For a Google Docs document,'text/markdown' is also valid. |
Return
Blob — The data as a blob.
getBlob()
getChartId()
Returns a stable identifier for the chart that is unique across the spreadsheet containing thechart ornull if the chart is not in a spreadsheet.
Return
Integer|null — A stable chart identifier.
getContainerInfo()
Returns information about where the chart is positioned within a sheet.
constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];constchart=sheet.newChart().setChartType(Charts.ChartType.BAR).addRange(sheet.getRange('A1:B8')).setPosition(5,5,0,0).build();constcontainerInfo=chart.getContainerInfo();// Logs the values used in setPosition()Logger.log('Anchor Column: %s\r\nAnchor Row %s\r\nOffset X %s\r\nOffset Y %s',containerInfo.getAnchorColumn(),containerInfo.getAnchorRow(),containerInfo.getOffsetX(),containerInfo.getOffsetY(),);
Return
Container — An object containing the chart container's position.
getHiddenDimensionStrategy()
Returns the strategy to use for handling hidden rows and columns. Defaults toIGNORE_ROWS.
constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];constrange=sheet.getRange('A1:B5');constchart=sheet.newChart().setChartType(Charts.ChartType.BAR).addRange(range).setHiddenDimensionStrategy(Charts.ChartHiddenDimensionStrategy.IGNORE_COLUMNS,).setPosition(5,5,0,0).build();// Logs the strategy to use for hidden rows and columns which is// Charts.ChartHiddenDimensionStrategy.IGNORE_COLUMNS in this case.Logger.log(chart.getHiddenDimensionStrategy());
Return
Chart — The strategy to use for hidden rows and columns.
getMergeStrategy()
Returns the merge strategy used when more than one range exists. IfMERGE_ROWS, row are merged; ifMERGE_COLUMNS, columns are merged. Defaults toMERGE_COLUMNS.
constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];constrange=sheet.getRange('A1:B10');constrange2=sheet.getRange('C1:C10');constchart=sheet.newChart().setChartType(Charts.ChartType.BAR).addRange(range).addRange(range2).setMergeStrategy(Charts.ChartMergeStrategy.MERGE_ROWS).setPosition(5,5,0,0).build();// Logs whether rows of multiple ranges are merged, which is MERGE_ROWS in this// case.Logger.log(chart.getMergeStrategy());
Return
Chart —MERGE_ROWS If rows are merged across multiple ranges;MERGE_COLUMNS if columns are merged across multiple ranges.
getNumHeaders()
Returns the number of rows or columns the range that are treated as headers.
constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];constrange=sheet.getRange('A1:B5');constchart=sheet.newChart().setChartType(Charts.ChartType.BAR).addRange(range).setNumHeaders(1).setPosition(5,5,0,0).build();// Logs the number of rows or columns to use as headers, which is 1 in this// case.Logger.log(chart.getHeaders());
Return
Integer — The number of rows or columns treated as headers. Negative values indicate the headers are auto-detected.
getOptions()
Returns the options for this chart, such as height, colors, and axes.
The returned options are immutable.
Return
Chart — The options for this chart, such as height, colors, and axes.
getRanges()
Returns the ranges that this chart uses as a data source.
constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];constchart=sheet.newChart().setChartType(Charts.ChartType.BAR).addRange(sheet.getRange('A1:B8')).setPosition(5,5,0,0).build();constranges=chart.getRanges();// There's only one range as a data source for this chart,// so this logs "A1:B8"for(constiinranges){constrange=ranges[i];Logger.log(range.getA1Notation());}
Return
Range[] — An array of ranges that serve as this chart's data source.
getTransposeRowsAndColumns()
Iftrue, the rows and columns used to populate the chart are switched. Defaults tofalse.
constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];constrange=sheet.getRange('A1:B5');constchart=sheet.newChart().addRange(range).setChartType(Charts.ChartType.BAR).setTransposeRowsAndColumns(true).setPosition(5,5,0,0).build();// Logs whether rows and columns should be transposed, which is true in this// case.Logger.log(chart.getTransposeRowsAndColumns());
Return
Boolean —True if the rows and columns used to construct the chart are transposed.
modify()
Returns anEmbedded that can be used to modify this chart. Invokesheet.updateChart(chart) to save any changes.
constsheet=SpreadsheetApp.getActiveSheet();letchart=sheet.getCharts()[0];chart=chart.modify().setOption('width',800).setOption('height',640).setPosition(5,5,0,0).build();sheet.updateChart(chart);
Return
Embedded — A builder for creating embedded charts.
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-12-11 UTC.