Google Apps Script

Class EmbeddedChart

Represents a chart that has been embedded into a Spreadsheet.

This example shows how to modify an existing chart:

 
var sheet = SpreadsheetApp.getActiveSheet();
 var range = sheet.getRange("A2:B8")
 var chart = 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:

function newChart(range, sheet) {
   var sheet = SpreadsheetApp.getActiveSheet();
   var chartBuilder = sheet.newChart();
   chartBuilder.addRange(range)
       .setChartType(Charts.ChartType.Line)
       .setOption('title', 'My Line Chart!');
   sheet.insertChart(chartBuilder.build());
 }
 

Methods

MethodReturn typeBrief description
getAs(contentType)BlobReturn the data inside this object as a blob converted to the specified content type.
getBlob()BlobReturn the data inside this object as a blob.
getContainerInfo()ContainerInfoReturns information about where the chart is positioned within a sheet.
getId()StringReturns the id that has been assigned to this object.
getOptions()ChartOptionsReturns the options for this chart, such as height, colors, axes, etc.
getRanges()Range[]Returns the Ranges that this chart uses as a data source.
getType()StringGets the type of this object.
modify()EmbeddedChartBuilderReturns an EmbeddedChartBuilder that can be used to modify this chart.
setId(id)ChartSets the id of this EmbeddedChart to be used with UiApp.

Detailed documentation

getAs(contentType)

Return the data inside this object as a blob converted to the specified content type.

Parameters

NameTypeDescription
contentTypeStringthe 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.

Return

Blob — the data as a blob


getBlob()

Return the data inside this object as a blob.

Return

Blob — the data as a blob


getContainerInfo()

Returns information about where the chart is positioned within a sheet.

 
var ss = SpreadsheetApp.getActiveSpreadsheet();
 var sheet = ss.getSheets()[0];

 var chart = sheet.newChart()
     .setChartType(Charts.ChartType.BAR)
     .addRange(sheet.getRange("A1:B8"))
     .setPosition(5, 5, 0, 0)
     .build();

 var containerInfo = chart.getContainerInfo();

 // Logs the values we 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

ContainerInfo — an object containing the chart container's position


getId()

Returns the id that has been assigned to this object.

This can be used in conjunction with app.getElementById() to retrieve a reference to this object.

Return

String — the id that has been assigned to this object


getOptions()

Returns the options for this chart, such as height, colors, axes, etc.

The returned options are immutable.

Return

ChartOptions — the options for this chart, such as height, colors, axes, etc.


getRanges()

Returns the Ranges that this chart uses as a data source.

 
var ss = SpreadsheetApp.getActiveSpreadsheet();
 var sheet = ss.getSheets()[0];

 var chart = sheet.newChart()
     .setChartType(Charts.ChartType.BAR)
     .addRange(sheet.getRange("A1:B8"))
     .setPosition(5, 5, 0, 0)
     .build();

 var ranges = chart.getRanges();

 // There's only one range as a data source for this chart,
 // so this logs "A1:B8"
 for (var i in ranges) {
   var range = ranges[i];
   Logger.log(range.getA1Notation());
 }
 

Return

Range[] — an array of ranges that serve as this chart's data source


getType()

Gets the type of this object.

Return

String — the object type


modify()

Returns an EmbeddedChartBuilder that can be used to modify this chart. Remember to invoke sheet.updateChart(chart) to save your changes.

 
var sheet = SpreadsheetApp.getActiveSheet();
 var chart = sheet.getCharts()[0];
 chart = chart.modify()
     .setOption('width', 800)
     .setOption('height', 640)
     .setPosition(5, 5, 0, 0)
     .build();
 sheet.updateChart(chart);
 

Return

EmbeddedChartBuilder — a builder for creating embedded charts


setId(id)

Sets the id of this EmbeddedChart to be used with UiApp.

Parameters

NameTypeDescription
idStringthe new id, which can be used to retrieve the EmbeddedChart from app.getElementById(id).

Return

Chart — the EmbeddedChart itself, useful for chaining.

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.