This builder allows you to edit an EmbeddedChart
. Make sure to call
sheet.updateChart(builder.build())
to save your changes.
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A1: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);
Methods
Method | Return type | Brief description |
---|---|---|
addRange(range) | EmbeddedChartBuilder | Adds a Range to the chart represented by this builder. |
asAreaChart() | EmbeddedAreaChartBuilder | Sets the chart type to AreaChart and returns an EmbeddedAreaChartBuilder . |
asBarChart() | EmbeddedBarChartBuilder | Sets the chart type to BarChart and returns an EmbeddedBarChartBuilder . |
asColumnChart() | EmbeddedColumnChartBuilder | Sets the chart type to ColumnChart and returns an EmbeddedColumnChartBuilder . |
asLineChart() | EmbeddedLineChartBuilder | Sets the chart type to LineChart and returns an EmbeddedLineChartBuilder . |
asPieChart() | EmbeddedPieChartBuilder | Sets the chart type to PieChart and returns an EmbeddedPieChartBuilder . |
asScatterChart() | EmbeddedScatterChartBuilder | Sets the chart type to ScatterChart and returns an EmbeddedScatterChartBuilder . |
asTableChart() | EmbeddedTableChartBuilder | Sets the chart type to TableChart and returns an EmbeddedTableChartBuilder . |
build() | EmbeddedChart | Builds the chart to reflect all changes made to it. |
getChartType() | ChartType | Returns the current chart type. |
getContainer() | ContainerInfo | Return the ContainerInfo, which encapsulates where the chart appears on the sheet. |
getRanges() | Range[] | Returns a copy of the list of ranges currently providing data for this chart. |
removeRange(range) | EmbeddedChartBuilder | Removes the specified Range from the chart represented by this builder. |
setChartType(type) | EmbeddedChartBuilder | Changes the type of chart. |
setOption(option, value) | EmbeddedChartBuilder |
Sets advanced options for this chart. |
setPosition(anchorRowPos, anchorColPos, offsetX, offsetY) | EmbeddedChartBuilder | Sets the position, changing where the chart appears on the sheet. |
Detailed documentation
addRange(range)
Adds a Range to the chart represented by this builder. Will not add the Range if it has already been added to the chart.
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();
sheet.insertChart(chart);
Parameters
Name | Type | Description |
---|---|---|
range | Range | the range to add |
Return
EmbeddedChartBuilder
— the builder for method chaining
asAreaChart()
Sets the chart type to AreaChart and returns an EmbeddedAreaChartBuilder
.
Return
EmbeddedAreaChartBuilder
— a builder for an area chart
asBarChart()
Sets the chart type to BarChart and returns an EmbeddedBarChartBuilder
.
Return
EmbeddedBarChartBuilder
— a builder for a bar chart
asColumnChart()
Sets the chart type to ColumnChart and returns an EmbeddedColumnChartBuilder
.
Return
EmbeddedColumnChartBuilder
— a builder for a column chart
asLineChart()
Sets the chart type to LineChart and returns an EmbeddedLineChartBuilder
.
Return
EmbeddedLineChartBuilder
— a builder for a line chart
asPieChart()
Sets the chart type to PieChart and returns an EmbeddedPieChartBuilder
.
Return
EmbeddedPieChartBuilder
— a builder for a pie chart
asScatterChart()
Sets the chart type to ScatterChart and returns an EmbeddedScatterChartBuilder
.
Return
EmbeddedScatterChartBuilder
— a builder for a scatter chart
asTableChart()
Sets the chart type to TableChart and returns an EmbeddedTableChartBuilder
.
Return
EmbeddedTableChartBuilder
— a builder for a table chart
build()
Builds the chart to reflect all changes made to it. This method will not automatically draw the chart on top of the spreadsheet. A new chart must be inserted via sheet.insertChart(chart), and an existing chart should be updated via sheet.update(chart);
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = sheet.getRange("A1:B5");
var chart = sheet.newChart()
.setChartType(Charts.ChartType.BAR)
.addRange(range)
.setPosition(5, 5, 0, 0)
.build()
sheet.insertChart(chart);
Return
EmbeddedChart
— the created chart, which must still be added to the spreadsheet
getContainer()
Return the ContainerInfo, which encapsulates where the chart appears on the sheet.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var chartBuilder = sheet.newChart()
.setChartType(Charts.ChartType.BAR)
.addRange(sheet.getRange("A1:B8"))
.setPosition(5, 5, 0, 0);
// This method returns the exact same data as Chart#getContainerInfo()
var containerInfo = chartBuilder.getContainer();
// 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
getRanges()
Returns a copy of the list of ranges currently providing data for this chart. Use addRange and removeRange to modify this list.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var chartBuilder = sheet.newChart()
.setChartType(Charts.ChartType.BAR)
.addRange(sheet.getRange("A1:B8"))
.setPosition(5, 5, 0, 0)
var ranges = chartBuilder.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 the chart to be built's data source
removeRange(range)
Removes the specified Range from the chart represented by this builder. Will not throw an error
if the Range is not in this chart.
The range removed must match up with a range added via addRange(range)
, or it will
not be removed, and it will not throw an exception. This method cannot be used to partially
remove values from a range.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var firstRange = sheet.getRange("A1:B5");
var secondRange = sheet.getRange("A6:B8");
var chartBuilder = sheet.newChart()
.setChartType(Charts.ChartType.BAR)
.addRange(firstRange)
// This range will render in a different color
.addRange(secondRange)
.setPosition(5, 5, 0, 0);
// Note that you can use either of these two formats, but the range
// MUST match up with a range that was added via addRange(), or it
// will not be removed, and will not throw an exception
chartBuilder.removeRange(firstRange);
chartBuilder.removeRange(sheet.getRange("A6:B8"));
var chart = chartBuilder.build();
sheet.insertChart(chart);
Parameters
Name | Type | Description |
---|---|---|
range | Range | the range to remove |
Return
EmbeddedChartBuilder
— the builder for method chaining
setChartType(type)
Changes the type of chart. Not all embedded chart types are currently supported. See
ChartType
.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = sheet.getRange("A1:B5");
var chart = sheet.newChart()
.setChartType(Charts.ChartType.BAR)
.addRange(range)
.setPosition(5, 5, 0, 0)
.build()
sheet.insertChart(chart);
Parameters
Name | Type | Description |
---|---|---|
type | ChartType | a chart type |
Return
EmbeddedChartBuilder
— the builder for method chaining
setOption(option, value)
Sets advanced options for this chart. See https://developers.google.com/chart/interactive/docs/reference for what options are available.
This method will NOT validate the option you specify is valid for this chart type nor if the value is of the correct format/structure.
This example shows how to change the animation duration to 1 second and set a legend.
builder.setOption('title', 'Earnings projections');
builder.setOption('animation.duration', 1000);
builder.setOption('legend', {position: 'top', textStyle: {color: 'blue', fontSize: 16}});
Parameters
Name | Type | Description |
---|---|---|
option | String | the name of the option |
value | Object | the value of the option |
Return
EmbeddedChartBuilder
— the builder for method chaining
setPosition(anchorRowPos, anchorColPos, offsetX, offsetY)
Sets the position, changing where the chart appears on the sheet. AnchorRowPos and AnchorColPos are 1-indexed.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = sheet.getRange("A1:B5");
var chart = sheet.newChart()
.setChartType(Charts.ChartType.BAR)
.addRange(range)
.setPosition(5, 5, 0, 0)
.build()
sheet.insertChart(chart);
Parameters
Name | Type | Description |
---|---|---|
anchorRowPos | Integer | the chart's top side will be anchored in this row |
anchorColPos | Integer | the chart's left side will be anchored in this column |
offsetX | Integer | the chart's upper right-hand corner will be offset by this many pixels |
offsetY | Integer | the chart's lower left-hand corner will be offset by this many pixels |
Return
EmbeddedChartBuilder
— the builder for method chaining