Created
October 2, 2017 03:11
-
-
Save willjobs/2b16c9464f1975421e4cf21895706c06 to your computer and use it in GitHub Desktop.
Custom function for Google sheets to sum all of the dollar amounts written in notes in a given cell. If given a range of cells, will calculate the sums for each cell independently and output them below the cell with the formula.
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
function sumdollars(input) { | |
if(input.map) { | |
return input.map(sumdollars); | |
} else { | |
if(input.length === 0) {return 0;} | |
if(typeof input === 'number') {return input;} | |
var strArr = input.split('$').slice(1); | |
var mysum = 0; | |
for(i=0; i < strArr.length; i++) { | |
mysum += parseFloat(strArr[i].split(/[^0-9\.]+/)[0]); | |
} | |
return mysum; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Google Sheets Custom Function: Sum Dollars
Custom function for Google sheets to sum all of the dollar amounts written in notes in a given cell. If given a range of cells, will calculate the sums for each cell independently and output them below the cell with the formula.
How to use
Step 1: Open a Google Sheets file (or create a new one). Then go to Tools --> Script Editor.
Step 2: Paste the code below.
Step 3: Use the formula in a cell in your spreadsheet by supplying the function with another cell (or range of cells). If a range of cells is supplied to the function, the function will output a value for each cell in the range (as documented here: https://developers.google.com/apps-script/guides/sheets/functions#optimization).
Code
Examples
Example 1
Cell A1:
Cell B1:
=sumdollars(A1)
Result:
Example 2
Cell A1:
Cell A2:
Cell A3:
Cell B1:
=sumdollars(A1:A3)
Result: