Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.