Apps 脚本和 setNumberFormat
Apps script and setNumberFormat
我有一个 Google 电子表格脚本,可以通过 API 提取一些数据。问题是我正在提取一些数据 returns 像这样的数字:142499.68000000
问题是我的电子表格配置为巴西格式,我得到了一些疯狂的结果 (14.249.968.000.000,00
)。我想像这样格式化这个数字:142.499,68
(我们使用逗号而不是美国格式的点)。
我一直在尝试 .setNumberFormat()
但没有成功。
如何在我的脚本中格式化数字?
您可以直接使用 JavaScript,特别是 Intl.NumberFormat:
function myFunction() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheetByName('Sheet1');
const cell = sheet.getRange("A1");
const number = new Intl.NumberFormat('de-DE').format(142499.68000000);
cell.setValue(number);
}
当我将电子表格区域设置更改为巴西时,它对我有用。
确保单元格上没有可能影响脚本返回格式的其他数字格式。
de-DE
版本returns想要的结果:
我有一个 Google 电子表格脚本,可以通过 API 提取一些数据。问题是我正在提取一些数据 returns 像这样的数字:142499.68000000
问题是我的电子表格配置为巴西格式,我得到了一些疯狂的结果 (14.249.968.000.000,00
)。我想像这样格式化这个数字:142.499,68
(我们使用逗号而不是美国格式的点)。
我一直在尝试 .setNumberFormat()
但没有成功。
如何在我的脚本中格式化数字?
您可以直接使用 JavaScript,特别是 Intl.NumberFormat:
function myFunction() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheetByName('Sheet1');
const cell = sheet.getRange("A1");
const number = new Intl.NumberFormat('de-DE').format(142499.68000000);
cell.setValue(number);
}
当我将电子表格区域设置更改为巴西时,它对我有用。
确保单元格上没有可能影响脚本返回格式的其他数字格式。
de-DE
版本returns想要的结果: