Class: Rbxl::EditableCell

Inherits:
Object
  • Object
show all
Defined in:
lib/rbxl/editable_cell.rb

Overview

A view onto a single <c> element inside an EditableWorksheet.

Cells are not stored — each call to Rbxl::EditableWorksheet#cell returns a fresh EditableCell that resolves the underlying <c> node on demand. Reads decode the current XML; writes mutate the worksheet’s DOM and mark the sheet dirty so the next Rbxl::EditableWorkbook#save re-serializes it.

Type matrix on write

  • nil — clears the cell’s value (children + t attribute removed), leaving an empty <c> that retains its s (style index)

  • true / false — boolean cell (+t=“b”+)

  • Integer / Float — number cell (no t attribute)

  • String — inline string cell (+t=“inlineStr”+); xl/sharedStrings.xml is never mutated, so this round-trips deterministically without a second pass over the SST

  • Date / Time / DateTime — raises EditableCellTypeError; convert to a numeric serial yourself if you need a date cell. Date support is intentionally deferred so 1.4.0 doesn’t ship a half-baked numFmt write

When overwriting an existing cell, the s (style index) attribute is preserved so template formatting (number format, font, fill, alignment) carries through to the new value. Any <f> (formula) and cached <v> are dropped — assigning a value means the cell is no longer a formula.

Constant Summary collapse

MAIN_NS =

Namespace for the main SpreadsheetML schema.

"http://schemas.openxmlformats.org/spreadsheetml/2006/main".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(worksheet:, coordinate:) ⇒ EditableCell

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Construct via Rbxl::EditableWorksheet#cell; not for direct use.

Parameters:

  • worksheet (EditableWorksheet)
  • coordinate (String)

    already-normalized A1-style coordinate



39
40
41
42
# File 'lib/rbxl/editable_cell.rb', line 39

def initialize(worksheet:, coordinate:)
  @worksheet = worksheet
  @coordinate = coordinate
end

Instance Attribute Details

#coordinateString (readonly)

Returns Excel-style coordinate, e.g. “B5”.

Returns:

  • (String)

    Excel-style coordinate, e.g. “B5”



32
33
34
# File 'lib/rbxl/editable_cell.rb', line 32

def coordinate
  @coordinate
end

Instance Method Details

#valueString, ...

Decodes the current value of the cell.

Returns:

  • (String, Integer, Float, true, false, nil)

    the cell’s value, or nil if the cell does not exist or has no stored value. Boolean cells return true/false; numeric cells return Integer when the stored value is integer-shaped, Float otherwise; t=“s” cells resolve through the workbook’s shared strings table; t=“inlineStr” and t=“str” cells return the literal text



52
53
54
55
56
57
# File 'lib/rbxl/editable_cell.rb', line 52

def value
  node = @worksheet.find_or_create_cell_node(@coordinate, create: false)
  return nil unless node

  decode(node)
end

#value=(new_value) ⇒ Object

Sets the cell’s value. See the class-level “Type matrix on write” documentation for accepted Ruby types and how each is serialized.

Parameters:

  • new_value (String, Integer, Float, true, false, nil)

Returns:

  • (Object)

    new_value

Raises:



66
67
68
69
70
71
72
73
# File 'lib/rbxl/editable_cell.rb', line 66

def value=(new_value)
  reject_unsupported_type!(new_value)

  node = @worksheet.find_or_create_cell_node(@coordinate, create: true)
  apply_value(node, new_value)
  @worksheet.mark_dirty!
  new_value
end