Class: Xsv::Workbook
- Inherits:
-
Object
- Object
- Xsv::Workbook
- 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
-
#num_fmts ⇒ Object
readonly
Returns the value of attribute num_fmts.
-
#shared_strings ⇒ Object
readonly
Returns the value of attribute shared_strings.
-
#sheets ⇒ Array<Sheet>
readonly
Access the Sheet objects contained in the workbook.
-
#trim_empty_rows ⇒ Object
readonly
Returns the value of attribute trim_empty_rows.
-
#xfs ⇒ Object
readonly
Returns the value of attribute xfs.
Class Method Summary collapse
-
.open(data, **kws) ⇒ Object
Open the workbook of the given filename, string or buffer.
Instance Method Summary collapse
-
#close ⇒ true
Close the handle to the workbook file and leave all resources for the GC to collect.
-
#get_num_fmt(style) ⇒ Object
Get number format for given style index.
-
#initialize(zip, trim_empty_rows: false) ⇒ Workbook
constructor
Open a workbook from an instance of Zip::File.
- #inspect ⇒ String
-
#sheets_by_name(name) ⇒ Array<Xsv::Sheet>
Returns an array of sheets for the case of same name sheets.
Constructor Details
#initialize(zip, trim_empty_rows: false) ⇒ Workbook
Open a workbook from an instance of Zip::File. Generally it's recommended to use the open method instead of the constructor.
Options:
trim_empty_rows (false) Scan sheet for end of content and don't return trailing rows
44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/xsv/workbook.rb', line 44 def initialize(zip, trim_empty_rows: 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 end |
Instance Attribute Details
#num_fmts ⇒ Object (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_strings ⇒ Object (readonly)
Returns the value of attribute shared_strings.
13 14 15 |
# File 'lib/xsv/workbook.rb', line 13 def shared_strings @shared_strings end |
#sheets ⇒ Array<Sheet> (readonly)
Access the Sheet objects contained in the workbook
11 12 13 |
# File 'lib/xsv/workbook.rb', line 11 def sheets @sheets end |
#trim_empty_rows ⇒ Object (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 |
#xfs ⇒ Object (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) ⇒ Object
Open the workbook of the given filename, string or buffer. For additional options see #initialize
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/xsv/workbook.rb', line 17 def self.open(data, **kws) @workbook = if data.is_a?(IO) || data.respond_to?(:read) # is it a buffer? new(Zip::File.open_buffer(data), **kws) elsif data.start_with?("PK\x03\x04") # is it a string containing a file? new(Zip::File.open_buffer(data), **kws) else # must be a filename new(Zip::File.open(data), **kws) end if block_given? begin yield(@workbook) ensure @workbook.close end else @workbook end end |
Instance Method Details
#close ⇒ true
Close the handle to the workbook file and leave all resources for the GC to collect
66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/xsv/workbook.rb', line 66 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
87 88 89 |
# File 'lib/xsv/workbook.rb', line 87 def get_num_fmt(style) @num_fmts[@xfs[style][:numFmtId]] end |
#inspect ⇒ String
60 61 62 |
# File 'lib/xsv/workbook.rb', line 60 def inspect "#<#{self.class.name}:#{object_id}>" end |
#sheets_by_name(name) ⇒ Array<Xsv::Sheet>
Returns an array of sheets for the case of same name sheets.
82 83 84 |
# File 'lib/xsv/workbook.rb', line 82 def sheets_by_name(name) @sheets.select { |s| s.name == name } end |