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

#a0 ⇒ Object

Returns the value of attribute a0.



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

def a0
  @a0
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

#filename ⇒ Object

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, dict: false) ⇒ Object

Returns the value of attribute rows.



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

def rows
  @rows
end

#sheet ⇒ Object

Returns the value of attribute sheet.



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

def sheet
  @sheet
end

Instance Method Details

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



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

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
        row[1].change_contents(dict[k]) if dict.include?(k)
      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
  end
  workbook.write(filename)
  @dict = @rows = nil
end