Class: Axlsx::Cell
- Inherits:
-
Object
- Object
- Axlsx::Cell
- Includes:
- OptionsParser
- Defined in:
- lib/axlsx/workbook/worksheet/cell.rb
Overview
The recommended way to generate cells is via Worksheet#add_row
A cell in a worksheet. Cell stores inforamation requried to serialize a single worksheet cell to xml. You must provde the Row that the cell belongs to and the cells value. The data type will automatically be determed if you do not specify the :type option. The default style will be applied if you do not supply the :style option. Changing the cell's type will recast the value to the type specified. Altering the cell's value via the property accessor will also automatically cast the provided value to the cell's type.
Constant Summary collapse
- INLINE_STYLES =
An array of available inline styes. TODO change this to a hash where each key defines attr name and validator (and any info the validator requires) then move it out to a module so we can re-use in in other classes. needs to define bla=(v) and bla methods on the class that hook into a set_attr method that kicks the suplied validator and updates the instance_variable for the key
['value', 'type', 'font_name', 'charset', 'family', 'b', 'i', 'strike','outline', 'shadow', 'condense', 'extend', 'u', 'vertAlign', 'sz', 'color', 'scheme']
Instance Attribute Summary collapse
-
#b ⇒ Boolean
The inline bold property for the cell.
-
#charset ⇒ String
The inline charset property for the cell As far as I can tell, this is pretty much ignored.
-
#color ⇒ Color
The inline color property for the cell.
-
#condense ⇒ Boolean
The inline condense property for the cell.
-
#extend ⇒ Boolean
The inline extend property for the cell.
-
#family ⇒ Integer
The inline family property for the cell 1 Roman 2 Swiss 3 Modern 4 Script 5 Decorative.
-
#font_name ⇒ String
The inline font_name property for the cell.
-
#formula_value ⇒ Object
this is the cached value for formula cells.
-
#i ⇒ Boolean
The inline italic property for the cell.
-
#outline ⇒ Boolean
The inline outline property for the cell.
-
#row ⇒ Row
readonly
The row this cell belongs to.
-
#scheme ⇒ Symbol
The inline scheme property for the cell this must be one of [:none, major, minor].
-
#shadow ⇒ Boolean
The inline shadow property for the cell.
-
#ssti ⇒ Integer
readonly
The Shared Strings Table index for this cell.
-
#strike ⇒ Boolean
The inline strike property for the cell.
-
#style ⇒ Integer
The index of the cellXfs item to be applied to this cell.
-
#sz ⇒ Inteter
The inline sz property for the cell.
-
#type ⇒ Symbol
The cell's data type.
-
#u ⇒ Boolean, String
The inline underline property for the cell.
-
#value ⇒ String, ...
The value of this cell.
-
#vertAlign ⇒ Symbol
The inline vertical alignment property for the cell this must be one of [:baseline, :subscript, :superscript].
Instance Method Summary collapse
-
#autowidth ⇒ Object
This is still not perfect...
-
#index ⇒ Integer
The index of the cell in the containing row.
-
#initialize(row, value = "", options = {}) ⇒ Cell
constructor
A new instance of Cell.
- #is_formula? ⇒ Boolean
-
#is_text_run? ⇒ Boolean
Indicates that the cell has one or more of the custom cell styles applied.
-
#merge(target) ⇒ Object
Merges all the cells in a range created between this cell and the cell or string name for a cell provided.
-
#plain_string? ⇒ Boolean
Indicates if the cell is good for shared string table.
-
#pos ⇒ Array
Of x/y coordinates in the cheet for this cell.
-
#r ⇒ String
The alpha(column)numeric(row) reference for this sell.
-
#r_abs ⇒ String
The absolute alpha(column)numeric(row) reference for this sell.
-
#reference(absolute = true) ⇒ String
returns the absolute or relative string style reference for this cell.
-
#to_xml_string(r_index, c_index, str = '') ⇒ String
Serializes the cell.
Methods included from OptionsParser
Constructor Details
#initialize(row, value = "", options = {}) ⇒ Cell
Returns a new instance of Cell.
33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 33 def initialize(row, value="", ={}) self.row=row @value = nil #@value = @font_name = @charset = @family = @b = @i = @strike = @outline = @shadow = nil #@formula_value = @condense = @u = @vertAlign = @sz = @color = @scheme = @extend = @ssti = nil @styles = row.worksheet.workbook.styles @row.cells << self @style ||= 0 @type ||= cell_type_from_value(value) @value = cast_value(value) end |
Instance Attribute Details
#b ⇒ Boolean
The inline bold property for the cell
161 162 163 |
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 161 def b @b end |
#charset ⇒ String
The inline charset property for the cell As far as I can tell, this is pretty much ignored. However, based on the spec it should be one of the following: 0  ANSI_CHARSET 1 DEFAULT_CHARSET 2 SYMBOL_CHARSET 77 MAC_CHARSET 128 SHIFTJIS_CHARSET 129  HANGUL_CHARSET 130  JOHAB_CHARSET 134  GB2312_CHARSET 136  CHINESEBIG5_CHARSET 161  GREEK_CHARSET 162  TURKISH_CHARSET 163  VIETNAMESE_CHARSET 177  HEBREW_CHARSET 178  ARABIC_CHARSET 186  BALTIC_CHARSET 204  RUSSIAN_CHARSET 222  THAI_CHARSET 238  EASTEUROPE_CHARSET 255  OEM_CHARSET
142 143 144 |
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 142 def charset @charset end |
#color ⇒ Color
The inline color property for the cell
215 216 217 |
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 215 def color @color end |
#condense ⇒ Boolean
The inline condense property for the cell
191 192 193 |
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 191 def condense @condense end |
#extend ⇒ Boolean
The inline extend property for the cell
197 198 199 |
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 197 def extend @extend end |
#family ⇒ Integer
The inline family property for the cell 1 Roman 2 Swiss 3 Modern 4 Script 5 Decorative
153 154 155 |
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 153 def family @family end |
#font_name ⇒ String
The inline font_name property for the cell
116 117 118 |
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 116 def font_name @font_name end |
#formula_value ⇒ Object
this is the cached value for formula cells. If you want the values to render in iOS/Mac OSX preview you need to set this.
48 49 50 |
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 48 def formula_value @formula_value end |
#i ⇒ Boolean
The inline italic property for the cell
167 168 169 |
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 167 def i @i end |
#outline ⇒ Boolean
The inline outline property for the cell
179 180 181 |
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 179 def outline @outline end |
#row ⇒ Row
The row this cell belongs to.
68 69 70 |
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 68 def row @row end |
#scheme ⇒ Symbol
The inline scheme property for the cell this must be one of [:none, major, minor]
241 242 243 |
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 241 def scheme @scheme end |
#shadow ⇒ Boolean
The inline shadow property for the cell
185 186 187 |
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 185 def shadow @shadow end |
#ssti ⇒ Integer
The Shared Strings Table index for this cell
250 251 252 |
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 250 def ssti @ssti end |
#strike ⇒ Boolean
The inline strike property for the cell
173 174 175 |
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 173 def strike @strike end |
#style ⇒ Integer
The index of the cellXfs item to be applied to this cell.
64 65 66 |
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 64 def style @style end |
#sz ⇒ Inteter
The inline sz property for the cell
224 225 226 |
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 224 def sz @sz end |
#type ⇒ Symbol
If the value provided cannot be cast into the type specified, type is changed to :string and the following logic is applied. :string to :integer or :float, type conversions always return 0 or 0.0 :string, :integer, or :float to :time conversions always return the original value as a string and set the cells type to :string. No support is currently implemented for parsing time strings.
The cell's data type. Currently only six types are supported, :date, :time, :float, :integer, :string and :boolean. Changing the type for a cell will recast the value into that type. If no type option is specified in the constructor, the type is automatically determed.
81 82 83 |
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 81 def type @type end |
#u ⇒ Boolean, String
true is for backwards compatability and is reassigned to :single
The inline underline property for the cell. It must be one of :none, :single, :double, :singleAccounting, :doubleAccounting, true
206 207 208 |
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 206 def u @u end |
#value ⇒ String, ...
The value of this cell.
92 93 94 |
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 92 def value @value end |
#vertAlign ⇒ Symbol
The inline vertical alignment property for the cell this must be one of [:baseline, :subscript, :superscript]
231 232 233 |
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 231 def vertAlign @vertAlign end |
Instance Method Details
#autowidth ⇒ Object
This is still not perfect...
- scaling is not linear as font sizes increst
- different fonts have different mdw and char widths
313 314 315 316 317 318 |
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 313 def autowidth return if is_formula? || value == nil mdw = 1.78 #This is the widest width of 0..9 in arial@10px) font_scale = (font_size/10.0).to_f ((value.to_s.count(Worksheet.thin_chars) * mdw + 5) / mdw * 256) / 256.0 * font_scale end |
#index ⇒ Integer
Returns The index of the cell in the containing row.
253 254 255 |
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 253 def index @row.cells.index(self) end |
#is_formula? ⇒ Boolean
306 307 308 |
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 306 def is_formula? @type == :string && @value.to_s.start_with?('=') end |
#is_text_run? ⇒ Boolean
Indicates that the cell has one or more of the custom cell styles applied.
101 102 103 |
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 101 def is_text_run? @is_text_run ||= false end |
#merge(target) ⇒ Object
Merges all the cells in a range created between this cell and the cell or string name for a cell provided
288 289 290 291 292 293 294 295 |
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 288 def merge(target) range_end = if target.is_a?(String) target elsif(target.is_a?(Cell)) target.r end self.row.worksheet.merge_cells "#{self.r}:#{range_end}" unless range_end.nil? end |
#plain_string? ⇒ Boolean
Indicates if the cell is good for shared string table
106 107 108 109 110 111 112 |
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 106 def plain_string? @type == :string && # String typed !is_text_run? && # No inline styles !@value.nil? && # Not nil !@value.empty? && # Not empty !@value.start_with?('=') # Not a formula end |
#pos ⇒ Array
Returns of x/y coordinates in the cheet for this cell.
281 282 283 |
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 281 def pos [index, row.index] end |
#r ⇒ String
Returns The alpha(column)numeric(row) reference for this sell.
260 261 262 |
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 260 def r Axlsx::cell_r index, @row.index end |
#r_abs ⇒ String
Returns The absolute alpha(column)numeric(row) reference for this sell.
267 268 269 |
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 267 def r_abs "$#{r.match(%r{([A-Z]+)([0-9]+)})[1,2].join('$')}" end |
#reference(absolute = true) ⇒ String
returns the absolute or relative string style reference for this cell. returned.
325 326 327 |
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 325 def reference(absolute=true) absolute ? r_abs : r end |
#to_xml_string(r_index, c_index, str = '') ⇒ String
Serializes the cell
302 303 304 |
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 302 def to_xml_string(r_index, c_index, str = '') CellSerializer.to_xml_string r_index, c_index, self, str end |