Class: Xsv::Workbook

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/xsv/workbook.rb

Overview

An OOXML Spreadsheet document is called a Workbook. A Workbook consists of multiple Sheets that are available in the array that’s accessible through #sheets

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(zip, trim_empty_rows: false, parse_headers: false) ⇒ Workbook

Open a workbook from an instance of Zip::File. Generally it’s recommended to use the open method instead of the constructor.

Parameters:

  • trim_empty_rows (Boolean) (defaults to: false)

    Scan sheet for end of content and don’t return trailing rows

  • parse_headers (Boolean) (defaults to: false)

    Call ‘parse_headers!` on all sheets on load

Raises:

  • (ArgumentError)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/xsv/workbook.rb', line 27

def initialize(zip, trim_empty_rows: false, parse_headers: false)
  raise ArgumentError, "Passed argument is not an instance of Zip::File. Did you mean to use Workbook.open?" unless zip.is_a?(Zip::File)
  raise Xsv::Error, "Zip::File is empty" if zip.size.zero?

  @zip = zip
  @trim_empty_rows = trim_empty_rows

  @sheets = []
  @xfs, @num_fmts = fetch_styles
  @sheet_ids = fetch_sheet_ids
  @relationships = fetch_relationships
  @shared_strings = fetch_shared_strings
  @sheets = fetch_sheets(parse_headers ? :hash : :array)
end

Instance Attribute Details

#num_fmtsObject (readonly)

Returns the value of attribute num_fmts.



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

def num_fmts
  @num_fmts
end

#shared_stringsObject (readonly)

Returns the value of attribute shared_strings.



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

def shared_strings
  @shared_strings
end

#sheetsArray<Sheet> (readonly)

Access the Sheet objects contained in the workbook

Returns:



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

def sheets
  @sheets
end

#trim_empty_rowsObject (readonly)

Returns the value of attribute trim_empty_rows.



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

def trim_empty_rows
  @trim_empty_rows
end

#xfsObject (readonly)

Returns the value of attribute xfs.



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

def xfs
  @xfs
end

Class Method Details

.open(data, **kws, &block) ⇒ Object

Deprecated.

Use Xsv.open instead



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

def self.open(data, **kws, &block)
  Xsv.open(data, **kws, &block)
end

Instance Method Details

#[](index_or_name) ⇒ <Xsv::Sheet>?

Get a sheet by index or name

Parameters:

  • index_or_name (String, Integer)

    The name of the sheet or index in the workbook

Returns:

  • (<Xsv::Sheet>, nil)

    returns the sheet instance or nil if it was not found



81
82
83
84
85
86
87
88
89
90
# File 'lib/xsv/workbook.rb', line 81

def [](index_or_name)
  case index_or_name
  when Integer
    sheets[index_or_name]
  when String
    sheets_by_name(index_or_name).first
  else
    raise ArgumentError, "Sheets can be accessed by Integer of String only"
  end
end

#closetrue

Close the handle to the workbook file and leave all resources for the GC to collect

Returns:

  • (true)


49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/xsv/workbook.rb', line 49

def close
  @zip.close
  @zip = nil
  @sheets = nil
  @xfs = nil
  @num_fmts = nil
  @relationships = nil
  @shared_strings = nil
  @sheet_ids = nil

  true
end

#each(&block) ⇒ Object



74
75
76
# File 'lib/xsv/workbook.rb', line 74

def each(&block)
  sheets.each(&block)
end

#get_num_fmt(style) ⇒ Object

Get number format for given style index



70
71
72
# File 'lib/xsv/workbook.rb', line 70

def get_num_fmt(style)
  @num_fmts[@xfs[style][:numFmtId]]
end

#inspectString

Returns:

  • (String)


43
44
45
# File 'lib/xsv/workbook.rb', line 43

def inspect
  "#<#{self.class.name}:#{object_id} sheets=#{sheets.count} trim_empty_rows=#{@trim_empty_rows}>"
end

#sheets_by_name(name) ⇒ Array<Xsv::Sheet>

Returns an array of sheets for the case of same name sheets.

Parameters:

  • name (String)

Returns:



65
66
67
# File 'lib/xsv/workbook.rb', line 65

def sheets_by_name(name)
  @sheets.select { |s| s.name == name }
end