Class: SimpleXlsx::Sheet

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document, name, stream, &block) ⇒ Sheet

Returns a new instance of Sheet.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/simple_xlsx/sheet.rb', line 9

def initialize document, name, stream, &block
  @document = document
  @stream = stream
  @name = name.to_xs
  @row_ndx = 1
  @stream.write <<-ends
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
<sheetData>
ends
  if block_given?
    yield self
  end
  @stream.write "</sheetData></worksheet>"
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/simple_xlsx/sheet.rb', line 6

def name
  @name
end

#ridObject

Returns the value of attribute rid.



7
8
9
# File 'lib/simple_xlsx/sheet.rb', line 7

def rid
  @rid
end

Class Method Details

.abcObject



76
77
78
# File 'lib/simple_xlsx/sheet.rb', line 76

def self.abc
  @@abc ||= ('A'..'Z').to_a
end

.column_index(n) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/simple_xlsx/sheet.rb', line 80

def self.column_index n
  result = []
  while n >= 26 do
    result << abc[n % 26]
    n /= 26
  end
  result << abc[result.empty? ? n : n - 1]
  result.reverse.join
end

.days_since_jan_1_1900(date) ⇒ Object



65
66
67
68
# File 'lib/simple_xlsx/sheet.rb', line 65

def self.days_since_jan_1_1900 date
  @@jan_1_1904 ||= Date.parse("1904 Jan 1")
  (date - @@jan_1_1904).to_i + 1462 # http://support.microsoft.com/kb/180162
end

.format_field_and_type_and_style(val_and_type) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/simple_xlsx/sheet.rb', line 36

def self.format_field_and_type_and_style val_and_type
  value, type = val_and_type
  if value.is_a?(String)
    [:inlineStr, "<is><t>#{value.to_xs}</t></is>", 5]
  elsif type == :period
    [:n, "<v>#{days_since_jan_1_1900(value.to_date)}</v>", 7]
  elsif value.is_a?(Date)
    [:n, "<v>#{days_since_jan_1_1900(value)}</v>", 2]
  elsif value.is_a?(Time)
    [:n, "<v>#{fractional_days_since_jan_1_1900(value)}</v>", 1]
  elsif value.is_a?(TrueClass) || value.is_a?(FalseClass)
    [:b, "<v>#{value ? '1' : '0'}</v>", 6]
  elsif type == :percent
    if value.is_a?(BigDecimal)
      [:n, "<v>#{value.to_s('f')}</v>", 8]
    else
      [:n, "<v>#{value.to_s}</v>", 8]
    end
  elsif value.is_a?(BigDecimal)
    [:n, "<v>#{value.to_s('f')}</v>", 4]
  elsif value.is_a?(Float)
    [:n, "<v>#{value.to_s}</v>", 4]
  elsif value.is_a?(Numeric)
    [:n, "<v>#{value.to_s}</v>", 3]
  else
    [:inlineStr, "<is><t>#{value.to_s.to_xs}</t></is>", 5]
  end
end

.fractional_days_since_jan_1_1900(value) ⇒ Object



70
71
72
73
74
# File 'lib/simple_xlsx/sheet.rb', line 70

def self.fractional_days_since_jan_1_1900 value
  @@jan_1_1904_midnight ||= ::Time.utc(1904, 1, 1)
  ((value - @@jan_1_1904_midnight) / 86400.0) + #24*60*60
    1462 # http://support.microsoft.com/kb/180162
end

Instance Method Details

#add_row(arry) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/simple_xlsx/sheet.rb', line 25

def add_row arry
  row = ["<row r=\"#{@row_ndx}\">"]
  arry.each_with_index do |val_and_type, col_ndx|
    kind, ccontent, cstyle = Sheet.format_field_and_type_and_style val_and_type
    row << "<c r=\"#{Sheet.column_index(col_ndx)}#{@row_ndx}\" t=\"#{kind.to_s}\" s=\"#{cstyle}\">#{ccontent}</c>"
  end
  row << "</row>"
  @row_ndx += 1
  @stream.write(row.join())
end