-
-
Save shishi/3786732 to your computer and use it in GitHub Desktop.
GoogleDrive::Spreadsheet sample
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 | |
# coding: utf-8 | |
require 'google_drive' | |
require 'highline/import' | |
require 'active_support/core_ext' | |
require 'pry' | |
def ask_authentication | |
username = ask("Enter your username: ") { |q| q.echo = true } | |
password = ask("Enter your password: ") { |q| q.echo = "*" } | |
[username, password] | |
end | |
class KpiSpreadsheet | |
COLUMNS = { | |
:date => '日付', | |
:user => 'UU', | |
:rcount => '売上件数', | |
:r => '売上', | |
} | |
attr_reader :spreadsheet | |
def initialize(spreadsheet) | |
@spreadsheet = spreadsheet | |
end | |
def url | |
spreadsheet.human_url | |
end | |
def self.open(username, password, url) | |
session = GoogleDrive.login(username, password) | |
spreadsheet = session.spreadsheet_by_url(url) | |
new(spreadsheet) | |
end | |
def self.create(username, password, title) | |
session = GoogleDrive.login(username, password) | |
spreadsheet = session.create_spreadsheet(title) | |
worksheet = spreadsheet.worksheets.first | |
worksheet.update_cells(1, 1, [COLUMNS.values]) | |
worksheet.save | |
new(spreadsheet) | |
end | |
def append_kpi(uu, rcount, r, time = beginning_of_hour) | |
worksheet = spreadsheet.worksheets.first | |
date = time.strftime("%Y-%m-%d %H:%M:%S") | |
worksheet.list.push(COLUMNS[:date] => date, COLUMNS[:user] => uu, COLUMNS[:rcount] => rcount, COLUMNS[:r] => r) | |
worksheet.save | |
end | |
private | |
def beginning_of_hour | |
Time.now.change(:min => 0, :sec => 0, :usec => 0) | |
end | |
end | |
def main | |
username, password = ask_authentication | |
if ARGV[0] | |
sheet = KpiSpreadsheet.open(username, password, ARGV[0]) | |
else | |
sheet = KpiSpreadsheet.create(username, password, 'ほげほげ') | |
end | |
puts "URL: #{sheet.url}" | |
# call append_kpi such as: sheet.append_kpi(12432, 251, 34000) | |
binding.pry | |
end | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment