Class: Xsv::Workbook
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, &block) ⇒ Object
deprecated
Deprecated.
Use open instead
Instance Method Summary collapse
-
#[](index_or_name) ⇒ <Xsv::Sheet>?
Get a sheet by index or name.
-
#close ⇒ true
Close the handle to the workbook file and leave all resources for the GC to collect.
- #each(&block) ⇒ Object
-
#get_num_fmt(style) ⇒ Object
Get number format for given style index.
-
#initialize(zip, trim_empty_rows: false, parse_headers: 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, 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.
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_fmts ⇒ Object (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_strings ⇒ Object (readonly)
Returns the value of attribute shared_strings.
15 16 17 |
# File 'lib/xsv/workbook.rb', line 15 def shared_strings @shared_strings end |
#sheets ⇒ Array<Sheet> (readonly)
Access the Sheet objects contained in the workbook
13 14 15 |
# File 'lib/xsv/workbook.rb', line 13 def sheets @sheets end |
#trim_empty_rows ⇒ Object (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 |
#xfs ⇒ Object (readonly)
Returns the value of attribute xfs.
15 16 17 |
# File 'lib/xsv/workbook.rb', line 15 def xfs @xfs end |
Class Method Details
Instance Method Details
#[](index_or_name) ⇒ <Xsv::Sheet>?
Get a sheet by index or name
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 |
#close ⇒ true
Close the handle to the workbook file and leave all resources for the GC to collect
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 |
#inspect ⇒ 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.
65 66 67 |
# File 'lib/xsv/workbook.rb', line 65 def sheets_by_name(name) @sheets.select { |s| s.name == name } end |