1

I'm looking to overwrite chart1 with the options from chart2. I've tried to extract the options from chart2 using chart.getOptions(), but I can't seem to set those options to chart1.

Ideally every option would be copied over:

  • All Format
  • Reference Range
  • Etc.

Code Sample:

function overwriteChartOptions(chart1, chart2) {
  var options = chart2.getOptions();
  var chart1 = chart1.modify()
    .setOption(options)
    .build();

  sheet1.updateChart(chart1);
}

overwriteChartOptions(sheet.getCharts()[0], sheet.getCharts()[1])

Error Message: Exception: The parameters (String) don't match the method signature for SpreadsheetApp.EmbeddedChartBuilder.setOption.

I've attempted to extract each and every option individually, but I wasn't able to find the documentation where app scripts listed that. All I was able to find is this and it doesn't contain a list of all of the option:

I've looked at alternative methods of overwriting a chart with another with methods like Sheets.Spreadsheets.batchUpdate(), but couldn't figure it out.

Is there a way to copy all options from one chart to another?

Edit: I've tried changing .setOption(options) to .setOptions(options), but received this error: TypeError: chartBuilder.addRange(...).setChartType(...).setOptions is not a function

jeanjai77
  • 93
  • 6
  • is there a `setOptions` method, with an "s"? `setOption` only sets one option at a time... – WhiteHat Jun 27 '22 at 13:46
  • No I've tried it, but it only returns an error: TypeError: chartBuilder.addRange(...).setChartType(...).setOptions is not a function – jeanjai77 Jun 27 '22 at 13:56
  • Related: (reverse engineering the options)https://stackoverflow.com/questions/13594839/google-apps-script-how-to-set-use-column-a-as-labels-in-chart-embedded-in-spr – TheMaster Jun 27 '22 at 14:58
  • Related doc: https://developers.google.com/apps-script/chart-configuration-options – TheMaster Jun 27 '22 at 15:37
  • Are you using the [Charts Service](https://developers.google.com/apps-script/reference/charts) or are you using the [Google Charts API](https://developers.google.com/chart/interactive/docs/reference)? – Gabriel Carballo Jun 27 '22 at 22:23
  • @GabrielCarballo Neither. OP is using the [`EmbeddedChart`](https://developers.google.com/apps-script/reference/spreadsheet/embedded-chart) – TheMaster Jun 27 '22 at 22:51
  • TheMaster is correct. I'm using EmbeddedChart, but open to other solutions! @GabrielCarballo – jeanjai77 Jun 27 '22 at 23:58
  • The main goal is really to overwrite one chart with another. Whichever method is used to accomplish that doesn't bother me as long as it work! I based my question on the setOption method as it seemed like the simplest option. The only thing that can't be done is completely replacing chart1 with a duplicate of chart2. Chart1 has references in other places (google slides), so I'd like to keep it intact so I don't need to write a function to find and update references to it. – jeanjai77 Jun 28 '22 at 00:17
  • I'm guessing api is the way to go. Use `Spreadsheets.get()` to get the chart structure first. – TheMaster Jun 28 '22 at 07:35
  • Could you share an example of the 2 charts you are trying to overwrite? – Gabriel Carballo Jun 28 '22 at 17:46
  • In this case, any two charts would work. Simply create a chart (highlight data range -> Insert -> Chart), then create another with the same data range (or different range, doesn't matter) with a slightly difference style/format. @GabrielCarballo – jeanjai77 Jun 30 '22 at 11:21
  • If you share spreadsheets, do note that [your email address can be accessed by the public](https://meta.stackoverflow.com/questions/394304/). You may also try the [Blank sheet maker here](https://webapps.stackexchange.com/a/138383). – TheMaster Jun 30 '22 at 12:39

0 Answers0