Class: Spreadshoot::Worksheet

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

Overview

Single worksheet containing one or more tables.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(spreadsheet, title, options = {}) {|_self| ... } ⇒ Worksheet

Not intended to be called directly. Use Spreadshoot#worksheet to create a worksheet.

Yields:

  • (_self)

Yield Parameters:



342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/spreadshoot.rb', line 342

def initialize spreadsheet, title, options = {}
  @cells = {}
  @spreadsheet = spreadsheet
  @title = title
  @options = options
  @row_index = 0
  @col_index = 0
  @column_widths = {}
  # default table, if none defined
  @current_table = Table.new(self, options)

  yield self
end

Instance Attribute Details

#cellsObject (readonly)

<worksheet xmlns=“schemas.openxmlformats.org/spreadsheetml/2006/main” > <sheetData> <row> <c> <v>1234</v> </c> </row> </sheetData> </worksheet>



338
339
340
# File 'lib/spreadshoot.rb', line 338

def cells
  @cells
end

#col_indexObject (readonly)

<worksheet xmlns=“schemas.openxmlformats.org/spreadsheetml/2006/main” > <sheetData> <row> <c> <v>1234</v> </c> </row> </sheetData> </worksheet>



338
339
340
# File 'lib/spreadshoot.rb', line 338

def col_index
  @col_index
end

#row_indexObject (readonly)

<worksheet xmlns=“schemas.openxmlformats.org/spreadsheetml/2006/main” > <sheetData> <row> <c> <v>1234</v> </c> </row> </sheetData> </worksheet>



338
339
340
# File 'lib/spreadshoot.rb', line 338

def row_index
  @row_index
end

#spreadsheetObject (readonly)

<worksheet xmlns=“schemas.openxmlformats.org/spreadsheetml/2006/main” > <sheetData> <row> <c> <v>1234</v> </c> </row> </sheetData> </worksheet>



338
339
340
# File 'lib/spreadshoot.rb', line 338

def spreadsheet
  @spreadsheet
end

#titleObject (readonly)

<worksheet xmlns=“schemas.openxmlformats.org/spreadsheetml/2006/main” > <sheetData> <row> <c> <v>1234</v> </c> </row> </sheetData> </worksheet>



338
339
340
# File 'lib/spreadshoot.rb', line 338

def title
  @title
end

#xmlObject (readonly)

<worksheet xmlns=“schemas.openxmlformats.org/spreadsheetml/2006/main” > <sheetData> <row> <c> <v>1234</v> </c> </row> </sheetData> </worksheet>



338
339
340
# File 'lib/spreadshoot.rb', line 338

def xml
  @xml
end

Instance Method Details

#row(options = {}, &block) ⇒ Object

Creates a row within a worksheet.

Parameters:

  • options (Hash) (defaults to: {})

    options to create a row with.

Returns:

  • created row

See Also:



389
390
391
392
393
# File 'lib/spreadshoot.rb', line 389

def row options = {}, &block
  row = @current_table.row options, &block
  @row_index += 1
  row
end

#set_col_width(col, width) ⇒ Object

Not intended to be used directly.



414
415
416
# File 'lib/spreadshoot.rb', line 414

def set_col_width col, width
  @column_widths[col] = width
end

#table(options = {}) {|table| ... } ⇒ Table

Creates a table within a worksheet.

Parameters:

  • options (Hash) (defaults to: {})

    Options to initialize table with.

Options Hash (options):

  • :direction (Symbol) — default: :vertical

    Orientation of the table (could be :horizontal or :vertical)

  • :next_to (Table)

    Optionally, place the table to the right of the already existing table

  • :row_topleft (Fixnum)

    Place the tables top left corner absolutely to a certain row

  • :col_topleft (Fixnum)

    Place the tables top left corner absolutely to a certain column

Yields:

Returns:

  • (Table)

    created table



403
404
405
406
407
408
409
410
# File 'lib/spreadshoot.rb', line 403

def table options = {}
  @current_table = table = Table.new(self, options)
  yield table
  @row_index = [@row_index, table.row_topleft + table.row_max].max
  @col_index = 0
  @current_table = Table.new(self, @options) # preparing one in case row directly called next
  table
end

#to_sObject

Outputs the worksheet as OOXML



358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# File 'lib/spreadshoot.rb', line 358

def to_s
  @xml ||= Builder::XmlMarkup.new.worksheet(:xmlns => "http://schemas.openxmlformats.org/spreadsheetml/2006/main") do |ws|
    unless @column_widths.empty?
      ws.cols do |xcols|
        @column_widths.keys.sort.each do |i|
          width = @column_widths[i]
          params = {:min => i+1, :max => i+1, :bestFit => 1}
          params.merge!({:customWidth => 1, :width => width}) if width
          xcols.col(params)
        end
      end
    end
    ws.sheetData do |sd|
      @cells.keys.sort.each do |row|
        sd.row(:r => row+1) do |xr|

          @cells[row].keys.sort.each do |col|
            cell = @cells[row][col]
            cell.output(xr)
          end
        end
      end
    end
  end
end