Class: Woa::Energon::ExcelDocument
- Includes:
- REXML
- Defined in:
- lib/energon/excel_document.rb
Overview
This is a subclass of Document but it deals with Excel templates instead of Text templates.
Everything is the same as Document (Except the input and the output). See Document for more details.
:include: rdoc-header
Constant Summary
Constants inherited from Document
Document::DefaultDelimiter, Document::DefaultNewLine
Instance Attribute Summary
Attributes inherited from Document
Instance Method Summary collapse
- #write ⇒ Object (also: #close)
Methods inherited from Document
#add_value, #add_values, #initialize, #valid?
Constructor Details
This class inherits a constructor from Woa::Energon::Document
Instance Method Details
#write ⇒ Object Also known as: close
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 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 |
# File 'lib/energon/excel_document.rb', line 16 def write() raise NoPlaceholderFound if @placeholders.empty? rows = {} @placeholders.each do |placeholder_hash| placeholder = placeholder_hash[:placeholder] data = Parser.merge(placeholder, @datas) if data.is_a?(Array) placeholder_hash[:cells].each do |cell| parent = cell begin next unless parent.name == "row" rows[parent] = [] if rows[parent].nil? rows[parent] << {:cell => cell, :data => data} break end while (parent = parent.parent) end else element = placeholder_hash[:shared_string_element] element.text = data end end inserted_rows = -1 rows.each do |row, cells| last_row = row index = row.attributes.get_attribute('r').value.to_i continue = true while continue cells.each do |element| datas = element[:data] cell = element[:cell] data = datas.pop if data.nil? continue = false break end write_data_to_cell data, cell end if continue new_row = row.deep_clone new_row.add_attribute('r', index.to_s) XPath.each(new_row, '//c[@r]') do |element| value = element.attributes.get_attribute('r').value.to_s.gsub(/\d+/, index.to_s) element.add_attribute('r', value) end index = index.next row.parent.insert_after(last_row, new_row) last_row = new_row inserted_rows = inserted_rows.next end end root = row.parent initial_index = row.attributes.get_attribute('r').value.to_i row.parent.delete(row) index = last_row.parent.index(last_row) XPath.each(root, 'row') do |element| next if element.parent.index(element) <= index value = element.attributes.get_attribute('r').value.to_i + inserted_rows element.add_attribute('r', value.to_s) XPath.each(element, 'c[@r]') do |element| new_value = element.attributes.get_attribute('r').value.to_s.gsub(/\d+/, value.to_s) element.add_attribute('r', new_value) end end XPath.each(root, '//mergeCell[@ref]') do |element| value = element.attributes.get_attribute('ref').value.to_s if /([A-Z]+)(\d+):([A-Z]+)(\d+)/ =~ value v1, v2, v3, v4 = Regexp.last_match[1..4] v2 = v2.to_i + inserted_rows if v2.to_i > initial_index v4 = v4.to_i + inserted_rows if v4.to_i > initial_index element.add_attribute('ref', "#{v1}#{v2}:#{v3}#{v4}") end end end @documents.each {|document| @openxml.save(document) } @openxml.write end |