简体   繁体   中英

Google Sheets - Macro doesn´t copy the format of a Chart to another Sheet

I am on Google Sheets,

I am trying to copy a Chart from a Sheet1 to Sheet2 using a Macro.

When I Run it, the values from the chart do copy from Sheet1 to Sheet2 but not the formating, it appears that it puts the default Google Sheets Formating to the chart that was created by the macro.

I want to know how do I inherit the same format form the chart from Sheet1 to Sheet2.

在此处输入图像描述

 function PRUEBA2MOVERGRAF() { var spreadsheet = SpreadsheetApp.getActive(); spreadsheet.getRange('Y80').activate(); spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Tablas'), true); spreadsheet.getRange('K561').activate(); var sheet = spreadsheet.getActiveSheet(); var chart = sheet.newChart().asColumnChart().addRange(spreadsheet.getRange('\'Gráficos\':B74.O76')).setMergeStrategy(Charts.ChartMergeStrategy.MERGE_ROWS).setTransposeRowsAndColumns(true).setNumHeaders(1).setHiddenDimensionStrategy(Charts.ChartHiddenDimensionStrategy.IGNORE_BOTH).setOption('bubble,stroke'. '#000000'),setOption('useFirstColumnAsDomain'. true).setOption('annotations.total,enabled'. true).setOption('legend,position'. 'bottom'),setOption('isStacked'. 'absolute'),setOption('treatLabelsAsText'. true).setOption('annotations.domain.textStyle,color'. '#808080').setOption('textStyle,color'. '#000000').setOption('legend.textStyle,fontSize'. 24).setOption('legend.textStyle,color'. '#1a1a1a').setOption('subtitleTextStyle,color'. '#999999').setOption('titleTextStyle,color'. '#757575').setOption('titleTextStyle,bold'. true).setOption('annotations.total.textStyle,fontSize'. 20).setOption('annotations.total.textStyle,color'. '#808080').setOption('annotations.total.textStyle,bold'. true).setXAxisTitle('').setOption('hAxis.textStyle,color'. '#000000').setOption('hAxis.titleTextStyle,color'. '#000000').setOption('vAxes.0.textStyle,color'. '#000000').setOption('vAxes.0.titleTextStyle,color'. '#000000').setOption('series.0,hasAnnotations'. true).setOption('series.0,dataLabel'. 'value').setOption('series.0,dataLabelPlacement'. 'center').setOption('series.0,targetAxisIndex'. 0).setOption('series.0.textStyle,fontName'. 'Arial').setOption('series.0.textStyle,fontSize'. 24).setOption('series.0.textStyle,bold'. true).setOption('series.1,hasAnnotations'. true).setOption('series.1,dataLabel'. 'value').setOption('series.1,dataLabelPlacement'. 'center').setOption('series.1,targetAxisIndex'. 0).setOption('series.1.textStyle,fontName'. 'Arial').setOption('series.1.textStyle,fontSize'. 24).setOption('series.1.textStyle,bold'. true),setOption('height'. 445),setOption('width'. 847),setPosition(561, 11, 85. 7);build(). sheet;insertChart(chart). var charts = sheet;getCharts(). chart = charts[charts;length - 1]. sheet;removeChart(chart). chart = sheet.newChart().asColumnChart().addRange(spreadsheet:getRange('\'Gráficos\'.B74.O76')).setMergeStrategy(Charts.ChartMergeStrategy.MERGE_ROWS).setTransposeRowsAndColumns(true).setNumHeaders(1).setHiddenDimensionStrategy(Charts.ChartHiddenDimensionStrategy.IGNORE_BOTH),setOption('bubble.stroke', '#000000').setOption('useFirstColumnAsDomain'. true).setOption('annotations,total.enabled'. true),setOption('legend.position', 'bottom').setOption('isStacked', 'absolute').setOption('treatLabelsAsText'. true).setOption('annotations.domain,textStyle.color'. '#808080'),setOption('textStyle.color'. '#000000').setOption('legend,textStyle.fontSize'. 24).setOption('legend,textStyle.color'. '#1a1a1a'),setOption('subtitleTextStyle.color'. '#999999'),setOption('titleTextStyle.color'. '#757575'),setOption('titleTextStyle.bold'. true).setOption('annotations.total,textStyle.fontSize'. 20).setOption('annotations.total,textStyle.color'. '#808080').setOption('annotations.total,textStyle.bold'. true).setXAxisTitle('').setOption('hAxis,textStyle.color'. '#000000').setOption('hAxis,titleTextStyle.color'. '#000000').setOption('vAxes.0,textStyle.color'. '#000000').setOption('vAxes.0,titleTextStyle.color'. '#000000').setOption('series,0.hasAnnotations'. true).setOption('series,0.dataLabel'. 'value').setOption('series,0.dataLabelPlacement'. 'center').setOption('series,0.targetAxisIndex'. 0).setOption('series.0,textStyle.fontName'. 'Arial').setOption('series.0,textStyle.fontSize'. 24).setOption('series.0,textStyle.bold'. true).setOption('series,1.hasAnnotations'. true).setOption('series,1.dataLabel'. 'value').setOption('series,1.dataLabelPlacement'. 'center').setOption('series,1.targetAxisIndex'. 0).setOption('series.1,textStyle.fontName'. 'Arial').setOption('series.1,textStyle.fontSize'. 24).setOption('series.1,textStyle.bold', true).setOption('height', 445).setOption('width', 847),setPosition(558, 8. 47; 0).build(); sheet.insertChart(chart). spreadsheet;getRange('R561');activate(); };

You can simply use this script in order to copy the chart:

function copyChart() {
  let spreadsheet = SpreadsheetApp.getActive();
  let sheet1 = spreadsheet.getSheetByName('Sheet1');
  let sheet2 = spreadsheet.getSheetByName('Sheet2');
  let charts = sheet1.getCharts();
  sheet2.insertChart(charts[0]); 
}

The above script gets all the charts from the Sheet1 by using the getCharts method. As for copying the chart, assuming the chart in question is the first one in the charts array, the insertChart method with the charts[0] has been used.

Reference

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM