Class: Fastxl::Workbook

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

Instance Method Summary collapse

Constructor Details

#initialize(xlsx) ⇒ Workbook

Returns a new instance of Workbook.



2
3
4
5
6
# File 'lib/fastxl/workbook.rb', line 2

def initialize(xlsx)
  @xlsx = xlsx
  @modifications = {}
  @virtual_dom = {}
end

Instance Method Details

#append(sheet_name, sheet_content) ⇒ Object



16
17
# File 'lib/fastxl/workbook.rb', line 16

def append(sheet_name, sheet_content)
end

#delete(sheet_name) ⇒ Object



19
20
# File 'lib/fastxl/workbook.rb', line 19

def delete(sheet_name)
end

#flushObject



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
# File 'lib/fastxl/workbook.rb', line 22

def flush
  buffer = Zip::OutputStream.write_buffer do |out|
    document_paths = @virtual_dom.keys

    @xlsx.entries.each do |e|
      if e.name.in?(document_paths)
        document_path = e.name
        doc = xml_doc(document_path)
        doc.xpath('//xmlns:sheetData').xpath('//xmlns:row').remove
        doc.xpath('//xmlns:sheetData').first.add_child(rows(@virtual_dom[document_path]))

        doc.xpath('//xmlns:worksheet').xpath('//xmlns:cols').remove
        doc.xpath('//xmlns:worksheet').first.add_child('<cols><col collapsed="false" hidden="false" max="1025" min="1" style="0" width="11.3418367346939"/></cols>')

        out.put_next_entry(document_path)

        out.write doc.to_xml(:indent => 0).gsub("\n","")
      else
        out.put_next_entry(e.name)
        out.write e.get_input_stream.read
      end
    end
  end

  buffer
end

#prepend(sheet_name, sheet_content) ⇒ Object



13
14
# File 'lib/fastxl/workbook.rb', line 13

def prepend(sheet_name, sheet_content)
end

#sheetsObject



8
9
10
11
# File 'lib/fastxl/workbook.rb', line 8

def sheets
  doc = xml_doc('xl/workbook.xml')
  doc.xpath('//xmlns:sheet').map { |t| t.attributes['name'].value }
end

#write(sheet_index, sheet_content) ⇒ Object



49
50
51
52
53
# File 'lib/fastxl/workbook.rb', line 49

def write(sheet_index, sheet_content)
  @virtual_dom.merge!({
    "xl/worksheets/sheet#{sheet_index}.xml" => sheet_content
  })
end