To achieve this, you can use Google Apps Script. Using a trigger that fires when the document is edited onEdit()
and reflecting the new content within the chart using the class EmbeddedChart
.
Example
Assuming the table looks something like this (the example should work with another format):
Name |
Measure 1 |
Measure 2 |
Marshall |
1 |
2 |
Barney |
2 |
3 |
Ted |
3 |
4 |
It will generate a chart like this:
- First you need to open add a new script via
Tools>Script Editor
- Inside the
Code.gs
file, add the following:
Code.gs
const onEdit = (event) => {
// Get the active SpreadSheet
let ss = SpreadsheetApp.getActiveSheet()
// If you want to limitate the range within
// you should use the range within the Event Objects
Logger.log(event.range)
let chart = ss.getCharts()[0]
// Get to A1 to the last available cell
let newRanges = ss.getRange(1, 1, ss.getLastRow(), ss.getLastColumn())
chart = chart.modify()
// Clear the old ones
.clearRanges()
// Add the updated values
.addRange(newRanges)
.build()
ss.updateChart(chart)
}
The above script triggers every time you edit the sheet where it is included. Get the updated data from the spreadsheet, and include it in the chart. Right now the script detects any change in the spreadsheet and adds from the first cell to the last to the chart.
Note: onEdit
is too broad, you can control it using different methods, for example:
if(e.range.getRow()>20) return
For example, adding this new data:
Name |
Measure 1 |
Measure 2 |
Measure 3 |
Marshall |
1 |
2 |
3 |
Barney |
2 |
2 |
4 |
Ted |
3 |
4 |
5 |
Lily |
5 |
6 |
5 |
It will automatically generate this chart:
If you think this should be included in Sheets, you can also send feedback to Google using Help>Help Sheets Improve
or via link.
Documentation