require 'net/http' require 'net/https' require 'uri' require 'rubygems' require 'hpricot' require 'timeout' # Make it easy to use some of the convenience methods using https # module Net class HTTPS < HTTP def initialize(address, port = nil) super(address, port) self.use_ssl = true end end end class GoogleSpreadSheet GOOGLE_LOGIN_URL = URI.parse('https://www.google.com/accounts/ClientLogin') def initialize(spreadsheet_key) @spreadsheet_key = spreadsheet_key @headers = nil @default_sheet = nil end def default_sheet=(numberofsheet) @default_sheet = numberofsheet end def authenticate(email, password) $VERBOSE = nil response = Net::HTTPS.post_form(GOOGLE_LOGIN_URL, {'Email' => email, 'Passwd' => password, 'source' => "formula", 'service' => 'wise' }) @headers = { 'Authorization' => "GoogleLogin auth=#{response.body.split(/=/).last}", 'Content-Type' => 'application/atom+xml' } end def evaluate_cell(cell) path = "/feeds/cells/#{@spreadsheet_key}/#{@default_sheet}/#{@headers ? "private" : "public"}/basic/#{cell}" doc = Hpricot(request(path)) result = (doc/"content[@type='text']").inner_html end def set_entry(entry) path = "/feeds/cells/#{@spreadsheet_key}/#{@default_sheet}/#{@headers ? 'private' : 'public'}/full" post(path, entry) end def entry(formula, row=1, col=1) < XML end def add_to_cell(formula) #puts entry(formula) set_entry(entry(formula)) end private def request(path) response, data = get_http.get(path, @headers) data end def post(path, entry) get_http.post(path, entry, @headers) end def get_http http = Net::HTTP.new('spreadsheets.google.com', 80) #http.set_debug_output $stderr http end end class Google < Openoffice TIMEOUT_IN_SECONDS = 2 def initialize(user, password, spreadsheetkey) @cells_read = false @cell = Hash.new @cell_type = Hash.new Timeout.timeout(TIMEOUT_IN_SECONDS) { @gs = GoogleSpreadSheet.new(spreadsheetkey) @gs.authenticate(user, password) @default_sheet = nil } end def sheets # http://spreadsheets.google.com/feeds/worksheets/ptu6bbahNZpYrdGHwteUNCw/private/full if DateTime.now < Date.new(2007,6,15) return ["Sheet eins","Sheet zwei","Sheet drei"] else return [] end end def default_sheet=(numberofsheet) @gs.default_sheet = numberofsheet end def cell(row, col) row,col = normalize(row,col) # formula = 42 || 'sin(0.2)' # gs = GoogleSpreadSheet.new("ptu6bbahNZpYrdGHwteUNCw") # gs.authenticate('thopre@gmail.com', 'nora3033') # gs.add_to_cell formula cellname = number_to_letter(col)+row.to_s Timeout.timeout(TIMEOUT_IN_SECONDS) { return @gs.evaluate_cell(cellname) # 'A1') } end def celltype(row, col) "string" end def empty?(row, col) cell(row, col).empty? end def read_cells # http://spreadsheets.google.com/feeds/list/ptu6bbahNZpYrdGHwteUNCw/1/private/full file = File.new("/home/tp/aaa.xml") @doc = REXML::Document.new file file.close @doc.each_element do |element| if element.name == "feed" p "feed gefunden" element.each_element do |feed| p feed feed.each_element do |fe| p fe.name end end end end # @cell["1,2"] = 42 # @cell_type["1,2"] = "string" # @cells_read = true # return #TODO: die Grenzen sind noch fix 1.upto(10) do |y| Openoffice.letter_to_number('A').upto(Openoffice.letter_to_number('H')) do |x| # p x.to_s+", "+y.to_s # unless empty?(y,x) # @cell["#{y},#{x}"] = cell(y,x) # @cell_type["#{y},#{x}"] = cell(y,x) # end end end @cells_read = true end def first_row read_cells unless @cells_read 1 end def last_row read_cells unless @cells_read 100 end end # class