Google Sheet Script Editor - how would one format their numbers properly?

﹥>﹥吖頭↗ 提交于 2021-01-29 06:51:19

问题


So I`m trying to format my number to include percentage sign using script editor

That "valuetoCheck" is the target i`m trying to change

I am sending these emails to my colleagues when error rate is greater than 10 %

That 'J1' is basically extracted from the sheet by using formula (=max(C:C)) and they are already in percent format in actual google sheet itself

How should I format that J1 so that it can be shown with proper percent sign in the email?

The current code generates the below message in the email..

Reported Maximum default rate is: 0.020380774742397016

and the number is supposed to be 2% if it was done properly

function checkValue()

{

var ss = SpreadsheetApp.getActive();
var sheet = ss.getSheetByName('Final_Report');
var url = ss.getUrl();
var valueToCheck = sheet.getRange('J1').getValue();
var emailSendTo = 'random@hotmail.com'
var subject = "Errpr - Detected!";
var message = "Please check Error rates \n\n Reported Maximum Error rate is: " + valueToCheck 

if(valueToCheck > 0.1)

  {

MailApp.sendEmail(emailSendTo, subject, message);
}
}

回答1:


Regardless of how it is formatted, the "value" will still be '0.020380774742397016'.

An option is to round the value, then multiply by 100 to allow you to display it as a percentage. Something like:

var valueToReport = (+valueToCheck.toFixed(4))*100;
var message = "Please check Error rates \n\n Reported Maximum Error rate is: " +valueToReport+"%";

This will display the value as "2.04%"



来源:https://stackoverflow.com/questions/54930909/google-sheet-script-editor-how-would-one-format-their-numbers-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!