Class: XlsxKit::Writer::Sheet
- Inherits:
-
Object
- Object
- XlsxKit::Writer::Sheet
- Defined in:
- lib/xlsxkit/writer.rb
Overview
Sheet 数据结构
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #<<(row) ⇒ Object
- #add_row(row) ⇒ Object
- #add_rows(rows) ⇒ Object
-
#each_row ⇒ Object
逐行产出 [values, row_num],统一将 Hash 行按表头对齐为数组。 若首行为 Hash,则以 keys 生成表头行,其余 Hash 行按表头对齐(缺失 key 补 nil)。.
-
#initialize(name) ⇒ Sheet
constructor
A new instance of Sheet.
Constructor Details
#initialize(name) ⇒ Sheet
Returns a new instance of Sheet.
233 234 235 236 |
# File 'lib/xlsxkit/writer.rb', line 233 def initialize(name) @name = name @rows = [] end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
231 232 233 |
# File 'lib/xlsxkit/writer.rb', line 231 def name @name end |
Instance Method Details
#<<(row) ⇒ Object
238 239 240 |
# File 'lib/xlsxkit/writer.rb', line 238 def <<(row) add_row(row) end |
#add_row(row) ⇒ Object
242 243 244 245 |
# File 'lib/xlsxkit/writer.rb', line 242 def add_row(row) @rows << row self end |
#add_rows(rows) ⇒ Object
247 248 249 250 |
# File 'lib/xlsxkit/writer.rb', line 247 def add_rows(rows) rows.each { |r| add_row(r) } self end |
#each_row ⇒ Object
逐行产出 [values, row_num],统一将 Hash 行按表头对齐为数组。 若首行为 Hash,则以 keys 生成表头行,其余 Hash 行按表头对齐(缺失 key 补 nil)。
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/xlsxkit/writer.rb', line 255 def each_row return if @rows.empty? if @rows.first.is_a?(Hash) headers = @rows.first.keys yield headers, 1 @rows.each_with_index do |row, idx| values = row.is_a?(Hash) ? headers.map { |k| row[k] } : Array(row) yield values, idx + 2 end else @rows.each_with_index do |row, idx| yield Array(row), idx + 1 end end end |