首页 > 解决方案 > 谷歌表格 - 宏不会将图表的格式复制到另一个表格

问题描述

我在 Google 表格上,

我正在尝试使用宏将图表从 Sheet1 复制到 Sheet2。

当我运行它时,图表中的值确实从 Sheet1 复制到 Sheet2 但不是格式,它似乎将默认的 Google Sheets 格式设置到由宏创建的图表中。

我想知道如何从 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();
};

标签: javascriptgoogle-apps-scriptgoogle-sheetsgoogle-visualization

解决方案


您可以简单地使用此脚本来复制图表:

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

Sheet1上面的脚本使用方法从 中获取所有图表getCharts。至于复制图表,假设有问题的图表是charts数组中的第一个,已经使用insertChart了带有 的方法。charts[0]

参考


推荐阅读