繁体   English   中英

如何为Google表格中单列中的每个值设置数字格式取决于值?

[英]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"); }
谷歌表上的结果

这是我得到的

预期结果

预期结果

  • 您想在 Google 电子表格的单元格中实现1654.123 -> 1654, 23.456 -> 23, 0.43 -> 0,43, 0.02 -> 0,02, 0.037 -> 0,037类的数字格式。
    • 您想使用这些值作为数字。
  • 您想使用 Google Apps 脚本来实现这一点。

如果我的理解是正确的,这个答案怎么样? 请将此视为几种可能的答案之一。

改装要点:

  • 在您的脚本中, numFormat是范围对象。 在这种情况下,当使用for (i = 0; i < numFormat.length; i++){ ,for 循环没有正确运行(for 循环中的脚本没有运行。),因为numFormat.lengthundefined
  • 当您想将十进制运算符从. , , 在这种情况下,如何更改电子表格的区域设置?
    • 在 pnuts 的回答中,建议将加拿大(法语)作为语言环境。 参考

修改后的脚本:

当你的脚本被修改时,它变成如下。

从:

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");
  • 从您的脚本中,它假设“C”列的数字格式已修改。

参考:

如果我误解了您的问题并且这不是您想要的方向,我深表歉意。

添加1:

虽然我不确定这是否是您当前的问题,但在您的脚本中,当“A”列中的单元格为空时,“C”列可能不是空的。 如果你想解决这个问题,如何修改如下?

从:

result.push([sourceValues[i] * (1 - ratio[i])])

到:

result.push([sourceValues[i][0] ? sourceValues[i][0] * (1 - ratio[i][0]) : ""]);
  • 在您的脚本中, sourceValuesratio是二维数组。

添加2:

不幸的是,我无法复制您的情况。 例如,当您的脚本和值像下面的脚本一样使用时,将检索以下结果。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM