Last active
December 20, 2015 07:18
-
-
Save vlucas/6091902 to your computer and use it in GitHub Desktop.
JavaScript bookmarklet to total everyone's hours on a Freshbooks invoice
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
| /** | |
| * 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)); |
Author
For some reason when I use this now (pasting into console) on the edit invoice page I get alerted with an empty object.
Author
@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
Result of running the script on the page looks like this:
