Class: Xsv::Workbook

Inherits:
Object
  • Object
show all
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)


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

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.



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

def num_fmts
  @num_fmts
end

#shared_stringsObject (readonly)

Returns the value of attribute shared_strings.



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

def shared_strings
  @shared_strings
end

#sheetsArray<Sheet> (readonly)

Access the Sheet objects contained in the workbook

Returns:



11
12
13
# File 'lib/xsv/workbook.rb', line 11

def sheets
  @sheets
end

#trim_empty_rowsObject (readonly)

Returns the value of attribute trim_empty_rows.



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

def trim_empty_rows
  @trim_empty_rows
end

#xfsObject (readonly)

Returns the value of attribute xfs.



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

def xfs
  @xfs
end

Class Method Details

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

Deprecated.

Use Xsv.open instead



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

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

Instance Method Details

#closetrue

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

Returns:

  • (true)


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

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

  true
end

#get_num_fmt(style) ⇒ Object

Get number format for given style index



68
69
70
# File 'lib/xsv/workbook.rb', line 68

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

#inspectString

Returns:

  • (String)


41
42
43
# File 'lib/xsv/workbook.rb', line 41

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:



63
64
65
# File 'lib/xsv/workbook.rb', line 63

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