google-apps-script - GAS:修改图表纵轴最小值和最大值
问题描述
感谢@iansedano 的roundup() 和rounddown() 等效项,以及@Tanaike 过去的帖子,我有一个函数可以计算图表垂直轴的最小值和最大值。但是,当我尝试修改图表时出现错误:
Exception: The parameters (String) don't match the method signature for SpreadsheetApp.Sheet.updateChart
对于修改,我使用了以下文档示例: Class EmbeddedChart
我在以下位置找到的选项: 图表配置选项
我不确定,但问题可能是可以有两个垂直轴,并且必须以某种方式指定左或右?
function test(){
c_ScaleVerticalAxis('Chart', 'Stock SMA3', 'C6:C1263');
}
function c_ScaleVerticalAxis(sheetName, chartTitle, rangeA1) {
// Author: Max Hugen
// Date: 2021-08-09
// Purpose: Modify MinValue & MaxValue of Vert.Axis to suit Value Data
// Params: rangeA1 of Values, eg "C6:C"
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheetByName(sheetName);
const range = sheet.getRange(rangeA1);
// Ref: @Tanaike, https://stackoverflow.com/a/64887850/190925
var chart = c_GetChartByTitle(sheetName, chartTitle);
var margin = 0.05; // a margin below/above the min/max data vals
var decPlaces = 0;
// Ref: @Tanaike, https://stackoverflow.com/a/45203334/190925
var ar = Array.prototype.concat.apply([], range.getValues());
var minVal = Math.min.apply(null, ar);
Logger.log('Data minVal: ' + minVal);
if (minVal > 100000) { decPlaces = -5; } else
if (minVal > 10000) { decPlaces = -4; } else
if (minVal > 1000) { decPlaces = -3; } else
if (minVal > 100) { decPlaces = -2; } else
{ decPlaces = -1; }
// Ref: @iansedano, https://stackoverflow.com/a/68710322/190925
minVal = rounddown(minVal*(1-margin), decPlaces);
Logger.log('Chart minVal: ' + minVal);
var maxVal = Math.max.apply(null, ar);
Logger.log('Data maxVal: ' + maxVal);
if (maxVal > 100000) { decPlaces = -5; } else
if (maxVal > 10000) { decPlaces = -4; } else
if (maxVal > 1000) { decPlaces = -3; } else
if (maxVal > 100) { decPlaces = -2; } else
{ decPlaces = -1; }
maxVal = roundup(maxVal*(1+margin), decPlaces );
Logger.log('Chart maxVal: ' + maxVal);
chart = chart.modify()
.setOption('vAxis.minValue',minVal)
.setOption('vAxis.maxValue',maxVal)
.build;
sheet.updateChart(chart);
}
解决方案
似乎vAxis.minValue
并且vAxis.maxValue
仅适用于Continuous Charts。
要为Discrete Charts设置最小和最大垂直轴,请分别使用vAxis.viewWindow.min
和vAxis.viewWindow.max
。
如配置选项中所述。
示例离散图表(数据列类型为string
)
示例代码:
function updateChart(){
var sheet = SpreadsheetApp.getActiveSheet();
var chart = sheet.getCharts()[0];
var newChart = chart.modify().setOption("vAxis.viewWindow.max", 20).setOption("vAxis.viewWindow.min", 5).build();
sheet.updateChart(newChart);
}
示例连续图(数据列类型为以下之一:number
、date
或)datetime
timeofday
示例代码:
function updateChart1(){
var sheet = SpreadsheetApp.getActiveSheet();
Logger.log(sheet.getName());
var chart = sheet.getCharts()[0];
var newChart = chart.modify().setOption("vAxis.maxValue", 30).setOption("vAxis.minValue", 0).build();
sheet.updateChart(newChart);
}
输出:
推荐阅读
- css - 当每一篇文章(高度适合内容)一个接一个开始时,我如何构建一个容器
- python-3.x - 将多个转换应用于 PCollection :: Error WriteToBigQuery
- .net - 在 .NET Web API(非核心)上使用 AutoMapper
- javascript - 如何使用 Javascript 关闭带有关闭按钮的超级菜单?
- php - php if else 程序,使用第一个“if”语句并忽略其余部分
- symfony - Symfony 4.3 - 无法自动装配引用类“Symfony\Component\Security\Core\Security”的服务参数
- c# - .NET WEB API2 从 JSON 输出中的子类中检索字段
- php - Laravel 5.8 - 未发送验证和欢迎电子邮件
- python - Robotarium 示例代码中的 ModuleNotFoundError
- javascript - 我如何让 kahootbot 多次加入?