Class: OpenXml::Xlsx::Parts::Worksheet

Inherits:
Part
  • Object
show all
Defined in:
lib/openxml/xlsx/parts/worksheet.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(workbook, index) ⇒ Worksheet

Returns a new instance of Worksheet.



7
8
9
10
11
12
13
14
15
# File 'lib/openxml/xlsx/parts/worksheet.rb', line 7

def initialize(workbook, index)
  @workbook = workbook
  @index = index
  @rows = []
  @tables = []
  @cell_ranges = []
  @rels = OpenXml::Parts::Rels.new
  @column_widths = {}
end

Instance Attribute Details

#cell_rangesObject (readonly)

Returns the value of attribute cell_ranges.



5
6
7
# File 'lib/openxml/xlsx/parts/worksheet.rb', line 5

def cell_ranges
  @cell_ranges
end

#indexObject (readonly)

Returns the value of attribute index.



5
6
7
# File 'lib/openxml/xlsx/parts/worksheet.rb', line 5

def index
  @index
end

#relsObject (readonly)

Returns the value of attribute rels.



5
6
7
# File 'lib/openxml/xlsx/parts/worksheet.rb', line 5

def rels
  @rels
end

#rowsObject (readonly)

Returns the value of attribute rows.



5
6
7
# File 'lib/openxml/xlsx/parts/worksheet.rb', line 5

def rows
  @rows
end

#workbookObject (readonly)

Returns the value of attribute workbook.



5
6
7
# File 'lib/openxml/xlsx/parts/worksheet.rb', line 5

def workbook
  @workbook
end

Instance Method Details

#add_row(attributes) ⇒ Object



28
29
30
# File 'lib/openxml/xlsx/parts/worksheet.rb', line 28

def add_row(attributes)
  rows.push Xlsx::Elements::Row.new(self, attributes)
end

#add_rows(*rows) ⇒ Object



22
23
24
25
26
# File 'lib/openxml/xlsx/parts/worksheet.rb', line 22

def add_rows(*rows)
  rows.flatten.each do |attributes|
    add_row attributes
  end
end

#add_table(id, name, ref, columns) ⇒ Object



36
37
38
39
40
# File 'lib/openxml/xlsx/parts/worksheet.rb', line 36

def add_table(id, name, ref, columns)
  table = Xlsx::Parts::Table.new(id, name, ref, columns)
  rels.add_relationship(REL_TABLE, "../tables/#{table.filename}")
  workbook.add_table table
end

#column_widths(*args) ⇒ Object



17
18
19
20
# File 'lib/openxml/xlsx/parts/worksheet.rb', line 17

def column_widths(*args)
  return @column_widths if args.none?
  @column_widths = args.first
end

#merge_cells(*ranges) ⇒ Object



32
33
34
# File 'lib/openxml/xlsx/parts/worksheet.rb', line 32

def merge_cells(*ranges)
  ranges.each { |range| cell_ranges.push range }
end

#nameObject



71
72
73
# File 'lib/openxml/xlsx/parts/worksheet.rb', line 71

def name
  "Sheet#{index}"
end

#to_xmlObject



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
# File 'lib/openxml/xlsx/parts/worksheet.rb', line 42

def to_xml
  build_standalone_xml do |xml|
    xml.worksheet(root_namespaces) do
      xml.sheetViews do
        xml.sheetView(showGridLines: 0, tabSelected: 1, workbookViewId: 0)
      end
      xml.sheetFormatPr(baseColWidth: 10, defaultColWidth: 13.33203125, defaultRowHeight: 20, customHeight: 1)
      xml.cols do
        column_widths.each do |column, width|
          xml.col(min: column, max: column, width: width, customWidth: 1)
        end
      end if column_widths.any?
      xml.sheetData do
        rows.each { |row| row.to_xml(xml) }
      end
      xml.mergeCells(count: merge_cells.size) do
        cell_ranges.each { |range| xml.mergeCell ref: range }
      end if cell_ranges.any?
      xml.pageMargins(left: 0.75, right: 0.75, top: 1, bottom: 1, header: 0.5, footer: 0.5)
      xml.pageSetup(orientation: "portrait", horizontalDpi: 4294967292, verticalDpi: 4294967292)
      xml.tableParts(count: tables.count) do
        tables.each do |rel|
          xml.tablePart("r:id" => rel.id)
        end
      end if tables.any?
    end
  end
end