Skip to content

Instantly share code, notes, and snippets.

@chroju
Created May 3, 2015 08:29
Show Gist options
  • Select an option

  • Save chroju/7b9d422732f1a0ddd45e to your computer and use it in GitHub Desktop.

Select an option

Save chroju/7b9d422732f1a0ddd45e to your computer and use it in GitHub Desktop.
Google Docs家計簿入力スクリプト
require 'rubygems'
require 'google_drive'
class CalcsUpdate
def initialize(inputs)
# Authorizes with OAuth and gets an access token.
client = Google::APIClient.new(:application_name => "calcs_update", :application_version => 0.1)
auth = client.authorization
auth.client_id = "CLIENT ID"
auth.client_secret = "CLIENT SECRET"
auth.scope = "https://www.googleapis.com/auth/drive " + "https://spreadsheets.google.com/feeds/"
auth.redirect_uri = "urn:ietf:wg:oauth:2.0:oob"
print("1. Open this page:\n%s\n\n" % auth.authorization_uri)
print("2. Enter the authorization code shown in the page: ")
auth.code = $stdin.gets.chomp
auth.fetch_access_token!
access_token = auth.access_token
# Creates a session.
session = GoogleDrive.login_with_oauth(access_token)
@ws = session.spreadsheet_by_key("KEY").worksheets[0]
@col = @ws.num_cols + 1
@row = @ws.num_rows + 1
@inputs = inputs
end
def updates
@ws[1, @col] = Time.now.strftime("%Y/%m/%d")
@inputs.each do |key, val|
f = false
1.upto @row do |i|
if @ws[i, 2] == key
@ws[i, @col] = val
f = true
end
end
if !f
puts "#{key} has not found!"
@ws[@row, 2] = key
@ws[@row, @col] = val
end
@ws.save
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment