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.



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

def initialize document, name, stream, &block
  @document = document
  @stream =  stream
  @name = name
  @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.



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

def name
  @name
end

#ridObject

Returns the value of attribute rid.



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

def rid
  @rid
end

Class Method Details

.abcObject



68
69
70
# File 'lib/simple_xlsx/sheet.rb', line 68

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

.column_index(n) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/simple_xlsx/sheet.rb', line 72

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



57
58
59
60
# File 'lib/simple_xlsx/sheet.rb', line 57

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(value) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/simple_xlsx/sheet.rb', line 37

def self.format_field_and_type_and_style value
  if value.is_a?(String)
    [:inlineStr, "<is><t>#{value.to_xs}</t></is>", 5]
  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]
  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]
  else
    [:inlineStr, "<is><t>#{value.to_s.to_xs}</t></is>", 5]
  end
end

.fractional_days_since_jan_1_1900(value) ⇒ Object



62
63
64
65
66
# File 'lib/simple_xlsx/sheet.rb', line 62

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



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

def add_row arry
  row = [%Q{<row r="#{@row_ndx}">}]
  arry.each_with_index do |value, col_ndx|
    kind, ccontent, cstyle = Sheet.format_field_and_type_and_style value
    row << %Q{<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