Skip to content

Instantly share code, notes, and snippets.

@mcoms
Created January 21, 2019 12:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcoms/757e329dab07fed7f5b9c926b904a50d to your computer and use it in GitHub Desktop.
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.
Issue Date,Due Date,Name,Client,Value,Status,Paid On
...
<!-- Copy the HTML table element from this page: https://youraccount.freeagentcentral.com/invoices (select to show all invoices -->
# 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