Giter Club home page Giter Club logo

excelxml's Introduction

ExcelXml

ExcelXml can parse the data out of an Excel XML Spreadsheet 2003.

See the Usage section below to get an idea of how it works.

Installation

Add this line to your application's Gemfile:

gem 'excelxml'

And then execute:

$ bundle

Or install it yourself as:

$ gem install excelxml

Usage

require 'excelxml'

# First step is to inherit from a worksheet parser.
class PersonParser < ExcelXml::Worksheet::Parser
  NAME = /name/i
  CITY = /city/i
  STATE = /state/i
  AGE = /age/i

  class Person < Struct.new(:name, :city, :state, :age); end

  # Override the #mandator_columns() method to auto discover your worksheets header row. 
  # #is_header? could alternatively be overridden.
  def mandatory_columns
    [NAME, CITY]
  end

  # A #rows accessor will be available to your parser so that you can iterate through worksheet data.
  def persons
    @persons ||= rows.collect do |fields| 
      Person.new(fields[NAME], fields[CITY], fields[STATE], fields[AGE])
    end
  end
end

# Next, let the workbook parser match your worksheet parsers to worksheets.
my_parsers = ExcelXml::Workbook::Parser.new(File.read(workbook), worksheet_parsers: PersonParser) 

# You'll get a PersonParser instance for each worksheet that had a header row as defined in PersonParser.
my_parsers[PersonParser].is_a? Array                     # => true
person_parser = my_parsers[PersonParser].first           # => #<PersonParser:0x00555555f42ab8>

person_parser.worksheet.name                             # => "PersonSheet" (name of worksheet tab)
person_parser.persons.each do |person| 
  puts "#{person.name} lives in #{person.city}."
end

# Worksheets that did not match a parsers #mandatory_columns or #is_header? end up here.
raw_worksheet = my_parsers.unidentified_worksheets.first # => #<ExcelXml::Worksheet:0x00555555f43841>
raw_worksheet.name                                       # => "NoHeaderSheet" (name of worksheet tab)

# ExcelXml::Worksheet#rows is a 2-dimensional array of string representing your worksheet.
raw_worksheet.rows.each do |raw_row| 
  raw_row.each {|raw_data| puts raw_data }
end

Contributing

  1. Fork it ( http://github.com/bdiz/excelxml/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

excelxml's People

Contributors

bdiz avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.