Class: Cuker::RubyXLFile

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

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) ⇒ RubyXLFile

Returns a new instance of RubyXLFile.



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

def initialize file_name

  premade = File.basename(file_name) =~ /xlsm/
  # premade = true
  # premade = false
  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_name : 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.



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

def workbook
  @workbook
end

#worksheetObject

Returns the value of attribute worksheet.



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

def worksheet
  @worksheet
end

#worksheetsObject

Returns the value of attribute worksheets.



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

def worksheets
  @worksheets
end

Instance Method Details

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



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/cuker/helpers/writers/ruby_x_l_writer.rb', line 113

def add_row ary
  super ary
  row, col = current_row, current_col
  worksheet.insert_row(row)
  ary.each do |val|
    worksheet.insert_cell(row, col, val.to_s)
    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



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

def current_col
  @offset
end

#current_rowObject



105
106
107
# File 'lib/cuker/helpers/writers/ruby_x_l_writer.rb', line 105

def current_row
  rows.size - 1
end

#finishupObject



142
143
144
145
146
# File 'lib/cuker/helpers/writers/ruby_x_l_writer.rb', line 142

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



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/cuker/helpers/writers/ruby_x_l_writer.rb', line 92

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



138
139
140
# File 'lib/cuker/helpers/writers/ruby_x_l_writer.rb', line 138

def sheet_rows
  worksheet.sheet_data.rows
end