UmyaSpreadsheet.RichText (umya_spreadsheet_ex v0.7.0)

View Source

Rich 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.

Converts RichText to HTML representation.

Functions

add_formatted_text(rich_text, text, font_props \\ %{})

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 resource
  • text - Text content to add
  • font_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

add_text_element(rich_text, text_element)

Adds a TextElement to a RichText object.

Parameters

  • rich_text - RichText resource
  • text_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

create()

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

create_from_html(html)

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

create_text_element(text, font_props \\ %{})

Creates a TextElement with text and optional font properties.

A TextElement represents a portion of text with consistent formatting.

Parameters

  • text - The text content
  • font_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

get_cell_rich_text(spreadsheet, sheet_name, coordinate)

Gets rich text from a specific cell.

Parameters

  • spreadsheet - UmyaSpreadsheet resource
  • sheet_name - Name of the worksheet
  • coordinate - 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

get_element_font_properties(text_element)

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"

get_element_text(text_element)

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"

get_elements(rich_text)

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

get_plain_text(rich_text)

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"

set_cell_rich_text(spreadsheet, sheet_name, coordinate, rich_text)

Sets rich text to a specific cell.

Parameters

  • spreadsheet - UmyaSpreadsheet resource
  • sheet_name - Name of the worksheet
  • coordinate - 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

to_html(rich_text)

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>"