XL.Book.AddFormat
-----------------

Adds a new format to the workbook, initial parameters can be copied from other format.

| Component | Version | macOS | Windows | Linux | Server | iOS SDK |
|---|---|---|---|---|---|---|
| [XL](component_XL.md) | [3.5](newinversion35.md) | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes |

MBS( "XL.Book.AddFormat"; BookRef { ; initFormat } ) 

**MBS**( **"XL.Book.AddFormat";** /\* Adds a new format to the workbook, initial parameters can be copied from other format. \*/  
**$BookRef**; /\* The reference number for the workbook.e.g. $ref \*/   
**$initFormat**) /\* Optional; The index of an existing format which you want to use as a template.e.g. 0 \*/ 

### Parameters

| Parameter | Description | Example | Flags |
|---|---|---|---|
| BookRef | The reference number for the workbook. | $ref |  |
| initFormat | The index of an existing format which you want to use as a template. | 0 | Optional |

### Result

Returns index of new format or error.

### Description

Adds a new format to the workbook, initial parameters can be copied from other format.   
### Examples

Writes sheet with red font:

 #create new xls file   
Set Variable \[$book ; Value:MBS ( "[XL.NewBook](XLNewBook.md)"; 0 )\]  
\#create fonts   
Set Variable \[$redColor ; Value:MBS ( "[XL.Color.Pack](XLColorPack.md)"; $book ; 255; 0; 0)\]  
Set Variable \[$redFont ; Value:MBS ( "[XL.Book.AddFont](XLBookAddFont.md)"; $book )\]  
Set Variable \[$r ; Value:MBS ( "[XL.Font.SetColor](XLFontSetColor.md)"; $book ; $redFont ; $redColor )\]  
\#   
\#create formats   
Set Variable \[$redFormat ; Value:MBS ( "XL.Book.AddFormat"; $book )\]  
Set Variable \[$r ; Value:MBS ( "[XL.Format.SetFont](XLFormatSetFont.md)"; $book ; $redFormat ; $redFont )\]  
\#   
\#create sheet   
Set Variable \[$sheet ; Value:MBS ( "[XL.Book.AddSheet](XLBookAddSheet.md)"; $book ; "Sales Receipt")\]  
\#   
\#add number in red   
Set Variable \[$r ; Value:MBS ( "[XL.Sheet.CellWriteNumber](XLSheetCellWriteNumber.md)"; $book ; $sheet ; 5; 3; 123; $redFormat )\]  
\#   
Set Field \[Writing data::Output ; MBS ("[XL.Book.Save](XLBookSave.md)"; $book ; "test.xls")\]  
Set Variable \[$r ; Value:MBS ("[XL.Book.Release](XLBookRelease.md)"; $book )\]  
Use a number format:

 # create new format   
Set Variable \[$format ; Value:MBS ( "XL.Book.AddFormat"; $book )\]  
\# use existing number format   
Set Variable \[$r ; Value:MBS ( "[XL.Format.SetNumFormat](XLFormatSetNumFormat.md)"; $book ; $format ; 7)\]  
\# and use format   
Set Variable \[$r ; Value:MBS ( "[XL.Sheet.CellWriteNumber](XLSheetCellWriteNumber.md)"; $book ; $sheet ; 5; 3; 123; $format )\]  
Apply a new format to a column:

 #create new format   
Set Variable \[$format ; Value:MBS ( "XL.Book.AddFormat"; $BookRef )\]  
Set Variable \[$r ; Value:MBS ( "[XL.Format.SetNumFormat](XLFormatSetNumFormat.md)"; $BookRef ; $format ; 7)\]  
\#set as format for column   
Set Variable \[$r ; Value:MBS ( "[XL.Sheet.SetColumn](XLSheetSetColumn.md)"; $BookRef ; 0; 6; 6; 50 ; $format ; 0 )\]  
\#loop over all rows   
Set Variable \[$lastRow ; Value:MBS ( "[XL.Sheet.GetLastRow](XLSheetGetLastRow.md)"; $BookRef ; $sheet )\]  
Set Variable \[$row ; Value:1\]  
Loop  
 Set Variable \[$r ; Value:MBS ( "[XL.Sheet.SetCellFormat](XLSheetSetCellFormat.md)"; $BookRef ; $sheet ; $row ; 6; $format )\]  
 #next  
 Set Variable \[$row ; Value:$row + 1\]  
 Exit Loop If \[$row &gt; $lastRow \]  
End Loop  
Create header row:

 #   
\# define a font:   
\#   
Set Variable \[ $headerFont ; Value: MBS ( "[XL.Book.AddFont](XLBookAddFont.md)"; $book ) \]   
Set Variable \[ $r ; Value: MBS ( "[XL.Font.SetSize](XLFontSetSize.md)"; $book ; $headerFont ; 12) \]   
Set Variable \[ $r ; Value: MBS ( "[XL.Font.SetName](XLFontSetName.md)"; $book ; $headerFont ; "Century Gothic") \]   
Set Variable \[ $r ; Value: MBS ( "[XL.Font.SetBold](XLFontSetBold.md)"; $book ; $headerFont ; 1) \]   
\#   
\# define a format:   
\#   
Set Variable \[ $headerFormat ; Value: MBS ( "XL.Book.AddFormat"; $book ) \]   
Set Variable \[ $r ; Value: MBS ( "[XL.Format.SetFont](XLFormatSetFont.md)"; $book ; $headerFormat ; $headerFont ) \]   
Set Variable \[ $r ; Value: MBS ( "[XL.Format.SetFillPattern](XLFormatSetFillPattern.md)"; $book ; $headerFormat ; 1) \]   
Set Variable \[ $r ; Value: MBS ( "[XL.Format.SetPatternForegroundColor](XLFormatSetPatternForegroundColor.md)"; $book ; $headerFormat ; MBS ( "[XL.Color.Pack](XLColorPack.md)"; $book ; 200; 200; 200) ) \]   
Set Variable \[ $r ; Value: MBS ( "[XL.Format.SetAlignH](XLFormatSetAlignH.md)"; $book ; $headerFormat ; 2 /\* center \*/ ) \]   
\#   
Set Variable \[ $r ; Value: MBS ( "[XL.Sheet.SetRow](XLSheetSetRow.md)"; $book ; $sheet ; $row ; 20 ) \]   
Set Variable \[ $r ; Value: MBS ( "[XL.Sheet.CellWriteText](XLSheetCellWriteText.md)"; $book ; $sheet ; $row ; 0; "Account" ; $headerFormat ) \]   
Set Variable \[ $r ; Value: MBS ( "[XL.Sheet.CellWriteText](XLSheetCellWriteText.md)"; $book ; $sheet ; $row ; 1; "Account Group" ; $headerFormat ) \]   
Set Variable \[ $r ; Value: MBS ( "[XL.Sheet.CellWriteText](XLSheetCellWriteText.md)"; $book ; $sheet ; $row ; 2; "Report Name" ; $headerFormat ) \]   
Set Variable \[ $r ; Value: MBS ( "[XL.Sheet.CellWriteText](XLSheetCellWriteText.md)"; $book ; $sheet ; $row ; 3; "Date TS" ; $headerFormat ) \]   
Set Variable \[ $r ; Value: MBS ( "[XL.Sheet.CellWriteText](XLSheetCellWriteText.md)"; $book ; $sheet ; $row ; 4; "Text" ; $headerFormat ) \]   
Set Variable \[ $r ; Value: MBS ( "[XL.Sheet.CellWriteText](XLSheetCellWriteText.md)"; $book ; $sheet ; $row ; 5; "Text2" ; $headerFormat ) \]   
Set Variable \[ $r ; Value: MBS ( "[XL.Sheet.CellWriteText](XLSheetCellWriteText.md)"; $book ; $sheet ; $row ; 6; "Text3" ; $headerFormat ) \]  
### See also

- [XL.Book.AddFont](XLBookAddFont.md)
- [XL.Format.SetWrap](XLFormatSetWrap.md)
- [XL.Sheet.CellWriteFormula](XLSheetCellWriteFormula.md)
- [XL.Sheet.CellWriteNumber](XLSheetCellWriteNumber.md)
- [XL.Sheet.CellWriteText](XLSheetCellWriteText.md)
- [XL.Sheet.CellWriteTexts](XLSheetCellWriteTexts.md)
- [XL.Sheet.GetLastRow](XLSheetGetLastRow.md)
- [XL.Sheet.SetCellFormats](XLSheetSetCellFormats.md)
- [XL.Sheet.SetColumn](XLSheetSetColumn.md)
- [XL.Sheet.SetRow](XLSheetSetRow.md)

### Release notes

- **Version 11.2**
    - Added [XL.Book.AddFontFromBook](https://www.mbsplugins.eu/XLBookAddFontFromBook.shtml) and [XL.Book.AddFormatFromBook](https://www.mbsplugins.eu/XLBookAddFormatFromBook.shtml) functions.

### Example Databases

- [XL/Writing data](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/XL/Writing%20data.shtml#5ScriptAnchor_)
- [XL/XL Import and Export](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/XL/XL%20Import%20and%20Export.shtml#4ScriptAnchor_)

### Blog Entries

- [MBS Plugin Advent calendar: 13 - XL](https://www.mbsplugins.de/archive/2023-12-13/MBS_Plugin_Advent_calendar_13_/monkeybreadsoftware_blog_filemaker)

### FileMaker Magazin

- [Ausgabe 5/2022, Seite 22](https://filemaker-magazin.de/neuigkeit/4196-Appetithappen-FMM_202205)

This function checks for a license.

Created 18th August 2014 , last changed 7th February 2020

  
[XL.Book.AddFontFromBook](XLBookAddFontFromBook.md) - [XL.Book.AddFormatFromBook](XLBookAddFormatFromBook.md)

[HTML Version](XLBookAddFormat.shtml)