1. Formatting numbers, dates, times and timestamps

There are two ways to describe the formats for the cells in your spreadsheet. Either you can use the tag "NumFormat" in the style options and specify a format string, or you can use one of the internal format numbers defined in Excel. You will find the complete list below. To use those, specify the tag "NumFormaCode" instead.

  1. Formatting numbers, dates, times and timestamps
    1. Excel Internal format numbers
      1. Number Formats
      2. Currency Formats
      3. Scientific Formats
      4. Engineering Formats
      5. Date Formats
      6. Time Formats
      7. Timestamp Formats
  2. Formatting using format strings
    1. Understanding Number Format Strings
      1. Common Format String Symbols
      2. Date and Time Format Strings
      3. Colour Codes
      4. Conditional Formatting within Number Formats
  3. How did we create the internal numbers list?

1.1. Excel Internal format numbers

I haven't found any official reference to the tables below, but I found out by experimenting. Look at the chapter near the bottom for a description of how I found out. For using those numbers below, use them in defining styles for your sheet:

Excel_AddStyle ( wb, "USD", JSON ("NumFormatCode", 7)); 

Wherever you add numbers using the Excel_SetCell function, it will be displayed with a currency symbol (according to regional settings), numbers with two decimals and 1000-group delimiters. The examples below are for Norway.

1.1.1. Number Formats

There are many duplicates in this table, but if you are parsing an Excel sheet, you find the complete table below. To use them, just pick one that fits.

NumFormatCode Rounding/Format 1000-sep Example
0 default no 1.5
1 no decimals no 2
2 two decimals no 1.50
3 no decimals yes 1,500
4 two decimals yes 1,500.00
23 default no 1.5
24 default no 1.5
25 default no 1.5
26 default no 1.5
37 no decimals - negative values in parenthesis yes 45,655
38 no decimals - negative values in parenthesis and red text yes 45,655
39 two decimals - negative values in parenthesis yes 1,500.00
40 two decimals - negative values in parenthesis and red text yes 1,500.00
41 no decimals - negative values in parenthesis yes 45,655
43 two decimals - negative values in parenthesis yes 1,500.00
49 left justified No 1.5
59 no decimals no 45,655
60 two decimals no 45,655.00
61 no decimals yes 45,655
62 two decimals yes 45,655.00
69 mixed fractions no 1 1/2
70 mixed fractions no 2 3/4
12 mixed fractions no 1 1/2
13 mixed fractions no 3 1/8

1.1.2. Currency Formats

Numbers with currency symbols according to regional settings. Those below are for Norway.

NumFormatCode Rounding/Format 1000-sep Example
5 no decimals - negative values in parenthesis yes kr 45,655
6 no decimals - negative values red, and in parenthesis yes kr 45,655
7 two-decimals - negative values in parenthesis yes kr 45,655.00
8 two-decimals - negative values red, and in parenthesis yes kr 45,655.00
42 no decimals - negative values in parenthesis yes kr 45,655
44 two decimals - negative values in parenthesis yes kr 45,655
63 two-decimals - negative values in parenthesis yes kr 45,655.00
64 two-decimals - negative values in parenthesis and red color yes kr 45,655.00
65 two-decimals - negative values in parenthesis yes kr 45,655.00
66 two-decimals - negative values in parenthesis and red color yes kr 45,655.00

1.1.3. Scientific Formats

The scientific format uses one digit before the comma and Exponential value.

NumFormatCode Rounding/Format 1000-sep Example
11 two dec no 4.57E+4

1.1.4. Engineering Formats

Engineering format uses exponent that are modules of 3.

NumFormatCode Rounding/Format 1000-sep Example
48 up to 2 decimals No 135.7E+3

1.1.5. Date Formats

NumFormatCode(s) Date Rounding/Format
14 29.12.2024 default date
15 29.des.24 full date named month
16 29.des day+named month, no year
17 des.24 named month+year
27-31 29.12.2024 default date
36 29.12.2024 default date
50-58 29.12.2024 default date
71, 72 29.12.2024 default date
73 29.des.24 default date
74 29.des day named month. no year
75 des.24 no day, named month and year.

1.1.6. Time Formats

NumFormatCode Time Rounding/Format
18 12:00 pm hh:mm am/pm
19 12:00:00 pm hh:mm:ss am/pm
20 12:00 hh:mm
21 12:00:00 hh:mm:ss
45 00:00 mm:ss
46 36:00:00 hh:mm:ss (hours > 24)
47 10:03,4 mm:ss, 1/10
76 12:00 hh:mm
77 12:00:00 hh:mm:ss
79 09:33 mm:ss
80 36:00:00 hh:mm:ss (hours > 24)
81 10:03,2 mm:ss, 1/10

1.1.7. Timestamp Formats

NumFormatCode Timestamp Rounding/Format
22 29.12.2024 12:00
78 29.12.2024 12:00

2. Formatting using format strings

The other way to format cells is to use format strings instead.

Excel_AddStyle ( wb, "USD", JSON ("NumFormat", "USD ### ### ##0.00")); 

This will be a currency format prefixed with USD and a number with two decimals.

2.1. Understanding Number Format Strings

Excel's number format strings are structured to define distinct display formats for different types of data within a single cell. These strings can be divided into up to four sections, separated by semicolons (;), each specifying the format for:

  1. Positive Numbers
  2. Negative Numbers
  3. Zero Values
  4. Text Strings

For example, the format string 0.00;[Red]-0.00;0.00;"Text:"@ applies:

If only one section is provided, it applies to all number types. Two sections mean the first applies to positive numbers and zeros, and the second to negative numbers. Three sections allow separate formatting for positive numbers, negative numbers, and zeros, respectively.

2.1.1. Common Format String Symbols

2.1.2. Date and Time Format Strings

Excel uses specific codes to represent dates and times:

Note: Excel interprets it as minutes rather than months when using m immediately after h or hh, or immediately before ss.

2.1.3. Colour Codes

You can apply colours to different sections by enclosing the colour name in square brackets:

For example, the format string [Red]0;[Blue]-0;0 displays positive numbers in red, negative numbers in blue, and zeros in the default colour.

2.1.4. Conditional Formatting within Number Formats

Excel allows conditional formatting directly within number formats by specifying conditions in square brackets.

For instance, the format string [>1000]0.00,"K";0.00 displays numbers greater than 1000 as thousands (e.g., 1500 as 1.50

3. How did we create the internal numbers list?

I have not found any official documentation about those numbers, but tons of pages describe how to format numbers in Excel.

After experimenting with some formatting in Excel, looking at the generated stylesheets, and finding the most important ones, I found another better method.

I generated a spreadsheet with the ACF plugin and defined 161 styles in a loop, all with the NumFormatCode from 0 to 160. Then I populated rows using the newly created style to see how it formats numbers, strings, date and time values. Here is the function I made:

function TestBuiltInStyles ()
    string res; 
    string path = desktop_directory ()+ "TestNumFOrmatCodes.xlsx"; 
    print path + "\n"; 
    if ( file_exists (path)) then
        res = delete_file ( path ); 
    end if
    int wb = Excel_Create ( path, "Codes"); 
    excel_addstyle ( wb, "H1", json("Font", "Arial", "FontSize", 18, "Decoration", "Bold")); 
    excel_addstyle ( wb, "H2", json("Font", "Arial", "FontSize", 14, "Decoration", "Bold", 
        "FillColor", "DDDDDD", "Borders", json ("Bottom", linetype_thin))); 
    
    int s = Excel_GetSheetID ( wb, 1); 
    excel_setCell ( s, 1, 1, "Internal NumFormatCodes in Excel", "H1" ); 
    array string hd = {"NumFormatCode", "Value 1.5", "Value 2", "Text", "Date", "Time", "TimeStamp" }; 
    excel_setColumns ( s, 2, 1, hd, "H2"); 
    long todayInt = date(now())-date("1900-01-01", "%Y-%m-%d")+2;
    float tsValue = 0.5 + todayInt; 
    int i, row; 
    string style; 
    for (i=0, 160)
        style = "Code"+i; 
        excel_addstyle ( wb, style, json ("NumFormatCode", i));
        row = i+3; 
        excel_setcell( s, row, 1, i);
        excel_setcell( s, row, 2, 1.5, style);
        excel_setcell( s, row, 3, 2, style);
        excel_setcell( s, row, 4, "Some Text", style);
        excel_setcell( s, row, 5, todayInt, style);
        excel_setcell( s, row, 6, 0.5, style);
        excel_setcell( s, row, 7, tsValue, style);
    end for
    excel_close ( wb); 
    return "OK"; 
end

Then I got a spreadsheet like this: NumFormatCodeExcel

Then I just had to categorize it for this documentation.

Good luck with your cell formatting.