Skip to content

Instantly share code, notes, and snippets.

@vlucas
Last active December 20, 2015 07:18
Show Gist options
  • Select an option

  • Save vlucas/6091902 to your computer and use it in GitHub Desktop.

Select an option

Save vlucas/6091902 to your computer and use it in GitHub Desktop.
JavaScript bookmarklet to total everyone's hours on a Freshbooks invoice
/**
* Loop over all invoice line items and total up hours/quantity for each person
* @author Vance Lucas, Brightbit, LLC
*/
var people = {};
$('table.invbody-items tr').each(function(index) {
var tr = $(this);
var personParts = tr.find('div.description').text().match(/.*\]([^\:]*):/);
if(personParts) {
var person = personParts[1];
if (people[person] == undefined) {
people[person] = 0;
}
people[person] += parseFloat(tr.find('div.quantity').text());
}
});
/**
* Sort object by key (ECMAScipt 5+ only)
*/
var sortObjectByKey = function(obj) {
var keys = [];
var sorted_keys = Object.keys(obj).sort();
var sorted_obj = {};
for(var i=0; i < sorted_keys.length; i++) {
sorted_obj[sorted_keys[i]] = obj[sorted_keys[i]];
}
return sorted_obj;
};
// Display results
alert(JSON.stringify(sortObjectByKey(people), null, 4));
@vlucas
Copy link
Copy Markdown
Author

vlucas commented Jul 26, 2013

Result of running the script on the page looks like this:
Total Hours Sample

@gregtzar
Copy link
Copy Markdown

For some reason when I use this now (pasting into console) on the edit invoice page I get alerted with an empty object.

@vlucas
Copy link
Copy Markdown
Author

vlucas commented May 2, 2014

@gtczap yes, it must be run from the "View Invoice" screen, not the edit screen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment