K Yassin
K Yassin

Reputation: 21

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.

I am having trouble having the numbers come in the proper currency format, which in my case is 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.

Upvotes: 1

Views: 1122

Answers (2)

K Yassin
K Yassin

Reputation: 21

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]));

Upvotes: 1

Yuri Khristich
Yuri Khristich

Reputation: 14537

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 €

Here you go:

function euro(n) {
  return n.replace(/\,/g, '_').replace(/\./, ',').replace(/_/g, '.') + '€';
}

console.log(euro('123,123.12')); // 123.123,12€

Upvotes: 1

Related Questions