Google表格脚本编辑器-一种如何正确格式化其数字? [英] Google Sheet Script Editor - how would one format their numbers properly?

查看:53
本文介绍了Google表格脚本编辑器-一种如何正确格式化其数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想使用脚本编辑器来格式化我的数字以包括百分号

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

"valuetoCheck"是我要更改的目标

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

当错误率大于10%时,我会将这些电子邮件发送给我的同事

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

"J1"基本上是通过使用公式(= max(C:C))从工作表中提取的,并且它们在实际的Google工作表本身中已经采用百分比格式

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

我应该如何格式化J1以便可以在电子邮件中以正确的百分号显示它?

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..

报告的最大默认汇率为:0.020380774742397016

Reported Maximum default rate is: 0.020380774742397016

如果正确处理,这个数字应该是2%

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

推荐答案

无论其格式如何,值"仍将为'0.020380774742397016'.

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

一个选项是将值四舍五入,然后乘以100以允许您将其显示为百分比.像这样:

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+"%";

这将显示为"2.04%"

This will display the value as "2.04%"

这篇关于Google表格脚本编辑器-一种如何正确格式化其数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆