UmyaSpreadsheet.RichText (umya_spreadsheet_ex v0.7.0)
View SourceRich Text functionality for UmyaSpreadsheet.
This module provides support for formatted text within cells, allowing multiple font styles, colors, and formatting within a single cell.
Examples
# Create a simple rich text
rich_text = UmyaSpreadsheet.RichText.create()
# Add formatted text
UmyaSpreadsheet.RichText.add_formatted_text(rich_text, "Bold text", %{bold: true})
UmyaSpreadsheet.RichText.add_formatted_text(rich_text, " and normal text", %{})
# Set rich text to a cell
UmyaSpreadsheet.RichText.set_cell_rich_text(spreadsheet, "Sheet1", "A1", rich_text)
# Create rich text from HTML
rich_text = UmyaSpreadsheet.RichText.create_from_html("<b>Bold</b> and <i>italic</i> text")
Summary
Functions
Adds formatted text directly to a RichText object.
Adds a TextElement to a RichText object.
Creates a new empty RichText object.
Creates a RichText object from an HTML string.
Creates a TextElement with text and optional font properties.
Gets rich text from a specific cell.
Gets font properties from a TextElement.
Gets text content from a TextElement.
Gets all text elements from a RichText object.
Gets plain text from a RichText object.
Sets rich text to a specific cell.
Converts RichText to HTML representation.
Functions
Adds formatted text directly to a RichText object.
This is a convenience function that creates a TextElement and adds it in one step.
Parameters
rich_text
- RichText resourcetext
- Text content to addfont_props
- Map of font properties (optional)
Examples
iex> rich_text = UmyaSpreadsheet.RichText.create()
iex> UmyaSpreadsheet.RichText.add_formatted_text(rich_text, "Bold text", %{bold: true})
:ok
iex> UmyaSpreadsheet.RichText.add_formatted_text(rich_text, " normal text", %{})
:ok
Adds a TextElement to a RichText object.
Parameters
rich_text
- RichText resourcetext_element
- TextElement resource to add
Examples
iex> rich_text = UmyaSpreadsheet.RichText.create()
iex> element = UmyaSpreadsheet.RichText.create_text_element("Test", %{bold: true})
iex> UmyaSpreadsheet.RichText.add_text_element(rich_text, element)
:ok
Creates a new empty RichText object.
Returns a Rich Text resource that can be used to build formatted text.
Examples
iex> rich_text = UmyaSpreadsheet.RichText.create()
iex> is_reference(rich_text)
true
Creates a RichText object from an HTML string.
Parses HTML to create formatted rich text with font styling.
Parameters
html
- HTML string to parse
Examples
iex> html = "<b>Bold</b> and <i>italic</i> text"
iex> rich_text = UmyaSpreadsheet.RichText.create_from_html(html)
iex> is_reference(rich_text)
true
Creates a TextElement with text and optional font properties.
A TextElement represents a portion of text with consistent formatting.
Parameters
text
- The text contentfont_props
- Map of font properties (optional)
Font Properties
:bold
- Boolean for bold text:italic
- Boolean for italic text:underline
- Boolean for underlined text:strikethrough
- Boolean for strikethrough text:size
- Font size (number):name
- Font name (string):color
- Color (hex string like "#FF0000" or color name)
Examples
iex> element = UmyaSpreadsheet.RichText.create_text_element("Bold text", %{bold: true, color: "#FF0000"})
iex> is_reference(element)
true
Gets rich text from a specific cell.
Parameters
spreadsheet
- UmyaSpreadsheet resourcesheet_name
- Name of the worksheetcoordinate
- Cell coordinate (e.g., "A1")
Returns
Returns a RichText resource. If the cell doesn't contain rich text, returns an empty RichText.
Examples
iex> spreadsheet = UmyaSpreadsheet.new_file()
iex> rich_text = UmyaSpreadsheet.RichText.get_cell_rich_text(spreadsheet, "Sheet1", "A1")
iex> is_reference(rich_text)
true
Gets font properties from a TextElement.
Parameters
text_element
- TextElement resource
Returns
{:ok, %{}}
- Map of font properties including name, size, bold, italic, strikethrough, underline, and color.
Examples
iex> element = UmyaSpreadsheet.RichText.create_text_element("Test", %{bold: true, size: 12})
iex> {:ok, props} = UmyaSpreadsheet.RichText.get_element_font_properties(element)
iex> props[:bold]
"true"
Gets text content from a TextElement.
Parameters
text_element
- TextElement resource
Examples
iex> element = UmyaSpreadsheet.RichText.create_text_element("Test", %{})
iex> UmyaSpreadsheet.RichText.get_element_text(element)
"Test"
Gets all text elements from a RichText object.
Parameters
rich_text
- RichText resource
Returns
List of TextElement resources.
Examples
iex> rich_text = UmyaSpreadsheet.RichText.create()
iex> UmyaSpreadsheet.RichText.add_formatted_text(rich_text, "Test", %{bold: true})
iex> elements = UmyaSpreadsheet.RichText.get_elements(rich_text)
iex> is_list(elements)
true
Gets plain text from a RichText object.
Extracts all text content without formatting.
Parameters
rich_text
- RichText resource
Examples
iex> rich_text = UmyaSpreadsheet.RichText.create()
iex> UmyaSpreadsheet.RichText.add_formatted_text(rich_text, "Test", %{bold: true})
iex> UmyaSpreadsheet.RichText.get_plain_text(rich_text)
"Test"
Sets rich text to a specific cell.
Parameters
spreadsheet
- UmyaSpreadsheet resourcesheet_name
- Name of the worksheetcoordinate
- Cell coordinate (e.g., "A1")rich_text
- RichText resource to set
Examples
iex> spreadsheet = UmyaSpreadsheet.new_file()
iex> rich_text = UmyaSpreadsheet.RichText.create()
iex> UmyaSpreadsheet.RichText.add_formatted_text(rich_text, "Test", %{bold: true})
iex> UmyaSpreadsheet.RichText.set_cell_rich_text(spreadsheet, "Sheet1", "A1", rich_text)
:ok
Converts RichText to HTML representation.
Parameters
rich_text
- RichText resource
Examples
iex> rich_text = UmyaSpreadsheet.RichText.create()
iex> UmyaSpreadsheet.RichText.add_formatted_text(rich_text, "Bold", %{bold: true})
iex> UmyaSpreadsheet.RichText.to_html(rich_text)
"<b>Bold</b>"