[英]How to set number format for each value from single column in Google Sheets depends from value?
I got an array in a single column.我在单列中有一个数组。 I need to reduce fractional part for big numbers like >= 1, keep 1 symbol of fractional part for numbers <1 and >=0,1, keep 2 symbols of fractional part for numbers <0,1 and >=0,01, keep 3 symbols of fractional part for numbers <0,01 and >=0,001.我需要减少大数的小数部分,例如 >= 1,为数字 <1 和 >=0,1 保留小数部分的 1 个符号,为数字 <0,1 和 >=0,01 保留小数部分的 2 个符号,为数字 <0,01 和 >=0,001 保留小数部分的 3 个符号。 All values can not be rounded, for user view only.所有值不能四舍五入,仅供用户查看。 For example:例如:
[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.
I tried to do that but my way is wrong我试图这样做,但我的方式是错误的
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'); } } }
Can you explain why and where I'm wrong?你能解释为什么我错了吗?
Modified script修改脚本
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"); }
Expected result预期结果
1654.123 -> 1654, 23.456 -> 23, 0.43 -> 0,43, 0.02 -> 0,02, 0.037 -> 0,037
in the cells on Google Spreadsheet.您想在 Google 电子表格的单元格中实现1654.123 -> 1654, 23.456 -> 23, 0.43 -> 0,43, 0.02 -> 0,02, 0.037 -> 0,037
类的数字格式。
If my understanding is correct, how about this answer?如果我的理解是正确的,这个答案怎么样? Please think of this as just one of several possible answers.请将此视为几种可能的答案之一。
numFormat
is the range object.在您的脚本中, numFormat
是范围对象。 Under this situation, when for (i = 0; i < numFormat.length; i++){
is used, the for loop is not correctly run (the script in the for loop is not run.), because numFormat.length
is undefined
.在这种情况下,当使用for (i = 0; i < numFormat.length; i++){
,for 循环没有正确运行(for 循环中的脚本没有运行。),因为numFormat.length
是undefined
。.
当您想将十进制运算符从.
to ,
, in this case, how about changing the locale of Spreadsheet?到,
, 在这种情况下,如何更改电子表格的区域设置?
When your script is modified, it becomes as follows.当你的脚本被修改时,它变成如下。
const sh = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sh = ss.getActiveSheet();
And和
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");
If I misunderstood your question and this was not the direction you want, I apologize.如果我误解了您的问题并且这不是您想要的方向,我深表歉意。
Although I'm not sure whether this is your current issue, in your script, when the cells are empty in the column "A", the column "C" might not be the empty.虽然我不确定这是否是您当前的问题,但在您的脚本中,当“A”列中的单元格为空时,“C”列可能不是空的。 If you want to resolve this issue, how about modifying as follows?如果你想解决这个问题,如何修改如下?
result.push([sourceValues[i] * (1 - ratio[i])])
result.push([sourceValues[i][0] ? sourceValues[i][0] * (1 - ratio[i][0]) : ""]);
sourceValues
and ratio
are 2 dimensional arrays.在您的脚本中, sourceValues
和ratio
是二维数组。Unfortunately, I cannot replicate your situation.不幸的是,我无法复制您的情况。 For example, when your script and values are used like below script, the following result is retrieved.例如,当您的脚本和值像下面的脚本一样使用时,将检索以下结果。
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.