Class DataViewDefinitionBuilder

  • DataViewDefinitionBuilder is used to build DataViewDefinition objects.

  • ThesetColumns method allows specifying which columns to include in the data view and can also define column roles.

  • Thebuild method finalizes and returns the DataViewDefinition object.

  • An example demonstrates usingsetColumns to display a subset of data from a spreadsheet in a chart.

  • Another example shows how to usesetColumns with column roles to style chart elements based on data.

DataViewDefinitionBuilder

Builder forDataViewDefinition objects.

Here's an example of using the builder. The data isimported from a Google spreadsheet.

functiondoGet(){// This example creates two table charts side by side. One uses a data view// definition to restrict the number of displayed columns.// Get sample data from a spreadsheet.constdataSourceUrl='https://docs.google.com/spreadsheet/tq?range=A1%3AF'+'&key=0Aq4s9w_HxMs7dHpfX05JdmVSb1FpT21sbXd4NVE3UEE&gid=4&headers=-1';// Create a chart to display all of the data.constoriginalChart=Charts.newTableChart().setDimensions(600,500).setDataSourceUrl(dataSourceUrl).build();// Create another chart to display a subset of the data (only columns 1 and// 4).constdataViewDefinition=Charts.newDataViewDefinition().setColumns([0,3]);constlimitedChart=Charts.newTableChart().setDimensions(200,500).setDataSourceUrl(dataSourceUrl).setDataViewDefinition(dataViewDefinition).build();consthtmlOutput=HtmlService.createHtmlOutput();constoriginalChartData=Utilities.base64Encode(originalChart.getAs('image/png').getBytes(),);constoriginalChartUrl=`data:image/png;base64,${encodeURI(originalChartData)}`;constlimitedChartData=Utilities.base64Encode(limitedChart.getAs('image/png').getBytes(),);constlimitedChartUrl=`data:image/png;base64,${encodeURI(limitedChartData)}`;htmlOutput.append('<table><tr><td>');htmlOutput.append(`<img border="1" src="${originalChartUrl}">`);htmlOutput.append('</td><td>');htmlOutput.append(`<img border="1" src="${limitedChartUrl}">`);htmlOutput.append('</td></tr></table>');returnhtmlOutput;}

Methods

MethodReturn typeBrief description
build()DataViewDefinitionBuilds and returns the data view definition object that was built using this builder.
setColumns(columns)DataViewDefinitionBuilderSets the indexes of the columns to include in the data view as well as specifying role-columninformation.

Detailed documentation

build()

Builds and returns the data view definition object that was built using this builder.

Return

DataViewDefinition — A data view definition object that was built using this builder.


setColumns(columns)

Sets the indexes of the columns to include in the data view as well as specifying role-columninformation. This subset of column indexes refer to the columns of the data source that thedata view is derived from.

A column role describes the purpose of the data in that column: for example, a column mighthold data describing tooltip text, data point annotations, or uncertainty indicators. For moredetails, seeDataTable Roles in the Google Chartsdocumentation.

Assuming a spreadsheet with the following data in A1:C3:

'abc', 20, 'blue';'def', 30, 'red';'ghi', 40, 'orange';
The following code creates a bar chart where each bar is a different color. The colors areassigned via a style "role column".
constCOLUMN_SPEC=[0,// categories1,// counts{sourceColumn:2,role:'style'},];functionroleColumnChart(){constspreadsheet=SpreadsheetApp.getActiveSpreadsheet();constsheet=spreadsheet.getActiveSheet();constviewSpec=Charts.newDataViewDefinition().setColumns(COLUMN_SPEC).build();constchartBuilder=sheet.newChart().setChartType(Charts.ChartType.BAR).setDataViewDefinition(viewSpec).setOption('useFirstColumnAsDomain',true).setPosition(5,1,0,0).setOption('hAxis',{title:'Counts'}).setOption('vAxis',{title:'Categories'}).addRange(sheet.getRange('A1:C3'));sheet.insertChart(chartBuilder.build());}

Parameters

NameTypeDescription
columnsObject[]An array of column indexes, or column descriptions (an object), to include in the data view. The column descriptions define a column role. The data table and the enumeration for data view columns are zero-based.

Return

DataViewDefinitionBuilder — This builder, useful for chaining.

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.