Charts are an essential tool used to visually represent data in a spreadsheet. An embedded chart is simply a chart on a worksheet with Google Apps Script that allows you to visually communicate your data. It can be customized according to your requirement and it provides a lot of visual representation of data, which makes it user-friendly.

Google Apps Script is a powerful tool that can be used to create, edit, and manipulate sheets, charts, and other objects in a spreadsheet. In this guide, we’ll cover how to create an embedded chart using Google Apps Script, including how to customize its elements.

Step 1: Accessing the Script Editor

Before we begin, navigate to the Google Sheet you’d like to work on. In the menu bar, click Tools -> Script editor to open a new tab and access the Script Editor.

Step 2: Writing Code

To write code for a chart, you need to start with an object reference to the sheet containing the data you want to use. Here’s the basic code structure of a chart:

function createChart() {
    var sheet = SpreadsheetApp.getActiveSheet(); // get the active sheet
    var chart = sheet.newChart() // create a new chart object
        .setChartType(Charts.ChartType.LINE) // set the chart type
        .addRange(sheet.getRange("A1:B5")) // specify the range
        .setPosition(5, 5, 0, 0) // specify chart position
        .build(); // build the chart
    sheet.insertChart(chart); // insert the chart to sheet
}

Here’s how the code works:

  1. getActiveSheet() gets the active sheet from the spreadsheet
  2. newChart() creates a new chart object
  3. setChartType() specifies the type of chart you want to create (in this case, a line chart)
  4. addRange() specifies the data range used to create the chart
  5. setPosition() sets the position of the chart on the spreadsheet
  6. build() builds the chart object
  7. sheet.insertChart(chart) inserts the chart to the sheet.

In this example, we’re using a Line chart. You can change it to other chart types by modifying the parameter in the `.setChartType()` method. Likewise, you can change the data range and position to your preference.

You could also customize your chart by adding headers, setting the color, modifying the scale or format of its elements. Here are some additional methods you can use to customize your chart:

  1. setOption() sets the chart options
  2. setOption('title', 'My Chart') sets the chart title
  3. setOption('height', 300) sets the height of the chart
  4. setOption('width', 500) sets the width of the chart
  5. setOption('fill', 20) sets the fill values for the chart bars
function customizeChart() {
    var sheet = SpreadsheetApp.getActiveSheet(); // get the active sheet
    var chart = sheet.newChart() // create a new chart object
        .setChartType(Charts.ChartType.COLUMN) // specify chart type
        .addRange(sheet.getRange("A1:B5")) // specify data range
        .setPosition(5, 5, 0, 0) // specify chart position
        .setOption('title', 'My Chart') // set chart title
        .setOption('height', 300) // set chart height
        .setOption('width', 500) // set chart width
        .setOption('fill', 20) // set chart fill values
        .build(); // build the chart
    sheet.insertChart(chart); // insert the chart to sheet
}

Step 3: Running the Code

Once you’ve finished writing your code for the chart, save your script by clicking on the save button or use the keyboard shortcut Ctrl+S. To run your script, click the Run button. Then, if the code runs without any errors, you will be able to see the result in the worksheet.