Class: XLSXDrone::Sheet

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

Overview

XLSX Sheet.

Instance Method Summary collapse

Constructor Details

#initialize(xlsx_sheet_mpointer) ⇒ XLSXDrone::Sheet



8
9
10
11
12
# File 'lib/xlsx_drone/sheet.rb', line 8

def initialize(xlsx_sheet_mpointer)
  @native_sheet = XLSXDrone::NativeBinding::XLSXSheetT.new(xlsx_sheet_mpointer)
  @native_cell = XLSXDrone::NativeBinding::XLSXCellT.new(FFI::MemoryPointer.new(1, XLSXDrone::NativeBinding::XLSXCellT.size, true))
  @styles = {}
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/xlsx_drone/sheet.rb', line 35

def empty?
  last_row() == 0 ? true : false
end

#last_columnString

Returns “A” if the sheet is empty.

Returns:

  • (String)

    “A” if the sheet is empty



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/xlsx_drone/sheet.rb', line 20

def last_column
  if(!@last_column)
    mpointer = XLSXDrone::NativeBinding.xlsx_get_last_column(@native_sheet) # NULL or a string
    if(mpointer.null?)
      # the sheet is empty
      @last_column = 'A'
    else
      @last_column = mpointer.get_string(0).force_encoding(Encoding::UTF_8)
    end
  else
    @last_column
  end
end

#last_rowInteger

Returns 0 if the sheet is empty.

Returns:

  • (Integer)

    0 if the sheet is empty



15
16
17
# File 'lib/xlsx_drone/sheet.rb', line 15

def last_row
  @native_sheet[:last_row]
end

#nameString

Returns:

  • (String)


40
41
42
# File 'lib/xlsx_drone/sheet.rb', line 40

def name
  @native_sheet[:name].get_string(0).force_encoding(Encoding::UTF_8)
end

#read_cell(row, column) ⇒ Integer, ...

Parameters:

  • row (Integer)
  • column (String)

Returns:

  • (Integer, Float, String, Time, NilClass)


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/xlsx_drone/sheet.rb', line 47

def read_cell(row, column)
  XLSXDrone::NativeBinding.xlsx_read_cell(@native_sheet, row, column, @native_cell)
  # if it has no style, then it's either a string or a number
  if(@native_cell[:style].null?)
    case @native_cell[:value_type]
      when 0
        @native_cell[:value][:pointer_to_char_value].get_string(0).force_encoding(Encoding::UTF_8)
      when 1
        @native_cell[:value][:int_value]
      when 2
        @native_cell[:value][:long_long_value]
      when 3
        @native_cell[:value][:double_value]
      else
        nil
    end
  else
    address = @native_cell[:style].address
    # speeding purpose
    if(!(@styles.has_key?(address)))
      style_obj = XLSXDrone::NativeBinding::XLSXStyleT.new(@native_cell[:style])
      @styles[address] = style_obj[:related_category]
    end
    case @styles[address]
      when 2
        # XLSX_DATE, it could be represented also as plain string
        if(@native_cell[:value_type] == 0)
          @native_cell[:value][:pointer_to_char_value].get_string(0).force_encoding(Encoding::UTF_8)
        else
          Time.new(1900) + ((@native_cell[:value][:int_value] - 2) * 86400)
        end
      when 4
        # XLSX_DATE_TIME, there are specific cases in which it's a DATE_TIME, but the internal representation appears as an int, so basically
        # the "time" part of the data comes fixed at mid-day or at the start of the day, that's what you actually see on Excel
        case(@native_cell[:value_type])
          when 0
            @native_cell[:value][:pointer_to_char_value].get_string(0).force_encoding(Encoding::UTF_8)
          when 1
            Time.new(1900) + ((@native_cell[:value][:int_value] - 2) * 86400)
          else
            match = @native_cell[:value][:double_value].to_s.match(/(\d+)\.(\d+)/)
            integral_part = match[1].to_i
            floating_part = "0.#{match[2]}".to_f
            Time.new(1900) + ((integral_part - 2) * 86400) + (floating_part * 86400)
        end
      when 0
        # XLSX_NUMBER
        case @native_cell[:value_type]
          when 1
            @native_cell[:value][:int_value]
          when 2
            @native_cell[:value][:long_long_value]
          when 3
            @native_cell[:value][:double_value]
          else
            nil
        end
      when 1
        # XLSX_TEXT
        @native_cell[:value][:pointer_to_char_value].get_string(0).force_encoding(Encoding::UTF_8)
      when 3
        # XLSX_TIME
        (Time.new(1900) + (@native_cell[:value][:double_value] * 86400)).strftime("%H:%M:%S")
      else
        # XLSX_UNKNOWN
        case @native_cell[:value_type]
          when 0
            @native_cell[:value][:pointer_to_char_value].get_string(0).force_encoding(Encoding::UTF_8)
          when 1
            @native_cell[:value][:int_value]
          when 2
            @native_cell[:value][:long_long_value]
          when 3
            @native_cell[:value][:double_value]
          else
            nil
        end
    end
  end
end