Class: ExcelProcessor

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(spreadsheet, image_log = {}) ⇒ ExcelProcessor

Returns a new instance of ExcelProcessor.



9
10
11
12
# File 'lib/excel_processor.rb', line 9

def initialize(spreadsheet, image_log={})
  @spreadsheet = spreadsheet
  @image_log = image_log
end

Instance Attribute Details

#image_logObject

Returns the value of attribute image_log.



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

def image_log
  @image_log
end

#spreadsheetObject

Returns the value of attribute spreadsheet.



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

def spreadsheet
  @spreadsheet
end

Instance Method Details

#read_spreadsheetObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/excel_processor.rb', line 14

def read_spreadsheet
  excel = WIN32OLE.new('Excel.Application')
  excel.visible = false
  img_log = excel.Workbooks.Open(@spreadsheet)
  img_ws = img_log.Worksheets(1)
     
     # Set-up arrays for column values

    link_array = []
    figure_array = []

    # Pull values from Figure # column

    for row in 10..img_ws.UsedRange.Rows.Count do 
      cell = img_ws.Cells(row,3).value.to_s
      if cell != nil
        c = cell.sub(/(\D*)/, "fig_")
        new_cell = c.sub(/(\.)/, "-")
      else
        new_cell = ""
      end

      figure_array << new_cell
    end
  
    # Pull values from Source URL column

    for row in 10..img_ws.UsedRange.Rows.Count do 
      cell = img_ws.Cells(row,4).Value
      if (cell != nil) && (cell.include?("."))
        new_cell = cell
      else
        new_cell = ""
      end

      link_array << new_cell
    end

    # Zip arrays into hash of figure numbers and links

    @image_log = Hash[figure_array.zip(link_array)]

    # Remove blanks

    @image_log.delete_if { |k, v| k == "" }

  # Shut it down 

  excel.ActiveWorkbook.Close(0)
  excel.Quit
end