问题
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