Created
January 21, 2019 12:41
-
-
Save mcoms/757e329dab07fed7f5b9c926b904a50d to your computer and use it in GitHub Desktop.
Takes the FreeAgent invoice table and generates a CSV file from it. Makes it easier to work with the data in a spreadsheet when calculating late payment fees.
We can make this file beautiful and searchable if this error is corrected: It looks like row 2 should actually have 7 columns, instead of 1 in line 1.
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
Issue Date,Due Date,Name,Client,Value,Status,Paid On | |
... |
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
<!-- Copy the HTML table element from this page: https://youraccount.freeagentcentral.com/invoices (select to show all invoices --> |
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
# frozen_string_literal: true | |
require 'bundler/inline' | |
require 'csv' | |
gemfile do | |
source 'https://rubygems.org' | |
gem 'nokogiri', '~> 1.10.1' | |
gem 'pry' | |
end | |
class String | |
def squish! | |
gsub!(/\A[[:space:]]+/, '') | |
gsub!(/[[:space:]]+\z/, '') | |
gsub!(/[[:space:]]+/, ' ') | |
self | |
end | |
end | |
fragment = Nokogiri::HTML.fragment(File.read('late_invoices.html')) | |
invoice_rows = fragment.css('table#invoices_list tbody tr').select { |tr| tr['id']&.start_with? 'invoice_' } | |
CSV.open('late_invoices.csv', 'wb') do |csv| | |
csv << ['Issue Date', 'Due Date', 'Name', 'Client', 'Value', 'Status', 'Paid On'] | |
invoice_rows.each do |invoice_row| | |
issue_date, due_date, name, client, value, status, = invoice_row.css('td') | |
status_text, paid_on = status.text.strip.squish!.split(' – ') | |
csv << [issue_date.text.strip, due_date.text.strip, name.text.strip, client.css('a:first-child').text.strip, value.text.strip, status_text, paid_on] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment