在写入电子表格时强制Google Apps脚本中的小数位数

时间:2016-10-26 14:23:37

标签: google-apps-script google-sheets number-formatting

我有一个处理谷歌电子表格的脚本。 如果我有变量" myVariable"它的值为2.10并使用该函数 SpreadsheetApp.getActiveSheet()。getRange(,)。setValue(myVariable)它存储为2.1而不是2.10。 当我再次调用该值时,如果它们为零,则会截断最后一个小数位。由于这些是财政数字,我需要保留两位小数。

有没有办法阻止这种情况?

1 个答案:

答案 0 :(得分:0)

您可以将工作表中的单元格格式设置为格式>数字>自动格式化为格式>数字>数字

但是如果你想使用App脚本本身来实现它,那么只需添加以下行:

var myVariable = 2.10;
SpreadsheetApp.getActiveSheet().getRange(,).setValue(myVariable);
SpreadsheetApp.getActiveSheet().getRange(,).setNumberFormat("0.00"); // Add this line to your code