[英]How to set number format for each value from single column in Google Sheets depends from value?
我在单列中有一个数组。 我需要减少大数的小数部分,例如 >= 1,为数字 <1 和 >=0,1 保留小数部分的 1 个符号,为数字 <0,1 和 >=0,01 保留小数部分的 2 个符号,为数字 <0,01 和 >=0,001 保留小数部分的 3 个符号。 所有值不能四舍五入,仅供用户查看。 例如:
[33800]->33800;
[468]->468;
[]-> "";
[1170.0000000000002]->1170;
[2437.5]->2437;
[2762.5]->2762;
[322.4]->322;
[1430.0000000000002]->1430;
[910]->910;
[1300]->1300;
[52]->52;
[0.023]->0,023;
[6500]->6500.
我试图这样做,但我的方式是错误的
function recalculate() { const sh = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); const sourceValues = sh.getRange(1, 1, sh.getLastRow()).getValues(); const ratio = sh.getRange(1, 2, sh.getLastRow()).getValues(); const targetRange = sh.getRange(1, 3, sh.getLastRow()); let result = []; for (let i = 0; i < sourceValues.length; i++){ result.push([sourceValues[i] * (1 - ratio[i])]) } console.log(result) let numFormat = targetRange.setValues(result); for (i = 0; i < numFormat.length; i++){ switch (numFormat[i] > 0) { case numFormat < 0.1: numFormat[i].setNumberFormat('#,##0.00') ; case numFormat < 1: numFormat[i].setNumberFormat('#,#0.0') ; default: numFormat[i].setNumberFormat('#0'); } } }
你能解释为什么我错了吗?
修改脚本
function recalculate() { const ss = SpreadsheetApp.getActiveSpreadsheet(); const sh = ss.getActiveSheet(); const sourceValues = sh.getRange(1, 1, sh.getLastRow()).getValues(); const ratio = sh.getRange(1, 2, sh.getLastRow()).getValues(); const targetRange = sh.getRange(1, 3, sh.getLastRow()); let result = []; console.log(JSON.stringify(sourceValues)) //log: [[13000],[468],[""],[3900],[3250],[3250],[520],[2600],[910],[1300],[52],[0.023],[6500]] let value; for (let i = 0; i < sourceValues.length; i++){ value = sourceValues[i][0] if (value === "" || value === 0) { result.push([""]) }else{ result.push([value * (1 - ratio[i])])} } console.log(JSON.stringify(result)) //log: [[33800],[468],[""],[1170.0000000000002],[2437.5],[2762.5],[322.4],[1430.0000000000002],[910],[1300],[52],[0.023],[6500]] let numFormat = targetRange.setValues(result); const rangeList = result.reduce((ar, [e], i) => { if (e > 1) ar.push(`C${i + 1}`); return ar; }, []); sh.getRangeList(rangeList).setNumberFormat("#"); ss.setSpreadsheetLocale("fr_CA"); }
预期结果
1654.123 -> 1654, 23.456 -> 23, 0.43 -> 0,43, 0.02 -> 0,02, 0.037 -> 0,037
类的数字格式。
如果我的理解是正确的,这个答案怎么样? 请将此视为几种可能的答案之一。
numFormat
是范围对象。 在这种情况下,当使用for (i = 0; i < numFormat.length; i++){
,for 循环没有正确运行(for 循环中的脚本没有运行。),因为numFormat.length
是undefined
。.
到,
, 在这种情况下,如何更改电子表格的区域设置?
当你的脚本被修改时,它变成如下。
const sh = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sh = ss.getActiveSheet();
和
for (i = 0; i < numFormat.length; i++){
switch (numFormat[i] > 0) {
case numFormat < 0.1:
numFormat[i].setNumberFormat('#,##0.00')
;
case numFormat < 1:
numFormat[i].setNumberFormat('#,#0.0')
;
default: numFormat[i].setNumberFormat('#0');
}
}
const rangeList = result.reduce((ar, [e], i) => {
if (e > 1) ar.push(`C${i + 1}`);
return ar;
}, []);
sh.getRangeList(rangeList).setNumberFormat("#");
ss.setSpreadsheetLocale("fr_CA");
如果我误解了您的问题并且这不是您想要的方向,我深表歉意。
虽然我不确定这是否是您当前的问题,但在您的脚本中,当“A”列中的单元格为空时,“C”列可能不是空的。 如果你想解决这个问题,如何修改如下?
result.push([sourceValues[i] * (1 - ratio[i])])
result.push([sourceValues[i][0] ? sourceValues[i][0] * (1 - ratio[i][0]) : ""]);
sourceValues
和ratio
是二维数组。不幸的是,我无法复制您的情况。 例如,当您的脚本和值像下面的脚本一样使用时,将检索以下结果。
33800
468
1170
2438
2763
322
1430
910
1300
52
0,023
6500
const result = [[33800],[468],[""],[1170.0000000000002],[2437.5],[2762.5],[322.4],[1430.0000000000002],[910],[1300],[52],[0.023],[6500]];
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sh = ss.getActiveSheet();
const targetRange = sh.getRange(1, 3, sh.getLastRow());
let numFormat = targetRange.setValues(result);
const rangeList = result.reduce((ar, [e], i) => {
if (e > 1) ar.push(`C${i + 1}`);
return ar;
}, []);
sh.getRangeList(rangeList).setNumberFormat("#");
ss.setSpreadsheetLocale("fr_CA");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.