Skip to content

Instantly share code, notes, and snippets.

@ansonhoyt
Created March 1, 2012 03:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ansonhoyt/1946919 to your computer and use it in GitHub Desktop.
Save ansonhoyt/1946919 to your computer and use it in GitHub Desktop.
Convert Excel of Arbitrary Tasks to JSON
#!/usr/bin/env ruby
# Reads an Excel and writes as JSON
# - Assumes header row containing field names, first sheet is read
# - Keeps null values
#
# TODO:
# - accept filename and read based on extension (xlsx, xls, csv).
# - output actual JSON.
# - clean up.
require 'rubygems'
require 'roo'
require 'json'
xlsx = Excelx.new("doc/Tasks.xlsx")
xlsx.default_sheet = xlsx.sheets.first
field_names = xlsx.row(xlsx.header_line)
arr = []
(xlsx.first_row + 1).upto(xlsx.last_row) do |line|
obj = {}
0.upto(field_names.count - 1) do |i|
obj[field_names[i]] = xlsx.row(line)[i]
end
arr << obj
end
puts JSON.pretty_generate arr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment