简体   繁体   English

如何通过 Google Script 从 Google 表格中的单元格将数字格式化为货币?

[英]How to format numbers into currency by Google Script from cells in Google sheets?

I currently have a financial Google sheet I'm pulling data from and throwing (populating) into documents using a script.我目前有一个财务 Google 表格,我正在使用脚本从中提取数据并将其投放(填充)到文档中。

I am having trouble having the numbers come in the proper currency format, which in my case is 34.042,41 €.我无法让数字以正确的货币格式出现,在我的例子中是 34.042,41 €。 (euro, '.' and then ',' for decimals) (欧元,'.' 然后是 ',' 表示小数)

I have this script at the moment:我现在有这个脚本:

body2.replaceText('{{Sum Onsite}}', formatcurrency(row[25]));

Where text is replaced by the input function.文本被输入函数替换的地方。

And at the bottom I created this other function to format the input into the desired output:在底部,我创建了另一个函数来将输入格式化为所需的输出:

function formatcurrency(currency) {
var currency = new Currency();
return Utilities.formatcurrency("€#.##0,00;€(#.##0,00)")
}

The error I get with this is that the 'Currency is not defined' and I'm not sure how to fix it.我得到的错误是“货币未定义”,我不知道如何解决它。

Any help would be appreciated.任何帮助,将不胜感激。 If you need more detail do let me know.如果您需要更多详细信息,请告诉我。

Thank you.谢谢你。

I don't know how your figures look like.我不知道你的数字是怎样的。 Suppose they look like this 123,123.12 and you want they look like this 123.123,12 €假设它们看起来像这个123,123.12而你希望它们看起来像这个123.123,12 €

Here you go:干得好:

 function euro(n) { return n.replace(/\\,/g, '_').replace(/\\./, ',').replace(/_/g, '.') + '€'; } console.log(euro('123,123.12')); // 123.123,12€

I found this works:我发现这有效:

function formatSalary(salary){
  return new Intl.NumberFormat('de-DE',{style:'currency',currency:'EUR'}).format(salary)
  }

and

body2.replaceText('{{Rechnungsbetrag}}', formatSalary(row[28]));

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

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