[英]Google Sheets - Macro doesn´t copy the format of a Chart to another Sheet
I am on Google Sheets,我在 Google 表格上,
I am trying to copy a Chart from a Sheet1 to Sheet2 using a Macro.我正在尝试使用宏将图表从 Sheet1 复制到 Sheet2。
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.当我运行它时,图表中的值确实从 Sheet1 复制到 Sheet2 但不是格式,它似乎将默认的 Google Sheets 格式设置到由宏创建的图表中。
I want to know how do I inherit the same format form the chart from Sheet1 to Sheet2.我想知道如何从 Sheet1 到 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.上面的脚本使用getCharts
方法从Sheet1
获取所有图表。 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.至于复制图表,假设有问题的图表是charts
数组中的第一个,则使用了带有charts[0]
的insertChart
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.