Class: XlsxWriter::Sheet

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

Constant Summary collapse

AUTO =
false

Instance Attribute Summary collapse

Attributes inherited from Xml

#document

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Xml

#generate, #generated?, #render, #template_path

Constructor Details

#initialize(document, name) ⇒ Sheet

Returns a new instance of Sheet.



24
25
26
27
28
29
# File 'lib/xlsx_writer/generators/sheet.rb', line 24

def initialize(document, name)
  @name = Sheet.excel_name name
  @rows = []
  @autofilters = []
  super document
end

Instance Attribute Details

#autofiltersObject (readonly)

Returns the value of attribute autofilters.



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

def autofilters
  @autofilters
end

#freeze_top_leftObject

Freeze the pane under this top left cell



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

def freeze_top_left
  @freeze_top_left
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#rowsObject (readonly)

Returns the value of attribute rows.



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

def rows
  @rows
end

Class Method Details

.excel_name(value) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/xlsx_writer/generators/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



48
49
50
# File 'lib/xlsx_writer/generators/sheet.rb', line 48

def absolute_path
  "/#{relative_path}"
end

#add_autofilter(range) ⇒ Object

specify range like “A1:C1”

Raises:

  • (::RuntimeError)


53
54
55
56
# File 'lib/xlsx_writer/generators/sheet.rb', line 53

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

#add_row(data) ⇒ Object

Raises:

  • (::RuntimeError)


58
59
60
61
62
63
# File 'lib/xlsx_writer/generators/sheet.rb', line 58

def add_row(data)
  raise ::RuntimeError, "Can't add row, already generated!" if generated?
  row = Row.new self, data
  rows << row
  row
end

#local_idObject



35
36
37
# File 'lib/xlsx_writer/generators/sheet.rb', line 35

def local_id
  ndx - 1
end

#ndxObject



31
32
33
# File 'lib/xlsx_writer/generators/sheet.rb', line 31

def ndx
  document.sheets.index(self) + 1
end

#pathObject

override Xml method to save memory



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/xlsx_writer/generators/sheet.rb', line 66

def path
  @path || @mutex.synchronize do
    @path ||= begin
      memo = ::File.join document.staging_dir, relative_path
      ::FileUtils.mkdir_p ::File.dirname(memo)
      ::File.open(memo, 'wb') do |f|
        to_file f
      end
      converted = UnixUtils.unix2dos memo
      ::FileUtils.mv converted, memo
      SheetRels.new(document, self).path
      @generated = true
      memo
    end
  end
end

#relative_pathObject



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

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

#ridObject

+1 because styles.xml occupies the first spot



40
41
42
# File 'lib/xlsx_writer/generators/sheet.rb', line 40

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