Class: XlsxWriter::Sheet

Inherits:
Object
  • Object
show all
Defined in:
lib/xlsx_writer/sheet.rb

Constant Summary collapse

BUFSIZE =

128kb

131072

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document, name, ndx) ⇒ Sheet

Returns a new instance of Sheet.



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/xlsx_writer/sheet.rb', line 29

def initialize(document, name, ndx)
  @mutex = Mutex.new
  @document = document
  @ndx = ndx
  @name = Sheet.excel_name name
  @row_count = 0
  @autofilters = []
  @max_row_length = 1
  @max_cell_pixel_width = Hash.new(Cell.pixel_width(5))
  @path = ::File.join document.staging_dir, relative_path
  ::FileUtils.mkdir_p ::File.dirname(path)
  @rows_tmp_file_writer = ::File.open(rows_tmp_file_path, 'wb')
end

Instance Attribute Details

#autofiltersObject (readonly)

Returns the value of attribute autofilters.



20
21
22
# File 'lib/xlsx_writer/sheet.rb', line 20

def autofilters
  @autofilters
end

#documentObject (readonly)

Returns the value of attribute document.



17
18
19
# File 'lib/xlsx_writer/sheet.rb', line 17

def document
  @document
end

#freeze_top_leftObject

Freeze the pane under this top left cell



27
28
29
# File 'lib/xlsx_writer/sheet.rb', line 27

def freeze_top_left
  @freeze_top_left
end

#max_cell_pixel_widthObject (readonly)

Returns the value of attribute max_cell_pixel_width.



24
25
26
# File 'lib/xlsx_writer/sheet.rb', line 24

def max_cell_pixel_width
  @max_cell_pixel_width
end

#max_row_lengthObject (readonly)

Returns the value of attribute max_row_length.



23
24
25
# File 'lib/xlsx_writer/sheet.rb', line 23

def max_row_length
  @max_row_length
end

#nameObject (readonly)

Returns the value of attribute name.



18
19
20
# File 'lib/xlsx_writer/sheet.rb', line 18

def name
  @name
end

#ndxObject (readonly)

Returns the value of attribute ndx.



19
20
21
# File 'lib/xlsx_writer/sheet.rb', line 19

def ndx
  @ndx
end

#pathObject (readonly)

Returns the value of attribute path.



21
22
23
# File 'lib/xlsx_writer/sheet.rb', line 21

def path
  @path
end

#row_countObject (readonly)

Returns the value of attribute row_count.



22
23
24
# File 'lib/xlsx_writer/sheet.rb', line 22

def row_count
  @row_count
end

Class Method Details

.excel_name(value) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/xlsx_writer/sheet.rb', line 6

def excel_name(value)
  str = value.to_s.dup
  str.gsub! '/', ''   # remove forward slashes
  str.gsub! /\s+/, '' # compress "inner" whitespace
  str.strip!          # trim whitespace from ends
  str.fast_xs
end

Instance Method Details

#absolute_pathObject



105
106
107
# File 'lib/xlsx_writer/sheet.rb', line 105

def absolute_path
  "/#{relative_path}"
end

#add_autofilter(range) ⇒ Object

specify range like “A1:C1”

Raises:

  • (::RuntimeError)


110
111
112
113
# File 'lib/xlsx_writer/sheet.rb', line 110

def add_autofilter(range)
  raise ::RuntimeError, "Can't add autofilter, already generated!" if generated?
  autofilters << Autofilter.new(self, range)
end

#add_row(cells) ⇒ Object

Raises:

  • (::RuntimeError)


115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/xlsx_writer/sheet.rb', line 115

def add_row(cells)
  raise ::RuntimeError, "Can't add row, already generated!" if generated?
  @row_count += 1
  row = Row.new self, cells, row_count
  @rows_tmp_file_writer.write row.to_xml
  if (l = row.cells.length) > max_row_length
    @max_row_length = l
  end
  row.cells.each_with_index do |cell, x|
    if (w = cell.pixel_width) > max_cell_pixel_width[x]
      max_cell_pixel_width[x] = w
    end
  end
  nil
end

#generateObject



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
# File 'lib/xlsx_writer/sheet.rb', line 47

def generate
  return if generated?
  @mutex.synchronize do
    return if generated?
    @generated = true
    @rows_tmp_file_writer.close
    File.open(path, 'wb') do |f|
      f.write "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n<worksheet xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\">\n"
      if freeze_top_left
        f.write "<sheetViews>\n<sheetView workbookViewId=\"0\">\n<pane ySplit=\"\#{y_split}\" topLeftCell=\"\#{freeze_top_left}\" activePane=\"topLeft\" state=\"frozen\"/>\n</sheetView>\n</sheetViews>\n"
      end
      f.write %{<cols>}
      (0..max_row_length-1).each do |x|
        f.write %{<col min="#{x+1}" max="#{x+1}" width="#{max_cell_pixel_width[x]}" bestFit="1" customWidth="1" />}
      end
      f.write %{</cols>}
      f.write %{<sheetData>}
      File.open(rows_tmp_file_path, 'rb') do |rows_tmp_file_reader|
        buffer = ''
        while rows_tmp_file_reader.read(BUFSIZE, buffer)
          f.write buffer
        end
      end
      f.write %{</sheetData>}
      autofilters.each { |autofilter| f.write autofilter.to_xml }
      f.write document.page_setup.to_xml
      f.write document.header_footer.to_xml
      f.write %{</worksheet>}
    end
    File.unlink rows_tmp_file_path
    converted = UnixUtils.unix2dos path
    FileUtils.mv converted, path
    SheetRels.new(document, self).generate
  end
end

#generated?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/xlsx_writer/sheet.rb', line 43

def generated?
  @generated == true
end

#local_idObject



92
93
94
# File 'lib/xlsx_writer/sheet.rb', line 92

def local_id
  ndx - 1
end

#relative_pathObject



101
102
103
# File 'lib/xlsx_writer/sheet.rb', line 101

def relative_path
  "xl/worksheets/sheet#{ndx}.xml"
end

#ridObject

+1 because styles.xml occupies the first spot



97
98
99
# File 'lib/xlsx_writer/sheet.rb', line 97

def rid
  "rId#{ndx + 1}"
end