javascript - 谷歌表格 - 宏不会将图表的格式复制到另一个表格
问题描述
我在 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();
};
解决方案
您可以简单地使用此脚本来复制图表:
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]
参考
推荐阅读
- javascript - 如何使用 js 和 css 来修复我的页面变得更宽的视差
- r - 动态设置漂亮的刻度线和标签以包含数据限制
- android - Kotlin RecyclerView 没有附加适配器
- javascript - Javascript 的 Abortable Fetch 会关闭 HTTP 连接吗?
- javascript - 修复 nextjs 组件级 scss 问题
- python - Scape Selenium to Pandas - 列表问题
- amazon-web-services - Is there a way to batch delete AWS Athena queries
- reactjs - click 事件未在来自 ref 的输入元素上触发
- python - 使用 Numpy 过滤两个数据帧
- javascript - 在 reducer 函数中使用 axios 时,状态更新一直等到 promise 解决