Module: RubyExcel

Defined in:
lib/rubyexcel.rb,
lib/rubyexcel/data.rb,
lib/rubyexcel/sheet.rb,
lib/rubyexcel/address.rb,
lib/rubyexcel/element.rb,
lib/rubyexcel/section.rb,
lib/rubyexcel/excel_tools.rb,
lib/rubyexcel/rubyexcel_components.rb

Overview

Namespace for all RubyExcel Classes and Modules

Defined Under Namespace

Modules: Address Classes: Cell, Column, Data, Element, Range, Row, Section, Sheet, Workbook

Class Method Summary collapse

Class Method Details

.borders(range, weight = 1, inner = false) ⇒ WIN32OLE::Range

Add borders to an Excel Range

Parameters:

  • range (WIN32OLE::Range)

    the Excel Range to add borders to

  • weight (Fixnum) (defaults to: 1)

    the weight of the borders

  • inner (Boolean) (defaults to: false)

    add inner borders

Returns:

  • (WIN32OLE::Range)

    the range initially given

Raises:

  • (ArgumentError)

    ‘First Argument must be WIN32OLE Range’



19
20
21
22
23
24
25
26
27
28
# File 'lib/rubyexcel/excel_tools.rb', line 19

def self.borders( range, weight=1, inner=false )
  range.ole_respond_to?( :borders ) or fail ArgumentError, 'First Argument must be WIN32OLE Range'
  [0,1,2,3].include?( weight ) or fail ArgumentError, "Invalid line weight #{ weight }. Must be from 0 to 3"
  defined?( ExcelConstants::XlEdgeLeft ) or WIN32OLE.const_load( range.application, ExcelConstants )
  consts = [ ExcelConstants::XlEdgeLeft, ExcelConstants::XlEdgeTop, ExcelConstants::XlEdgeBottom, ExcelConstants::XlEdgeRight, ExcelConstants::XlInsideVertical, ExcelConstants::XlInsideHorizontal ]
  inner or consts.pop(2)
  weight = [ 0, ExcelConstants::XlThin, ExcelConstants::XlMedium, ExcelConstants::XlThick ][ weight ]
  consts.each { |const| weight.zero? ? range.Borders( const ).linestyle = ExcelConstants::XlNone : range.Borders( const ).weight = weight }
  range
end

.sample_dataObject

Example data to use in tests / demos



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rubyexcel/rubyexcel_components.rb', line 14

def self.sample_data
  [
    [ 'Part',  'Ref1', 'Ref2', 'Qty', 'Cost' ],
    [ 'Type1', 'QT1',  '231',  1,     35.15  ], 
    [ 'Type2', 'QT3',  '123',  1,     40     ], 
    [ 'Type3', 'XT1',  '321',  3,     0.1    ], 
    [ 'Type1', 'XY2',  '132',  1,     30.00  ], 
    [ 'Type4', 'XT3',  '312',  2,     3      ], 
    [ 'Type2', 'QY2',  '213',  1,     99.99  ], 
    [ 'Type1', 'QT4',  '123',  2,     104    ]
  ]
  
end

.sample_hashObject

Example hash to demonstrate imports



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rubyexcel/rubyexcel_components.rb', line 32

def self.sample_hash

  {
    Part1: {
      Type1: {
        SubType1: 1, SubType2: 2, SubType3: 3
      },
      Type2: {
        SubType1: 4, SubType2: 5, SubType3: 6
      }
    },
    Part2: {
      Type1: {
        SubType1: 1, SubType2: 2, SubType3: 3
      },
      Type2: {
        SubType1: 4, SubType2: 5, SubType3: 6
      }
    }
  }

end

.sample_sheetObject

Shortcut to create a Sheet with example data



59
60
61
# File 'lib/rubyexcel/rubyexcel_components.rb', line 59

def self.sample_sheet
  Workbook.new.load RubyExcel.sample_data
end