我正在寻找Google Sheets函数ROUNDDOWN()和ROUNDUP()的替代品,以便在GAS中使用。搜索Javascript数学函数没有找到任何解决方案。
下面是我要写的代码:
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);
const data = range.getValues();
const chart = c_GetChartByTitle(sheetName, chartTitle);
var margin = 0.1; // a margin below/above the min/max data vals
var decPlaces = 0;
var minVal = Math.min(data);
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; }
minVal = rounddown(minVal*(1-margin), decPlaces);
var maxVal = Math.max(data);
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 );
chart = chart.modify()
.setOption('vAxis.minValue',minVal)
.setOption('vAxis.maxValue',maxVal)
.build;
sheet.updateChart(chart);
}
function c_GetChartByTitle(sheetName, chartTitle) {
// Author: Tanaike
// Link: https://stackoverflow.com/a/64887850/190925
// Date: circa 2020-11
// Purpose: Return a Chart object
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheetByName(sheetName);
if (!sheet) return null;
const charts = sheet.getCharts();
for (let i = 0; i < charts.length; i++) {
var chart = charts[i];
if (chart.getOptions().get("title") == chartTitle) {
return chart;
}
}
return null;
}有没有已知的替代方法,或者我应该尝试编写一些自定义函数来实现这一点?
发布于 2021-08-09 10:10:47
用于自动舍入- Math.round
Math.round(4.5) // 5作为ROUNDDOWN() - Math.floor的替代方案
Math.floor(4.89) // 4作为ROUNDUP() - Math.ceil的替代方案
Math.ceil(4.21) // 5除了使用这些函数外,您不能指定您想要的小数位数,因为您需要将这些函数包装在您自己的自定义函数中,以下是一种方法:
function roundup(value, decimalPlaces){
const power = 10 ** decimalPlaces
return Math.ceil(value * power) / power
}
function rounddown(value, decimalPlaces){
const power = 10 ** decimalPlaces
return Math.floor(value * power) / power
}这将不需要您更改上面的代码。
这段代码只是将您试图查找的舍入到10的数字乘以小数位数的幂,然后对其使用Math.round或ceil,然后将其除以相同的数字。例如,在执行该函数时,函数值可能如下所示:
function roundup(5.23245, 3){
const power = 10 ** 3 // 1000
return Math.ceil(5.23245 * 1000) / 1000 // 5.232
}编辑:
除了现在的函数,还有错误检查和接受负数的功能
function roundup(value, decimalPlaces){
if (!Number.isInteger(decimalPlaces)) throw "decimal places needs to be an integer"
if (decimalPlaces === 0) throw "decimal places can't be 0"
const power = 10 ** Math.abs(decimalPlaces)
if (decimalPlaces < 0) {
return Math.ceil(value / power) * power
} else {
return Math.ceil(value * power) / power
}
}
function rounddown(value, decimalPlaces){
if (!Number.isInteger(decimalPlaces)) throw "decimal places needs to be an integer"
if (decimalPlaces === 0) throw "decimal places can't be 0"
const power = 10 ** Math.abs(decimalPlaces)
if (decimalPlaces < 0) {
return Math.floor(value / power) * power
} else {
return Math.floor(value * power) / power
}
}

https://stackoverflow.com/questions/68709479
复制相似问题