Class: Cuker::RubyXLFile

Inherits:
AbstractFile show all
Defined in:
lib/cuker/helpers/writers/ruby_x_l_writer.rb

Direct Known Subclasses

SummaryRubyXLFile

Instance Attribute Summary collapse

Attributes inherited from AbstractFile

#name, #rows

Attributes included from LoggerSetup

#log

Instance Method Summary collapse

Methods inherited from AbstractFile

#read_rows

Methods included from LoggerSetup

#init_logger, reset_appender_log_levels

Constructor Details

#initialize(file_name, template_file_name) ⇒ RubyXLFile

Returns a new instance of RubyXLFile.



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
# File 'lib/cuker/helpers/writers/ruby_x_l_writer.rb', line 56

def initialize file_name, template_file_name

  premade = File.basename(file_name) =~ /xlsm/
  # premade = true
  # premade = false
  # template_file_name = "simple_macro_template.xlsm"
  template_file_path = File.join File.dirname(__FILE__), template_file_name
  # template_file_name = './lib/cuker/helpers/writers/simple_macro_template.xlsm'
  # template_file_name = './lib/cuker/helpers/writers/demo_file2.xlsm'
  @file_name = premade ? template_file_path : file_name

  super @file_name
  @log.debug "Making new #{self.class} => #{@file_name}"

  @workbook = premade ? RubyXL::Parser.parse(@file_name) : RubyXL::Workbook.new
  # @workbook.add_worksheet('Acceptance Tests')
  # @workbook[0].sheet_name = 'Acceptance Tests'

  @worksheets = @workbook.worksheets

  # todo: delete sheet convenienve method
  # @workbook['test_id'].delete

  # delete_sheet 'null'
  locate_sheet 'test_id'

  # @worksheet = @workbook[0]
  @worksheet = @workbook['Acceptance Tests raw']


  @rows = sheet_rows.dup # starting Row
  @offset = 0 # starting Col

  # inserting a blank cell to make sure title format is not being copied
  @worksheet.add_cell(@rows.size, @offset, ' ')

  @file_name = file_name
end

Instance Attribute Details

#workbookObject

Returns the value of attribute workbook.



54
55
56
# File 'lib/cuker/helpers/writers/ruby_x_l_writer.rb', line 54

def workbook
  @workbook
end

#worksheetObject

Returns the value of attribute worksheet.



54
55
56
# File 'lib/cuker/helpers/writers/ruby_x_l_writer.rb', line 54

def worksheet
  @worksheet
end

#worksheetsObject

Returns the value of attribute worksheets.



54
55
56
# File 'lib/cuker/helpers/writers/ruby_x_l_writer.rb', line 54

def worksheets
  @worksheets
end

Instance Method Details

#add_row(ary) ⇒ Object Also known as: add_line



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/cuker/helpers/writers/ruby_x_l_writer.rb', line 116

def add_row ary
  super ary
  row, col = current_row, current_col
  worksheet.insert_row(row)
  ary.each do |val|
    case val.class.to_s
    when "String", "Integer", /Nil/
      worksheet.insert_cell(row, col, val)
    else
      @log.error "SummaryRubyXLFile auto stringification of unknown format: #{val.class} => '#{val}'"
      worksheet.insert_cell(row, col, val.to_s)
      # worksheet.insert_cell(row, col, val.to_s)
    end
    col += 1
  end

  link_sheet_name = "#{ary[0]} results"
  workbook.add_worksheet(link_sheet_name)
  # back_link_value = @link_sheet[0][0].value
  # back_link_formula = @link_sheet[0][0].formula
  # @workbook[link_sheet_name].add_cell(0, 0, back_link_value, back_link_formula)
  # workbook.worksheets <<
  # (link_sheet_name)

  @log.trace workbook.worksheets.map(&:sheet_name)
  # @log.debug sheet_rows
  # @log.debug worksheet.rows
end

#current_colObject



112
113
114
# File 'lib/cuker/helpers/writers/ruby_x_l_writer.rb', line 112

def current_col
  @offset
end

#current_rowObject



108
109
110
# File 'lib/cuker/helpers/writers/ruby_x_l_writer.rb', line 108

def current_row
  rows.size - 1
end

#finishupObject



152
153
154
155
156
# File 'lib/cuker/helpers/writers/ruby_x_l_writer.rb', line 152

def finishup
  # @workbook.write("#{@name}")
  @workbook.worksheets.delete_at(locate_sheet 'test_id') if locate_sheet 'test_id'
  @workbook.write("#{@file_name}") if @workbook
end

#locate_sheet(sheet_name) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/cuker/helpers/writers/ruby_x_l_writer.rb', line 95

def locate_sheet sheet_name
  sheet_index = @workbook.worksheets.index {|x| x.sheet_name == sheet_name}
  @link_sheet = @workbook.worksheets[sheet_index]
  if sheet_index
    @log.debug "located sheet #{sheet_name} @location #{sheet_index}"
    # @workbook.worksheets.delete_at(sheet_index)
    sheet_index
  else
    @log.error "no sheet named '#{sheet_name}' found.. available sheets []"
    nil
  end
end

#sheet_rowsObject

Returns ary of rows.

Returns:

  • ary of rows



148
149
150
# File 'lib/cuker/helpers/writers/ruby_x_l_writer.rb', line 148

def sheet_rows
  worksheet.sheet_data.rows
end