Created
February 13, 2012 15:20
-
-
Save bycoffe/1817556 to your computer and use it in GitHub Desktop.
Create a Google Spreadsheet from an electronic FEC filing, using Fech
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
require 'fech' | |
require 'google_spreadsheet' | |
module Fech | |
class Filing | |
# Create a Google Spreadsheet from a filing. | |
# Note that for large filings, this will fail because of the limit | |
# Google Spreadsheets places on the number of cells. | |
def upload_to_google_spreadsheet(email, password, title=nil) | |
session = GoogleSpreadsheet.login(email, password) | |
title ||= "#{summary.committee_name} - #{summary.report_code} - #{summary.date_signed}" | |
spreadsheet = session.create_spreadsheet(title) | |
# Create an ordered list of unique field types | |
form_types = [] | |
# And a hash of {form_type: [rows]} | |
filing_data = {} | |
each_row do |row| | |
form_type = row[0] | |
form_types << form_type unless form_types.include?(form_type) | |
filing_data[form_type] = [] unless filing_data.include?(form_type) | |
filing_data[form_type] << row | |
end | |
form_types.each_with_index do |form_type, idx| | |
form_headers = map_for(form_type) | |
# Create the worksheet for this form type | |
worksheet = spreadsheet.add_worksheet(title=form_type, max_rows=filing_data[form_type].length, max_cols=form_headers.length) | |
form_headers.each_with_index do |hdr, idx| | |
# row 1, cell idx+1 (cells are 1-indexed) | |
worksheet[1, idx+1] = hdr | |
end | |
filing_data[form_type].each_with_index do |row, row_idx| | |
row_idx += 2 # add 1 for 1-indexed rows, add 1 to start 1 row below header | |
row.each_with_index do |val, cell_idx| | |
cell_idx += 1 # Add 1 for 1-indexed cells | |
worksheet[row_idx, cell_idx] = val | |
end | |
end | |
worksheet.save | |
end | |
# Remove the first worksheet (which is created by default) | |
spreadsheet.worksheets[0].delete | |
spreadsheet.human_url | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment