Class: Webdrone::Xlsx

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(a0) ⇒ Xlsx

Returns a new instance of Xlsx.



15
16
17
18
19
# File 'lib/webdrone/xlsx.rb', line 15

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

Instance Attribute Details

#a0Object (readonly)

Returns the value of attribute a0.



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

def a0
  @a0
end

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



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

def both(sheet: nil, filename: nil)
  update_sheet_filename(sheet, filename)

  if @both.nil?
    reset
    workbook = RubyXL::Parser.parse(@filename)
    worksheet = workbook[@sheet]
    rows = worksheet.sheet_data.rows.collect do |row|
      row.cells.collect do |cell|
        cell&.value
      end
    end
    head = rows.shift
    @both = rows.collect do |row|
      dict = {}
      row.each_with_index do |val, i|
        dict[head[i]] = val if !head[i].nil?
      end
      dict
    end
  end

  @both
rescue StandardError => error
  Webdrone.report_error(@a0, error)
end

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



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/webdrone/xlsx.rb', line 21

def dict(sheet: nil, filename: nil)
  update_sheet_filename(sheet, filename)

  if @dict.nil?
    reset
    @dict = {}
    workbook = RubyXL::Parser.parse(@filename)
    worksheet = workbook[@sheet]
    worksheet.sheet_data.rows.tap do |_head, *body|
      body.each do |row|
        k, v = row[0].value, row[1].value
        @dict[k] = v
      end
    end
  end

  @dict
rescue StandardError => error
  Webdrone.report_error(@a0, error)
end

#filenameObject

Returns the value of attribute filename.



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

def filename
  @filename
end

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



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/webdrone/xlsx.rb', line 42

def rows(sheet: nil, filename: nil)
  update_sheet_filename(sheet, filename)

  if @rows.nil?
    reset
    workbook = RubyXL::Parser.parse(@filename)
    worksheet = workbook[@sheet]
    @rows = worksheet.sheet_data.rows.collect do |row|
      row.cells.collect do |cell|
        cell&.value
      end
    end
  end

  @rows
rescue StandardError => error
  Webdrone.report_error(@a0, error)
end

#sheetObject

Returns the value of attribute sheet.



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

def sheet
  @sheet
end

Instance Method Details

#resetObject



140
141
142
143
144
# File 'lib/webdrone/xlsx.rb', line 140

def reset
  @dict = @rows = @both = nil
rescue StandardError => error
  Webdrone.report_error(@a0, error)
end

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



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
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/webdrone/xlsx.rb', line 88

def save(sheet: nil, filename: nil, dict: nil, rows: nil)
  @sheet = sheet if sheet
  @filename = filename if filename
  @dict = dict if dict
  @rows = rows if rows
  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
      end
    end
    head = rows.shift
    @both.each_with_index do |entry, rowi|
      entry.each do |k, v|
        coli = head.index(k)
        if coli
          if worksheet[rowi + 1].nil? || worksheet[rowi + 1][coli].nil?
            worksheet.add_cell(rowi + 1, coli, v)
          else
            worksheet[rowi + 1][coli].change_contents(v)
          end
        end
      end
    end
  end
  workbook.write(@filename)
  reset
rescue StandardError => error
  Webdrone.report_error(@a0, error)
end