简体   繁体   中英

setnumberformat() in Google Apps Script on multiple conditions (e.g. x<a, x>b) based on guide tokens

I've always had a hard time understanding properly number format tokens and this occasion seems no exception.

A small condition that I place at the bottom of my scripts lets me differentiate how varying ranges of numbers appear on the sheet:

    if (value < 1)               {var a = a.toFixed(8);} 
    if (value > 1 && value < 10) {var a = a.toFixed(4);}
    if (value >=10)              {var a = a.toFixed(2);}

I would like to transpose this pattern to the setNumberFormat() method however for the sake of simplification and hopefully speeding up the scripts.

Now, this guide suggests that specific conditions can be set in order to provide a broader range of possibilities; that's my case I thought, the problem is that I simply can't make the script work:

this one works

  var range = sheet_Numbers.getRange(['C:E']);

  range.setNumberFormat("[<1]###0.########;[>10]####.##;##.####");

图片1 but doesn't produce the expected result: I meant numbers between 1 and 10 displaying 4 digits after the decimal point, not 3 .

All my other attempts at applying the guides directions return error instead.
Following the guide I thought I could provide something like this, but that doesn't seem to be the case however:

  var range = sheet_Numbers.getRange(['C:E']);

  range.setNumberFormat("[[<1]####.########;[>10]####.##;####.####];[NEGATIVE FORMAT];[ZERO FORMAT]");

Any suggestion?

If you want 5.655 to be rendered as 5.6550(4 digits after the decimal regardless of significance), use 0 instead of #

"[<1]###0.########;[>10]####.##;##.0000"

or more generally

"[<1]###0.00000000;[>10]####.00;##.0000"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM