2

I am new to google script. I have a scenario like the below.

I have Sheet1 with Column A and B. Usually I try to store decimal values in to it. I mean integer part in column-A and decimal part in Column-B

For example:

Scenario-1: It the value is 23.75 then Column-A should be 23 and ColumnB would be 75

Scenario-2: If the value is 22.00 then Column-A should be 22 and ColumnB would be 00

I tried like this

 sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1');
        column = sheet.getRange("B:B");
        column.setNumberFormat("@");
        sRange = "O1:O" + 65;
        cell = sheet.getRange(sRange);
        cell.setValue("00"); 

This works fine for scenario-1, but for scenario-2 it displays only one zero (leading zero is automatically removed). I tried to change the complete column formatting to plain text but no luck. I am strugging to achieve this for the past two days. Any help is greatly appreciated.

StackUser
  • 5,370
  • 2
  • 24
  • 44
  • 1
    Try setting the number format on the columns to a custom number format of `00`. Usually the format is `#` or `#,###.00` or similar. The `#` represents an optional digit. The `0` represents a required digit. [Documentation](https://support.google.com/docs/answer/56470?co=GENIE.Platform%3DDesktop&hl=en). – CalamitousCode Jan 30 '19 at 11:05
  • Also, you should be declaring your variables with `var` so you don't create unintended global variables. – CalamitousCode Jan 30 '19 at 11:11

1 Answers1

2

Adding an apostrophe:

cell.setValue("'00");

might be considered adequate.

pnuts
  • 58,317
  • 11
  • 87
  • 139