简体   繁体   English

为什么方法setValue与Browser.msgBox输出的不同?

[英]Why method setValue doesn't output same as Browser.msgBox?

Since I could not make .toFixed(2) to work I designed my own piece of code to add desired decimal digits after the "." 由于无法使.toFixed(2)正常工作,因此我设计了自己的代码,在“。”之后添加了所需的十进制数字。 by simple joining two strings with + sign. 通过简单地连接两个带+号的字符串。

While Browser.msgBox outputs the 2 strings joined correctly as "1.00", it seems like getRange.setValue outputs only the first of the 2 strings as "1" :( 虽然Browser.msgBox将正确连接的2个字符串输出为“ 1.00”,但好像getRange.setValue仅将2个字符串中的第一个输出为“ 1” :(

function myFunction() {
var ss_calc = SpreadsheetApp.openById("1cFt0DbnpWGHquKk4ijxdKhwkaF8GhumWDWjTpHuSXbQ");
var sheet_calc = ss_calc.getSheetByName("Calcs");      
var ss_source = SpreadsheetApp.openById("1gXeXmiw9EnzQXaiE7H8_zrilE2zyotlSuuIS8X9IxfQ");
var sheet_source = ss_source.getSheetByName("Farmah");  

var decDig = ""; var strDec = ""; var impVal = "";
impVal = sheet_source.getRange(12,7).getValue().toString();
if (JSON.stringify(impVal).indexOf(".")>-1) 
 { if (JSON.stringify(impVal).split(".")[1].length < 2 ) 
  { 
   if (JSON.stringify(impVal).split(".")[1].length < 1) 
    { 
     decDig = "00"; 
    }
   else 
    { 
     decDig = "0"; 
    }
   }
  } 
else 
{ 
 decDig = ".00"; 
}
var strDec = impVal.toString() + decDig.toString();
Browser.msgBox(JSON.stringify(impVal).indexOf(".")+ "\\n" +
           impVal.toString()+ "\\n" +
           decDig+ "\\n" +
           strDec);
sheet_calc.getRange(1,1).setValue(strDec);
}  

From sheet_calc.getRange(1,1).setValue(strDec); 来自sheet_calc.getRange(1,1).setValue(strDec); I am expecting to get output "1.00" but I get only "1" :( 我期望得到输出“ 1.00”,但我只会得到“ 1” :(

What am I missing? 我想念什么?

Here are the links to google spreadsheets ( anyone with the link can edit :) 这是google电子表格的链接(知道链接的任何人都可以编辑:)

(above code has to be triggered manually by script editor in the first spreadsheet here under): (以上代码必须由下面的第一个电子表格中的脚本编辑器手动触发):

https://docs.google.com/spreadsheets/d/1cFt0DbnpWGHquKk4ijxdKhwkaF8GhumWDWjTpHuSXbQ/edit?usp=sharing https://docs.google.com/spreadsheets/d/1cFt0DbnpWGHquKk4ijxdKhwkaF8GhumWDWjTpHuSXbQ/edit?usp=sharing

https://docs.google.com/spreadsheets/d/1gXeXmiw9EnzQXaiE7H8_zrilE2zyotlSuuIS8X9IxfQ/edit?usp=sharing https://docs.google.com/spreadsheets/d/1gXeXmiw9EnzQXaiE7H8_zrilE2zyotlSuuIS8X9IxfQ/edit?usp=sharing

  • You want to put the value of 1.00 to a cell "A1". 您想要将值1.00放入单元格“ A1”。

If my understanding is correct, how about this modification? 如果我的理解是正确的,那么该修改如何? I think that the reason of your issue is that the value by putting by setValue() is converted to the number. 我认为问题的原因在于setValue()放置的值被转换为数字。 By this, 1 is shown. 由此,示出了1 In order to put the value as 1.00 , I think that there are 3 patterns. 为了将值设为1.00 ,我认为有3种模式。 Please select one of them for your situation. 请根据您的情况选择其中之一。

Pattern 1: 模式1:

In this pattern, from your question, the value is put as a string using setNumberFormat("@") . 在这种模式下,根据您的问题,使用setNumberFormat("@")将值作为字符串setNumberFormat("@")

From: 从:

sheet_calc.getRange(1,1).setValue(strDec);

To: 至:

sheet_calc.getRange(1,1).setNumberFormat("@").setValue(strDec);

Pattern 2: 模式2:

In this pattern, from your question, the format of cell is set using setNumberFormat("0.00") . 在这种模式下,根据您的问题,使用setNumberFormat("0.00")设置单元格的格式。

From: 从:

sheet_calc.getRange(1,1).setValue(strDec);

To: 至:

sheet_calc.getRange(1,1).setNumberFormat("0.00").setValue(strDec);

Pattern 3: 模式3:

In this pattern, from the script of your shared Spreadsheet, When decDig is ".00" , the format is set. 在这种模式下,从共享电子表格的脚本中,当decDig".00" ,将设置格式。

From: 从:

sheet_calc.getRange(x+6,c).setValue(strDec);

To: 至:

var range = sheet_calc.getRange(x+6,c);
if (decDig) {
  range.setNumberFormat("0.00").setValue(strDec); // or setNumberFormat("@")
} else {
  range.setValue(strDec);
}

Reference: 参考:

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

From sheet_calc.getRange(1,1).setValue(strDec); 来自sheet_calc.getRange(1,1).setValue(strDec); I am expecting to get output "1.00" but I get only "1" :( 我期望得到输出"1.00"但我只会得到"1" :(

Google Sheets, as well as other spreadsheet apps, have an automatic data type assignation, so things that look as numbers are converted to Google Sheets number data type, etc. Google表格以及其他电子表格应用程序具有自动的数据类型分配功能,因此,看起来像数字的东西会转换为Google表格数字数据类型等。

You could prepend an ' to force that a value be treated as text or you could set the number format in such way that numbers are displayed with two decimals. 您可以在前面加上'来强制将值视为文本,也可以设置数字格式,使数字以两位小数显示。 The cell formatting could be applied in advance, ie, by using the Google Sheets UI commands or you could use Apps Script to set the format for you. 可以预先应用单元格格式,即通过使用Google Sheets UI命令,也可以使用Apps脚本为您设置格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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