Components All New MacOS Windows Linux iOS
Examples Mac & Win Server Client Guides Statistic FMM Blog Deprecated Old

XL.Book.AddCustomNumFormat

Adds a new custom number format to the workbook.

Component Version macOS Windows Linux Server iOS SDK
XL 3.5 ✅ Yes ✅ Yes ✅ Yes ✅ Yes ✅ Yes
MBS( "XL.Book.AddCustomNumFormat"; bookRef; format )   More

Parameters

Parameter Description Example
bookRef The reference to the workbook. Please use XL.LoadBook to load a file. $ref
format The format string. "#.000"

Result

Returns number or error.

Description

Adds a new custom number format to the workbook.
The format string customNumFormat indicates how to format and render the numeric value of a cell.
Returns the custom format identifier. It's used in XL.Format.SetNumFormat.
Read website for details:
http://www.libxl.com/custom-format.html

Examples

Create Excel file with some date value:

#create new xls file
Set Variable [$book; Value:MBS( "XL.NewBook"; 0 )]
#create date format
Set Variable [$dateFormat; Value:MBS( "XL.Book.AddFormat"; $book)]
Set Variable [$dateFormatID; Value:MBS( "XL.Book.AddCustomNumFormat"; $book; "mmmm\ d\,\ yyyy")]
Set Variable [$r; Value:MBS( "XL.Format.SetNumFormat"; $book; $dateFormat; $dateFormatID)]
#create sheet
Set Variable [$sheet; Value:MBS( "XL.Book.AddSheet"; $book; "Sales Receipt")]
#Write date
Set Variable [$d; Value:MBS( "XL.Date.Pack"; $book; 2013; 10; 21)]
Set Variable [$r; Value:MBS( "XL.Sheet.CellWriteNumber"; $book; $sheet; 4; 4; $d; $dateFormat)]
#
Set Field [Writing data::Output; MBS("XL.Book.Save"; $book; "test.xls")]
Set Variable [$r; Value:MBS("XL.Book.Release"; $book)]

Add american phone number formatting:

Set Variable [$f; Value:MBS( "XL.Book.AddCustomNumFormat"; $book; "[<=9999999]###\\-####;\\(###\\)\\ ###\\-####")]

Add dollar amount formatting:

Set Variable [$f; Value:MBS( "XL.Book.AddCustomNumFormat"; $book; "_($ # ##0.00_);_($ (# ##0.00);_($* -??_);_(@_)")]

Add a custom format with condition:

# define custom number format
#
Set Variable [ $customFormat ; Value: MBS( "XL.Book.AddCustomNumFormat"; $book; "[Red]#[<=100];[Blue]#[>100]") ]
#
# make format
#
Set Variable [ $textFormat ; Value: MBS( "XL.Book.AddFormat"; $book) ]
Set Variable [ $r ; Value: MBS( "XL.Format.SetNumFormat"; $book; $textFormat; $customFormat) ]
#
# add sheet and use format:
#
Set Variable [ $sheet ; Value: MBS( "XL.Book.AddSheet"; $book; "") ]
Set Variable [ $r ; Value: MBS( "XL.Sheet.CellWriteNumber"; $book; $sheet; 8; 1; 90; $textFormat) ]
Set Variable [ $r ; Value: MBS( "XL.Sheet.CellWriteNumber"; $book; $sheet; 8; 1; 110; $textFormat) ]

Format dollar with CAD before:

MBS( "XL.Book.AddCustomNumFormat"; $book; "\"CAD \"$#,##0.00;\"CAD -$\"#,##0.00;\"CAD $\"0.00;@")

See also

Example Databases

This function checks for a license.

Created 18th August 2014, last changed 30th March 2024


XL.Book.AddConditionalFormat - XL.Book.AddFont