Created
March 1, 2012 03:03
-
-
Save ansonhoyt/1946919 to your computer and use it in GitHub Desktop.
Convert Excel of Arbitrary Tasks to JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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