Module: Xsv

Defined in:
lib/xsv.rb,
lib/xsv/sheet.rb,
lib/xsv/helpers.rb,
lib/xsv/version.rb,
lib/xsv/workbook.rb,
lib/xsv/sax_parser.rb,
lib/xsv/styles_handler.rb,
lib/xsv/sheet_rows_handler.rb,
lib/xsv/sheets_ids_handler.rb,
lib/xsv/sheet_bounds_handler.rb,
lib/xsv/relationships_handler.rb,
lib/xsv/shared_strings_parser.rb

Overview

XSV is a fast, lightweight parser for Office Open XML spreadsheet files (commonly known as Excel or .xlsx files). It strives to be minimal in the sense that it provides nothing a CSV reader wouldn’t, meaning it only deals with minimal formatting and cannot create or modify documents.

Defined Under Namespace

Modules: Helpers Classes: AssertionFailed, DuplicateHeaders, Error, RelationshipsHandler, SaxParser, SharedStringsParser, Sheet, SheetBoundsHandler, SheetRowsHandler, SheetsIdsHandler, StylesHandler, Workbook

Constant Summary collapse

VERSION =
"1.3.1"

Class Method Summary collapse

Class Method Details

.open(filename_or_string, trim_empty_rows: false, parse_headers: false) ⇒ Xsv::Workbook

Open the workbook of the given filename, string or buffer.

Parameters:

  • filename_or_string (String, IO)

    the contents or filename of a workbook

  • 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

Returns:



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/xsv.rb', line 35

def self.open(filename_or_string, trim_empty_rows: false, parse_headers: false)
  zip = if filename_or_string.is_a?(IO) || filename_or_string.respond_to?(:read) # is it a buffer?
    Zip::File.open_buffer(filename_or_string)
  elsif filename_or_string.start_with?("PK\x03\x04") # is it a string containing a file?
    Zip::File.open_buffer(filename_or_string)
  else # must be a filename
    Zip::File.open(filename_or_string)
  end

  workbook = Xsv::Workbook.new(zip, trim_empty_rows: trim_empty_rows, parse_headers: parse_headers)

  if block_given?
    begin
      yield(workbook)
    ensure
      workbook.close
    end
  else
    workbook
  end
end