Git Product home page Git Product logo

exceltocsv's Introduction

ExcelToCsv::ExcelFile

ExcelFile is a file converter to convert Excel spreadsheets to CSV files. It is specifically designed for the criteria required to generate properly formated CSV files for use with GDLC.

Usage

Quick example:

require 'exceltocsv'

converter = ExcelToCsv::ExcelFile.new
converter.xl_to_csv( 'path/to/input.xls', 'path/to/output.csv' )

Example rake task that updates (converts xls files) csvs based on last modified date of each file within a directory structure.

plk.rake
require 'exceltocsv'

desc "Update CSV files from XLS source"
task :update do
  plks = FileList['plk/xls/**/*.xls']

  # Pathmap string maps to csv dir with csv target file
  pm = "%{^plk/xls,plk/csv;.xls$,.csv;.xlsx$,.csv}p"
  # Remove any source files when the dest file exists and is newer.
  plks.delete_if do |s|
    # Downcase the path,
    d = s.pathmap( pm ).downcase
    # and snakecase the target filename.
    d = snakecase_filename(d)
    File.exists?(d) && File.stat(s).mtime <= File.stat(d).mtime
  end

  target_csvs = plks.pathmap( pm )

  # I want the target filenames normalized to lower case.
  target_csvs.each { |p| p.downcase! }

  # Create all target dirs
  target_dirs = target_csvs.pathmap("%d")
  target_dirs.uniq!
  mkdir_p target_dirs

  # Convert all newer XL files to CSVs.
  # Note that this method only converts the first sheet in the workbook.
  converter = ExcelToCsv::ExcelFile.new
  plks.each do |x|
    converter.xl_to_csv(x, snakecase_filename(x.pathmap(pm).downcase))
  end

  puts "All target files are up to date" if plks.empty?
end

def snakecase_filename(filepath)
  snake_file_path = File.join(filepath.pathmap("%d"), filepath.pathmap("%n").snakecase + filepath.pathmap("%x"))
end

License

See LICENSE. Website: http://ktechsystems.com

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.