简体   繁体   English

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

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

这是我得到的

Expected result预期结果

预期结果

  • You want to achieve the number format like 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类的数字格式。
    • You want to use the values as the number.您想使用这些值作为数字。
  • You want to achieve this using Google Apps Script.您想使用 Google Apps 脚本来实现这一点。

If my understanding is correct, how about this answer?如果我的理解是正确的,这个答案怎么样? Please think of this as just one of several possible answers.请将此视为几种可能的答案之一。

Modification points:改装要点:

  • In your scrit, 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.lengthundefined
  • When you want to change the decimal operator from .当您想将十进制运算符从. to , , in this case, how about changing the locale of Spreadsheet?, , 在这种情况下,如何更改电子表格的区域设置?
    • At pnuts's answer, Canada (French) is proposed as the locale.在 pnuts 的回答中,建议将加拿大(法语)作为语言环境。 Ref参考

Modified script:修改后的脚本:

When your script is modified, it becomes as follows.当你的脚本被修改时,它变成如下。

From:从:

const sh = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();

To:到:

const ss = SpreadsheetApp.getActiveSpreadsheet();
const sh = ss.getActiveSheet();

And

From:从:

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');
    }
} 

To:到:

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");
  • From your script, it supposes that the number format of the column "C" is modified.从您的脚本中,它假设“C”列的数字格式已修改。

References:参考:

If I misunderstood your question and this was not the direction you want, I apologize.如果我误解了您的问题并且这不是您想要的方向,我深表歉意。

Added 1:添加1:

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?如果你想解决这个问题,如何修改如下?

From:从:

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

To:到:

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

Added 2:添加2:

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

Sample script:示例脚本:

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.

相关问题 每次运行时如何从Google表格中提取不同的值 - How to pull a different value from google sheets each time it runs 如何在 Google 表格上将值从一列复制到另一列? - How to copy value from one column to another on Google Sheets? 如何根据同一工作表中列的特定值在 Google 表格的另一列中设置值 - How to set value in another column of Google Sheets based on certain value of column in the same sheets Google Sheets API:从公共工作表中读取单个单元格值 - Google Sheets API: Read single cell value from public sheet 如何从Google表格中的单元格中获取价值,以用作Google Script中的数字变量 - How to take value from cell in Google sheets, to be used as number variable in Google Script 如何从谷歌表格中的多个条件搜索中获取价值 - How to get value from multiple criteria search in google sheets 用谷歌表格单元格中的值替换谷歌文档上的值 - Replace a value on google docs with a value from a google sheets cell 在谷歌表中,如何将 A 列中的 #N/A 替换为 B 列中的相应值 - In google sheets, how to replace #N/A in column A with corresponding value in column B 如何在 javascript 中读取单个谷歌表格单元格值 - How to read a single google sheets cell value in javascript 将值从字符串设置为数字 - Set value from string to number
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM