Class: Webdrone::Xlsx

Inherits:
Object
  • Object
show all
Defined in:
lib/webdrone/xlsx.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(a0) ⇒ Xlsx

Returns a new instance of Xlsx.



11
12
13
14
15
# File 'lib/webdrone/xlsx.rb', line 11

def initialize(a0)
  @a0 = a0
  @filename = 'data.xlsx'
  @sheet = 0
end

Instance Attribute Details

#a0Object

Returns the value of attribute a0.



9
10
11
# File 'lib/webdrone/xlsx.rb', line 9

def a0
  @a0
end

#both(sheet: nil, filename: nil) ⇒ Object

Returns the value of attribute both.



9
10
11
# File 'lib/webdrone/xlsx.rb', line 9

def both
  @both
end

#dict(sheet: nil, filename: nil) ⇒ Object

Returns the value of attribute dict.



9
10
11
# File 'lib/webdrone/xlsx.rb', line 9

def dict
  @dict
end

#filenameObject

Returns the value of attribute filename.



9
10
11
# File 'lib/webdrone/xlsx.rb', line 9

def filename
  @filename
end

#rows(sheet: nil, filename: nil) ⇒ Object

Returns the value of attribute rows.



9
10
11
# File 'lib/webdrone/xlsx.rb', line 9

def rows
  @rows
end

#sheetObject

Returns the value of attribute sheet.



9
10
11
# File 'lib/webdrone/xlsx.rb', line 9

def sheet
  @sheet
end

Instance Method Details

#resetObject



123
124
125
126
127
# File 'lib/webdrone/xlsx.rb', line 123

def reset()
  @dict = @rows = @both = nil
rescue => exception
  Webdrone.report_error(@a0, exception, Kernel.caller_locations)
end

#save(sheet: nil, filename: nil, dict: nil, rows: nil) ⇒ Object



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
# File 'lib/webdrone/xlsx.rb', line 79

def save(sheet: nil, filename: nil, dict: nil, rows: nil)
  @filename = filename if filename
  @sheet = sheet if sheet
  workbook = RubyXL::Parser.parse(@filename)
  worksheet = workbook[@sheet]
  if @dict != nil
    worksheet.sheet_data.rows.tap do |head, *body|
      body.each do |row|
        k = row[0].value
        if @dict.include?(k)
          row[1].change_contents(@dict[k])
        end
      end
    end
  elsif @rows != nil
    @rows.each_with_index do |row, rowi|
      row.each_with_index do |data, coli|
        if worksheet[rowi] == nil || worksheet[rowi][coli] == nil
          worksheet.add_cell(rowi, coli, data)
        else
          worksheet[rowi][coli].change_contents(data)
        end
      end
    end
  elsif @both != nil
    rows = worksheet.sheet_data.rows.collect do |row|
      row.cells.collect do |cell|
        cell.value if cell != nil
      end
    end
    head = rows.shift
    @both.each_with_index do |entry, i|
      entry.each do |k, v|
        index = head.index(k)
        worksheet[i + 1][index].change_contents(v) if index
      end
    end
  end
  k = workbook.write(@filename)
  reset
rescue => exception
  Webdrone.report_error(@a0, exception, Kernel.caller_locations)
end