Google Sheet Chart: set title of currently selected chart from named cell

by Riccardo   Last Updated February 13, 2018 10:03 AM

There's a sample over here that will show how to set the first's chart title to the active cell:

function onOpen(){
  SpreadsheetApp.getUi().createMenu('Charts').addItem('Update title of the first chart from active cell', 'myFunction').addToUi();
}
function myFunction() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var chart = sheet.getCharts()[0];
  chart = chart.modify()
  .setOption('title', sheet.getActiveCell().getValue() || 'Empty')
  .build();
  sheet.updateChart(chart);
}

The script adds a menu and will update the first's chart title to the current active cell.

It would be interesting to do the contrary, for instance you may have multiple sheets and build charts from each sheet range. The script should allow to set the current selected chart's title to a give named cell.

Assuming the scenario above, and while a chart is selected, pseudocode:

  1. Get the sheet name from currently selected chart
  2. Use reference to cell to get text that will be used for the title, as a general rule each sheet shall have a cell named ChartTitle, therefore using the sheet name from step1 we should be allowed to build a correct reference
  3. Set the currently selected chart title to the returned text

Problems:

  • Is there a function that will return the currently selected chart?
  • If so, is it possible to retrieve the range of the chart in a way the sheet name may be extracted?


Related Questions



Webservice which allows chat with math formulas

Updated August 26, 2017 03:03 AM