Я пытаюсь написать простой макрос-скрипт, который создает столбчатую диаграмму для каждого выделенного столбца данных.
/** @OnlyCurrentDoc */
function macro1() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('B:B').activate();
var sheet = spreadsheet.getActiveSheet();
var chart = sheet.newChart()
.asPieChart()
.addRange(spreadsheet.getRange('B1:B6'))
.setMergeStrategy(Charts.ChartMergeStrategy.MERGE_COLUMNS)
.setTransposeRowsAndColumns(false)
.setNumHeaders(1)
.setHiddenDimensionStrategy(Charts.ChartHiddenDimensionStrategy.IGNORE_BOTH)
.setOption('applyAggregateData', 0)
.setOption('useFirstColumnAsDomain', true)
.setOption('isStacked', 'false')
.setOption('title', 'Count of Do you like fruit?')
.setPosition(5, 3, 137, 15)
.build();
sheet.insertChart(chart);
var charts = sheet.getCharts();
chart = charts[charts.length - 1];
sheet.removeChart(chart);
chart = sheet.newChart()
.asColumnChart()
.addRange(spreadsheet.getRange('B1:B6'))
.setMergeStrategy(Charts.ChartMergeStrategy.MERGE_COLUMNS)
.setTransposeRowsAndColumns(false)
.setNumHeaders(1)
.setHiddenDimensionStrategy(Charts.ChartHiddenDimensionStrategy.IGNORE_BOTH)
.setOption('applyAggregateData', 0)
.setOption('bubble.stroke', '#000000')
.setOption('useFirstColumnAsDomain', true)
.setOption('isStacked', 'false')
.setOption('title', 'Count of Do you like fruit?')
.setOption('annotations.domain.textStyle.color', '#808080')
.setOption('textStyle.color', '#000000')
.setOption('legend.textStyle.color', '#191919')
.setOption('pieSliceTextStyle.color', '#000000')
.setOption('titleTextStyle.color', '#757575')
.setOption('annotations.total.textStyle.color', '#808080')
.setXAxisTitle('Count of Do you like fruit?')
.setOption('series.0.aggregateFunction', 'count')
.setPosition(5, 3, 137, 15)
.build();
sheet.insertChart(chart);
charts = sheet.getCharts();
chart = charts[charts.length - 1];
sheet.removeChart(chart);
chart = sheet.newChart()
.asColumnChart()
.addRange(spreadsheet.getRange('B1:B6'))
.setMergeStrategy(Charts.ChartMergeStrategy.MERGE_COLUMNS)
.setTransposeRowsAndColumns(false)
.setNumHeaders(1)
.setHiddenDimensionStrategy(Charts.ChartHiddenDimensionStrategy.IGNORE_BOTH)
.setOption('applyAggregateData', 0)
.setOption('bubble.stroke', '#000000')
.setOption('useFirstColumnAsDomain', true)
.setOption('isStacked', 'false')
.setOption('title', 'Count of Do you like fruit?')
.setOption('annotations.domain.textStyle.color', '#808080')
.setOption('textStyle.color', '#000000')
.setOption('legend.textStyle.color', '#191919')
.setOption('pieSliceTextStyle.color', '#000000')
.setOption('titleTextStyle.color', '#757575')
.setOption('annotations.total.textStyle.color', '#808080')
.setXAxisTitle('Count of Do you like fruit?')
.setOption('hAxis.textStyle.color', '#000000')
.setOption('vAxes.0.textStyle.color', '#000000')
.setOption('series.0.aggregateFunction', 'count')
.setPosition(5, 3, 137, 15)
.build();
sheet.insertChart(chart);
charts = sheet.getCharts();
chart = charts[charts.length - 1];
sheet.removeChart(chart);
chart = sheet.newChart()
.asColumnChart()
.addRange(spreadsheet.getRange('B1:B6'))
.setMergeStrategy(Charts.ChartMergeStrategy.MERGE_COLUMNS)
.setTransposeRowsAndColumns(false)
.setNumHeaders(1)
.setHiddenDimensionStrategy(Charts.ChartHiddenDimensionStrategy.IGNORE_BOTH)
.setOption('applyAggregateData', 0)
.setOption('bubble.stroke', '#000000')
.setOption('useFirstColumnAsDomain', true)
.setOption('isStacked', 'false')
.setOption('title', 'Count of Do you like fruit?')
.setOption('annotations.domain.textStyle.color', '#808080')
.setOption('textStyle.color', '#000000')
.setOption('legend.textStyle.color', '#191919')
.setOption('pieSliceTextStyle.color', '#000000')
.setOption('titleTextStyle.color', '#757575')
.setOption('annotations.total.textStyle.color', '#808080')
.setXAxisTitle('Count of Do you like fruit?')
.setOption('hAxis.textStyle.color', '#000000')
.setOption('vAxes.0.textStyle.color', '#000000')
.setOption('series.0.aggregateFunction', 'count')
.setOption('series.0.hasAnnotations', true)
.setOption('series.0.dataLabel', 'value')
.setPosition(5, 3, 137, 15)
.build();
sheet.insertChart(chart);
};
function examplemacro() {
var spreadsheet = SpreadsheetApp.getActive();
var sheet = spreadsheet.getActiveSheet();
sheet.getRange(1, spreadsheet.getCurrentCell().getColumn() - 3, sheet.getMaxRows(), 1).activate();
sheet = spreadsheet.getActiveSheet();
var chart = sheet.newChart()
.asPieChart()
.addRange(spreadsheet.getCurrentCell().offset(0, 0, 6, 1))
.setMergeStrategy(Charts.ChartMergeStrategy.MERGE_COLUMNS)
.setTransposeRowsAndColumns(false)
.setNumHeaders(1)
.setHiddenDimensionStrategy(Charts.ChartHiddenDimensionStrategy.IGNORE_BOTH)
.setOption('applyAggregateData', 0)
.setOption('useFirstColumnAsDomain', true)
.setOption('isStacked', 'false')
.setOption('title', 'Count of Do you like fruit?')
.setPosition(10, 4, 100, 11)
.build();
sheet.insertChart(chart);
var charts = sheet.getCharts();
chart = charts[charts.length - 1];
sheet.removeChart(chart);
chart = sheet.newChart()
.asColumnChart()
.addRange(spreadsheet.getCurrentCell().offset(0, 0, 6, 1))
.setMergeStrategy(Charts.ChartMergeStrategy.MERGE_COLUMNS)
.setTransposeRowsAndColumns(false)
.setNumHeaders(1)
.setHiddenDimensionStrategy(Charts.ChartHiddenDimensionStrategy.IGNORE_BOTH)
.setOption('applyAggregateData', 0)
.setOption('bubble.stroke', '#000000')
.setOption('useFirstColumnAsDomain', true)
.setOption('isStacked', 'false')
.setOption('title', 'Count of Do you like fruit?')
.setOption('annotations.domain.textStyle.color', '#808080')
.setOption('textStyle.color', '#000000')
.setOption('legend.textStyle.color', '#191919')
.setOption('pieSliceTextStyle.color', '#000000')
.setOption('titleTextStyle.color', '#757575')
.setOption('annotations.total.textStyle.color', '#808080')
.setXAxisTitle('Count of Do you like fruit?')
.setOption('series.0.aggregateFunction', 'count')
.setPosition(10, 4, 100, 11)
.build();
sheet.insertChart(chart);
charts = sheet.getCharts();
chart = charts[charts.length - 1];
sheet.removeChart(chart);
chart = sheet.newChart()
.asColumnChart()
.addRange(spreadsheet.getCurrentCell().offset(0, 0, 6, 1))
.setMergeStrategy(Charts.ChartMergeStrategy.MERGE_COLUMNS)
.setTransposeRowsAndColumns(false)
.setNumHeaders(1)
.setHiddenDimensionStrategy(Charts.ChartHiddenDimensionStrategy.IGNORE_BOTH)
.setOption('bubble.stroke', '#000000')
.setOption('useFirstColumnAsDomain', true)
.setOption('isStacked', 'false')
.setOption('title', 'Count of Do you like fruit?')
.setOption('annotations.domain.textStyle.color', '#808080')
.setOption('textStyle.color', '#000000')
.setOption('legend.textStyle.color', '#191919')
.setOption('pieSliceTextStyle.color', '#000000')
.setOption('titleTextStyle.color', '#757575')
.setOption('annotations.total.textStyle.color', '#808080')
.setXAxisTitle('Count of Do you like fruit?')
.setOption('hAxis.textStyle.color', '#000000')
.setPosition(10, 4, 100, 11)
.build();
sheet.insertChart(chart);
charts = sheet.getCharts();
chart = charts[charts.length - 1];
sheet.removeChart(chart);
chart = sheet.newChart()
.asColumnChart()
.addRange(spreadsheet.getCurrentCell().offset(0, 0, 6, 1))
.setMergeStrategy(Charts.ChartMergeStrategy.MERGE_COLUMNS)
.setTransposeRowsAndColumns(false)
.setNumHeaders(1)
.setHiddenDimensionStrategy(Charts.ChartHiddenDimensionStrategy.IGNORE_BOTH)
.setOption('applyAggregateData', 0)
.setOption('bubble.stroke', '#000000')
.setOption('useFirstColumnAsDomain', true)
.setOption('isStacked', 'false')
.setOption('title', 'Count of Do you like fruit?')
.setOption('annotations.domain.textStyle.color', '#808080')
.setOption('textStyle.color', '#000000')
.setOption('legend.textStyle.color', '#191919')
.setOption('pieSliceTextStyle.color', '#000000')
.setOption('titleTextStyle.color', '#757575')
.setOption('annotations.total.textStyle.color', '#808080')
.setXAxisTitle('Count of Do you like fruit?')
.setOption('hAxis.textStyle.color', '#000000')
.setOption('vAxes.0.textStyle.color', '#000000')
.setOption('series.0.aggregateFunction', 'sum')
.setPosition(10, 4, 100, 11)
.build();
sheet.insertChart(chart);
charts = sheet.getCharts();
chart = charts[charts.length - 1];
sheet.removeChart(chart);
chart = sheet.newChart()
.asColumnChart()
.addRange(spreadsheet.getCurrentCell().offset(0, 0, 6, 1))
.setMergeStrategy(Charts.ChartMergeStrategy.MERGE_COLUMNS)
.setTransposeRowsAndColumns(false)
.setNumHeaders(0)
.setHiddenDimensionStrategy(Charts.ChartHiddenDimensionStrategy.IGNORE_BOTH)
.setOption('applyAggregateData', 0)
.setOption('bubble.stroke', '#000000')
.setOption('useFirstColumnAsDomain', true)
.setOption('isStacked', 'false')
.setOption('title', 'Count of Do you like fruit?')
.setOption('annotations.domain.textStyle.color', '#808080')
.setOption('textStyle.color', '#000000')
.setOption('legend.textStyle.color', '#191919')
.setOption('pieSliceTextStyle.color', '#000000')
.setOption('titleTextStyle.color', '#757575')
.setOption('annotations.total.textStyle.color', '#808080')
.setXAxisTitle('Count of Do you like fruit?')
.setOption('hAxis.textStyle.color', '#000000')
.setOption('vAxes.0.textStyle.color', '#000000')
.setOption('series.0.aggregateFunction', 'sum')
.setPosition(10, 4, 100, 11)
.build();
sheet.insertChart(chart);
charts = sheet.getCharts();
chart = charts[charts.length - 1];
sheet.removeChart(chart);
chart = sheet.newChart()
.asColumnChart()
.addRange(spreadsheet.getCurrentCell().offset(0, 0, 6, 1))
.setMergeStrategy(Charts.ChartMergeStrategy.MERGE_COLUMNS)
.setTransposeRowsAndColumns(false)
.setNumHeaders(1)
.setHiddenDimensionStrategy(Charts.ChartHiddenDimensionStrategy.IGNORE_BOTH)
.setOption('applyAggregateData', 0)
.setOption('bubble.stroke', '#000000')
.setOption('useFirstColumnAsDomain', true)
.setOption('isStacked', 'false')
.setOption('title', 'Count of Do you like fruit?')
.setOption('annotations.domain.textStyle.color', '#808080')
.setOption('textStyle.color', '#000000')
.setOption('legend.textStyle.color', '#191919')
.setOption('pieSliceTextStyle.color', '#000000')
.setOption('titleTextStyle.color', '#757575')
.setOption('annotations.total.textStyle.color', '#808080')
.setXAxisTitle('Count of Do you like fruit?')
.setOption('hAxis.textStyle.color', '#000000')
.setOption('vAxes.0.textStyle.color', '#000000')
.setOption('series.0.aggregateFunction', 'sum')
.setPosition(10, 4, 100, 11)
.build();
sheet.insertChart(chart);
charts = sheet.getCharts();
chart = charts[charts.length - 1];
sheet.removeChart(chart);
chart = sheet.newChart()
.asColumnChart()
.addRange(spreadsheet.getCurrentCell().offset(0, 0, 6, 1))
.setMergeStrategy(Charts.ChartMergeStrategy.MERGE_COLUMNS)
.setTransposeRowsAndColumns(false)
.setNumHeaders(1)
.setHiddenDimensionStrategy(Charts.ChartHiddenDimensionStrategy.IGNORE_BOTH)
.setOption('applyAggregateData', 0)
.setOption('bubble.stroke', '#000000')
.setOption('useFirstColumnAsDomain', true)
.setOption('isStacked', 'false')
.setOption('title', 'Count of Do you like fruit?')
.setOption('annotations.domain.textStyle.color', '#808080')
.setOption('textStyle.color', '#000000')
.setOption('legend.textStyle.color', '#191919')
.setOption('pieSliceTextStyle.color', '#000000')
.setOption('titleTextStyle.color', '#757575')
.setOption('annotations.total.textStyle.color', '#808080')
.setXAxisTitle('Count of Do you like fruit?')
.setOption('hAxis.textStyle.color', '#000000')
.setOption('vAxes.0.textStyle.color', '#000000')
.setOption('series.0.aggregateFunction', 'sum')
.setPosition(10, 4, 100, 11)
.build();
sheet.insertChart(chart);
charts = sheet.getCharts();
chart = charts[charts.length - 1];
sheet.removeChart(chart);
chart = sheet.newChart()
.asColumnChart()
.addRange(spreadsheet.getCurrentCell().offset(0, 0, 6, 1))
.setMergeStrategy(Charts.ChartMergeStrategy.MERGE_COLUMNS)
.setTransposeRowsAndColumns(false)
.setNumHeaders(1)
.setHiddenDimensionStrategy(Charts.ChartHiddenDimensionStrategy.IGNORE_BOTH)
.setOption('applyAggregateData', 0)
.setOption('bubble.stroke', '#000000')
.setOption('useFirstColumnAsDomain', true)
.setOption('isStacked', 'false')
.setOption('title', 'Count of Do you like fruit?')
.setOption('annotations.domain.textStyle.color', '#808080')
.setOption('textStyle.color', '#000000')
.setOption('legend.textStyle.color', '#191919')
.setOption('pieSliceTextStyle.color', '#000000')
.setOption('titleTextStyle.color', '#757575')
.setOption('annotations.total.textStyle.color', '#808080')
.setXAxisTitle('Count of Do you like fruit?')
.setOption('hAxis.textStyle.color', '#000000')
.setOption('vAxes.0.textStyle.color', '#000000')
.setOption('series.0.aggregateFunction', 'sum')
.setOption('series.0.hasAnnotations', true)
.setOption('series.0.dataLabel', 'value')
.setPosition(10, 4, 100, 11)
.build();
sheet.insertChart(chart);
};
Пример таблицы показывает 3 столбца с вопросами и ответами под ним. Также записан макрос 1.
То, что я хотел бы сделать, - это создать макрос, с помощью которого я смог начать с помощью курсора, выделяющего весь столбец, а затем запустить макрос, чтобы создать простую гистограмму для данных в столбце, который я выделил.
Проблема в том, что, хотя я думал, что все делал правильно, проверяя, что использовал относительные значения, при попытке запустить макрос в следующем столбце он работает только для столбца и данных, которые я первоначально выделил в макрос .. ясно, что я хочу, чтобы он работал для любого столбца, который я выделил.
Есть ли какой-нибудь макрос, который может помочь? Очень ценится.
Спасибо.