Styles Formatting
Styles can be defined through special type - general type for all available styles. But information that will be using to style object, depends on usage, e.g. to style cells everything will be use, to style rich texts - only font information.
// create a new styles
ss := styles.New(
styles.Font.Bold,
styles.Font.Color("#ff0000"),
)
//update styles
ss.Set(
styles.Border.Color("#009000"),
styles.Border.Type(styles.BorderStyleMedium),
)
Modify Styles
While you can modify created styles, you should keep in mind, that modifying will work only before applying styles to cell and any modifications after applying, will create new styles.
ss := styles.New(
styles.Font.Bold,
))
//font will be `bold`
sheet.CellByRef("A1").SetStyles(ss)
//modify styles
ss.Set(
styles.Font.Color("#ff0000"),
)
//`A2` - will be `bold and red`
//`A1` - will be only `bold` and without color
sheet.CellByRef("A2").SetStyles(ss)
Font
Excel can only display installed fonts, that's why using standard fonts(e.g.: Calibri
, Times New Roman
or Courier New
) is highly recommended.
The default font for cell is Calibri
(Excel 2007+)
package code
import (
"github.com/plandem/xlsx/format/styles"
)
func Example_stylesFont() {
//all possible settings for font
styles.New(
styles.Font.Name("Courier New"),
styles.Font.Bold,
styles.Font.Italic,
styles.Font.Strikeout,
styles.Font.Superscript,
styles.Font.Subscript,
styles.Font.Shadow,
styles.Font.Condense,
styles.Font.Extend,
styles.Font.Family(styles.FontFamilyRoman),
styles.Font.Color("#FF0000"),
styles.Font.Size(16),
styles.Font.Underline(styles.UnderlineTypeSingle),
styles.Font.Scheme(styles.FontSchemeMinor),
styles.Font.Charset(styles.FontCharsetMAC),
)
}
Predefined values
Xlsx2Go defined all built-in values to use for styling. For more information, check API documentation