Last active
August 29, 2015 14:12
-
-
Save 844196/87687a06b27247824782 to your computer and use it in GitHub Desktop.
一般公開されているGoogleスプレッドシートのURLからCSVを標準出力するスクリプト
This file contains hidden or 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/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