4
votes

I built a chart in a Google Sheets. Now I need to duplicate the chart.

There is UI copy interface for the charts. I tried to replicate it in Google Apps Script with no luck:

function copyChart() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var chart = sheet.getCharts()[0];
  var chartCopy = sheet.newChart();
  chartCopy = chart;
  sheet.insertChart(chartCopy);
}

How do I make a working script that duplicates the first chart in the current spreadsheet?

2

2 Answers

2
votes

With the current state of the Spreadsheet Service, there is no way to duplicate an existing EmbeddedChart completely. It is possible, though, to copy parts of a chart, and then set the remaining options.

This utility function could be expanded if / when the API becomes more complete. (Or it could be obsoleted if Google perfects cloning!)

/**
 * Returns a new EmbeddedChart instance with some properties 
 * replicated from the original.
 *
 * @param   {EmbeddedChart} original    source chart to be cloned
 * @returns {EmbeddedChart}             new, cloned chart
 */ 
function cloneChart( original ) {
  original = original.modify();  // Necessary for read-access to some properties(!)
  var clone = SpreadsheetApp.getActiveSheet().newChart();

  // Set chart type
  clone.setChartType(original.getChartType());

  // Set position - caller should provide unique position
  var originalContainer = original.getContainer();
  var originalPostion = { anchorRowPos: originalContainer.getAnchorRow(),
                          anchorColPos: originalContainer.getAnchorColumn(), 
                          offsetX: originalContainer.getOffsetX(), 
                          offsetY: originalContainer.getOffsetY()
                     };

  clone.setPosition(originalPostion.anchorRowPos,
                    originalPostion.anchorColPos, 
                    originalPostion.offsetX, 
                    originalPostion.offsetY);

  // Copy ranges
  var ranges = original.getRanges();
  for (r=0; r<ranges.length; r++) {
    clone.addRange(ranges[r]);
  }

  return clone.build();
}

For instance:

function copyChart() {
  var ss = SpreadsheetApp.getActive();
  var sheet = SpreadsheetApp.getActiveSheet();
  var chart = sheet.getCharts()[0];
  var newChart = cloneChart(chart,customLineChart);
  sheet.insertChart(newChart);

  var numcharts = sheet.getCharts().length;
  debugger;
  return;
  var chartBlob = chart.getBlob();
  var builder = sheet.newChart();
  chartCopy = chart;
  sheet.insertChart(chartCopy);
}

/*
 * Customize settings for line charts
 */
function customLineChart( chartBuilder ) {
  chartBuilder.asLineChart()
              .setTitle('Chart Title (Copy)')
              .setOption('legend', {position: 'right', textStyle: {fontSize: 16}})
              .setOption('height', 350)
              .setOption('width', 450);
  // ....  
}

It's not complete, but it's a start. It's really a shame that this part of the API is so inadequate.

Screenshot

0
votes

I know this is a very old thread but I recently had this problem myself and used macro recording as a work around.

Just record, then copy (or move) a chart and you will get access to all its details when you edit the macro.

By copying that portion of code, you can reposition or copy a chart with relative ease which can be useful when building a master sheet and duplicating it etc.