Skip to content

Instantly share code, notes, and snippets.

@844196
Last active August 29, 2015 14:12
Show Gist options
  • Save 844196/87687a06b27247824782 to your computer and use it in GitHub Desktop.
Save 844196/87687a06b27247824782 to your computer and use it in GitHub Desktop.
一般公開されているGoogleスプレッドシートのURLからCSVを標準出力するスクリプト
#!/usr/bin/ruby
#
# 一般公開されているGoogleスプレッドシートのURLからCSVを標準出力するスクリプト
#
# Author:
# 844196 (Masaya Tk)
# License:
# MIT
#
require "rubygems"
require "open-uri"
require "nokogiri"
url = ARGV[0]
charset = nil
html = open(url) do |f|
charset = f.charset
f.read
end
csv = []
doc = Nokogiri::HTML.parse(html, nil, charset)
doc.xpath('//tbody/tr').each do |node|
row = []
range = Range.new(0, node.css('td').count, true)
for num in range do
str = node.css('td')[num].text unless node.css('td')[num].nil?
row << str
end
row = row.map{|i| %Q("#{i}")}
csv << row
end
csv.each do |line|
puts line.join(",")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment