Rich Text
Rich text is text with multiply style formatting. The rule of thumb is to break the string into parts and put a style object before the part that you want to format. String parts that don’t have a style are given a default styles.
For example to get 'This is text with bold part':
// unformatted string
'This is text with bold part'
// break it into parts
'This is text with ', 'bold', ' part'
// add styles before the parts that should be formatted
'This is text with ', styles, 'bold', ' part'
Excel does not allow the use of two consecutive formats in a rich text or an empty string part.
Styling Limits
In Excel, only the font properties of the styles are applied to the string parts in a rich text. Other features such as border, background, text wrap and alignment must be applied to the cell. You can add last argument
with styles for cell
Text limits
Excel has built-in limits for cell's text value. Check Excel Limits for more information about built-in limits
Example
package code
import (
"github.com/plandem/xlsx"
"github.com/plandem/xlsx/format/styles"
"github.com/plandem/xlsx/types/options/column"
)
func Example_richText() {
xl := xlsx.New()
sheet := xl.AddSheet("")
sheet.Col(1).SetOptions(
options.New(
options.Width(48),
),
)
//Text with bold and italic parts
sheet.CellByRef("B2").SetText(
"Text with ",
styles.New(
styles.Font.Bold,
),
"bold",
" and ",
styles.New(
styles.Font.Italic,
),
"italic",
" parts",
)
//Text with red and green parts
sheet.CellByRef("B4").SetText(
"Text with ",
styles.New(
styles.Font.Color("#ff0000"),
),
"red", " and ",
styles.New(
styles.Font.Color("#00ff00"),
),
"green",
" parts",
)
//Centered text with underlined part
sheet.CellByRef("B6").SetText(
"Centered text with ",
styles.New(
styles.Font.Underline(styles.UnderlineTypeSingle),
),
"underlined",
" part",
styles.New(
styles.Alignment.HAlign(styles.HAlignCenter),
),
)
//E=mc2
sheet.CellByRef("B8").SetText(
"E=mc",
styles.New(
styles.Font.Superscript,
),
"2",
styles.New(
styles.Alignment.HAlign(styles.HAlignRight),
),
)
xl.SaveAs("./foo.xlsx")
}