Integers and float point numbers in Apps Script and Google Sheets

1,211 views
Skip to first unread message

Miguel Freitas

unread,
Apr 15, 2024, 3:19:13 AM4/15/24
to Google Apps Script Community
I am using a script to get data from a google sheet and send it as an email formatted as HTML template. Everything is working fine except that some data values in the google sheet are being rounded but when I send them to the html template they show up as float point type loosing their round. 

I have tried the following line:

var testResult = Math.round(rowData[3]).toString(0);

I can see from the log message that it converts the value to 19

Screenshot 2024-04-12 175403.png
but then on the html template it displays as floating point

 
Screenshot 2024-04-12 174454.png
Can someone kindly help me on this one?

Thanks in advance.


Fabrice Faucheux

unread,
Apr 15, 2024, 1:59:32 PM4/15/24
to Google Apps Script Community
Hi

It sounds like you're encountering an issue where the rounding appears correct when logged, but the values lose their formatting when passed into the HTML template. Here are a few potential causes and solutions for this issue:

1. Ensure Consistent Formatting: Even though you round the number using `Math.round()`, it's a good practice to ensure the formatted value is consistently used in your script. Check that the `testResult` variable is the one being passed into the HTML template and not `rowData[3]` directly.

2. Check Data Type Conversion: Since you are using `.toString(0)`, it should normally just convert the rounded number to a string without any decimals. If it’s still showing decimals, it might be that somewhere else in your script the rounding isn't applied, or the wrong variable is being sent to the template.

3. Template Rendering Issues: Sometimes, the way variables are injected into the HTML template might cause them to revert to their original format or undergo unexpected conversions. Make sure that the data binding or interpolation method used in the template accurately reflects the formatted string.

4. Force Formatting in HTML: If you want to ensure no mistakes in the HTML rendering, you can also handle the rounding directly in the HTML template, if your template engine supports it. This is less clean as logic should ideally be separated from presentation, but it is a fallback.

Here is a refined approach to make sure your number is rounded and converted to a string correctly:

var roundedValue = Math.round(rowData[3]);
var testResult = roundedValue.toString();

And ensure that `testResult` is the variable being interpolated into your HTML template.

If you continue to face issues, please provide more details about how you are passing data into the HTML template  so I can give more targeted advice.

George Ghanem

unread,
Apr 15, 2024, 6:24:30 PM4/15/24
to google-apps-sc...@googlegroups.com
An easy way to solve it is to use the method getDisplayValues() rather than using getValues(). This way it takes it exactly as you have it formatted in your spreadsheet.


--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/1a606e35-65d9-48a1-95bd-2295d6e9a0ddn%40googlegroups.com.

Miguel Freitas

unread,
Apr 18, 2024, 10:54:19 AM4/18/24
to google-apps-sc...@googlegroups.com
Hi Fabrice.

Thank you very much for your email. It has a lot of important information. I've managed to fix the issue. I was not passing the variable to the template after the math round function.

Anyway your feedback is valuable and I will keep it for future reference.

Many thanks in advance

--
You received this message because you are subscribed to a topic in the Google Groups "Google Apps Script Community" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-apps-script-community/gJrxOJ-BxOc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/62cf0bb2-19a3-4039-878f-6510ec620bd7n%40googlegroups.com.


--

Miguel Freitas

Assistente Administrativo

mig...@linguaupgrade.com

234 037 975 (Rede fixa nacional)

911 130 498 (Rede móvel nacional)

Rua Domingos Carrancho, n.º1, 2.º Dto.
3800 145 Aveiro PORTUGAL

linguaupgrade.com


The content of this email is confidential and intended for the recipient specified in message only. It is strictly forbidden to share any part of this message with any third party, without a written consent of the sender. If you received this message by mistake, please reply to this message and follow with its deletion, so that we can ensure such a mistake does not occur in the future.

Miguel Freitas

unread,
Apr 18, 2024, 10:58:19 AM4/18/24
to google-apps-sc...@googlegroups.com
Hi George,

Thank you for your reply.

That's definitely a good idea. I'm new to apps script so I'm still looking at how to do things. I managed to fix the problem. I was not passing the right variable to the template. Now it's working fine.

I will keep this in mind for the future.

All the best,
Miguel Freitas



You received this message because you are subscribed to a topic in the Google Groups "Google Apps Script Community" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-apps-script-community/gJrxOJ-BxOc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/CAA4K68a0vHKv8rKRr%3Dj1dREGK6u0hO5R2HMfbhupCsuWyi_c1Q%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages