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:



352
353
354
355
356
357
358
359
360
361
362
363
364
# File 'lib/spreadshoot.rb', line 352

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>



348
349
350
# File 'lib/spreadshoot.rb', line 348

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>



348
349
350
# File 'lib/spreadshoot.rb', line 348

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>



348
349
350
# File 'lib/spreadshoot.rb', line 348

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>



348
349
350
# File 'lib/spreadshoot.rb', line 348

def spreadsheet
  @spreadsheet
end

#titleObject (readonly)

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



348
349
350
# File 'lib/spreadshoot.rb', line 348

def title
  @title
end

#xmlObject (readonly)

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



348
349
350
# File 'lib/spreadshoot.rb', line 348

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:



399
400
401
402
403
# File 'lib/spreadshoot.rb', line 399

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.



424
425
426
# File 'lib/spreadshoot.rb', line 424

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



413
414
415
416
417
418
419
420
# File 'lib/spreadshoot.rb', line 413

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



368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# File 'lib/spreadshoot.rb', line 368

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