Skip to content

Instantly share code, notes, and snippets.

@mikeymckay
Created July 5, 2009 20:49
Show Gist options
  • Save mikeymckay/141114 to your computer and use it in GitHub Desktop.
Save mikeymckay/141114 to your computer and use it in GitHub Desktop.
Spreadems - sms from a spreadsheet
require 'rubygems'
require 'sinatra'
require 'roo'
get '/' do
sample_data = [
["12025551212", "Happy New Year"],
["12025551212", "Happy Easter"],
["12025551212", "Happy Kwanza!!!"]
]
style + '
<div>Upload an excel or openoffice spreadsheet. The first column should have the phone number, with the second column having the message to send to that number</div>' +
display_spreadsheet(sample_data) + '
<form name="form" action="spreadsheet" method="POST" enctype="multipart/form-data">
<input type="file" name="data">
<br/>
<input type="submit">
</form>
'
end
post '/spreadsheet' do
spreadsheet = case params[:data][:filename]
when /\.xls/
Excel.new(params[:data][:tempfile].path, false, :ignore)
when /\.ods/
Openoffice.new(params[:data][:tempfile].path, false, :ignore)
end
data = []
1.upto(spreadsheet.last_row){|row|
data.push [spreadsheet.cell(row,1), spreadsheet.cell(row,2)]
}
style +
"Uploaded data<br/>" +
display_spreadsheet(data) +
"<button>Click to send as SMS</button> <small>(Coming soon!)</small><br/>" +
"<a href='/'>Back</a>"
end
def display_spreadsheet(data)
out = ""
data.each_with_index{|item, index|
out += "<tr><td class='header'>#{index+1}</td><td>#{item[0]}</td><td>#{item[1]}</td></tr>"
}
"<table>
<tr class='header'><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td>A</td><td>B</td></tr>
#{out}
</table>"
end
def style
"<style>
body{
background-color: lightblue;
font-size:2em;
}
table{
background-color: white;
margin-top: 10px;
margin-bottom: 10px;
padding: 10px 10px 10px 10px;
}
td{
border:1px solid gray;
}
.header{
background-color: lightgray;
color: black;
text-align: center;
}
div{
background-color: white;
}
</style>"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment