parseMoney(text, opts) turns currency text into a number you can sum. It strips symbols and currency codes, treats parentheses or a trailing minus as negative, and understands a European decimal comma. It returns null when the text cannot be parsed, so you can flag those rows instead of storing a zero.
When to use
- Bank, PayPal, or Stripe exports that mix
$1,234.56,€1.234,56, and(100.00) - A "Amount raw" column you want to turn into a numeric Amount
- Catching rows a
VALUEformula would silently break on
How to use this snippet
{decimal} is optional. Pass '.' or ',' to force the decimal separator. If you leave it off, the helper looks at the last comma or dot: 1,234.56 is US style, 1.234,56 or 1,23 is European. A lone comma with three digits after it (1,234) is treated as a thousands separator.
/**
* Parse currency text to a number, or null if it cannot be parsed.
* Strips symbols and 3-letter currency codes. Parentheses or a trailing
* minus make the number negative. opts.decimal is '.' or ',' when you
* need to force the decimal separator.
*
* @param {string|number} text
* @param {{decimal?: string}} [opts]
* @return {number|null}
*/
function parseMoney(text, opts) {
opts = opts || {};
if (text == null || text === '') return null;
if (typeof text === 'number') return isFinite(text) ? text : null;
var s = String(text).replace(/\u00a0/g, ' ').trim();
if (!s) return null;
var neg = false;
if (/^\(.*\)$/.test(s)) {
neg = true;
s = s.slice(1, -1).trim();
} else if (/-\s*$/.test(s)) {
neg = true;
s = s.replace(/-\s*$/, '').trim();
} else if (/^\+\s*/.test(s)) {
s = s.replace(/^\+\s*/, '').trim();
} else if (/^-\s*/.test(s)) {
neg = true;
s = s.replace(/^-\s*/, '').trim();
}
s = s.replace(/[A-Za-z]{3}(?=\s|$)|^(?:[A-Za-z]{3}\s*)/g, '');
s = s.replace(/[$€£¥₹₩¢₽₺]/g, '');
s = s.replace(/\s+/g, '');
if (!s) return null;
var decimal = opts.decimal;
if (decimal != null && decimal !== '.' && decimal !== ',') {
throw new Error("parseMoney: decimal must be '.' or ','");
}
if (decimal == null) {
var lastComma = s.lastIndexOf(',');
var lastDot = s.lastIndexOf('.');
if (lastComma !== -1 && lastDot !== -1) {
decimal = lastComma > lastDot ? ',' : '.';
} else if (lastComma !== -1) {
var after = s.length - lastComma - 1;
decimal = (after === 1 || after === 2) ? ',' : '.';
} else {
decimal = '.';
}
}
if (decimal === ',') {
if (/[.]/.test(s) && !/,/.test(s)) return null;
s = s.replace(/\./g, '').replace(',', '.');
} else {
s = s.replace(/,/g, '');
}
if (!/^\d+(\.\d+)?$/.test(s)) return null;
var n = Number(s);
if (!isFinite(n)) return null;
return neg ? -n : n;
}
Example
function fillAmounts() {
var sheet = SpreadsheetApp.getActive().getSheetByName('Payments');
var last = sheet.getLastRow();
if (last < 2) return;
var raw = sheet.getRange(2, 1, last - 1, 1).getDisplayValues();
var out = raw.map(function (r) {
var n = parseMoney(r[0]);
return [n, n === null && String(r[0]).trim() ? 'unparseable' : ''];
});
sheet.getRange(2, 2, out.length, 2).setValues(out); // B amount, C flag
}
Notes
0is a real number. Empty cells returnnullwith no flag in the example, because there is nothing to parse.- Forcing
{decimal: ','}on a US list will misread1,234.56. Use it when a column is consistently European. - This is not locale-aware accounting software. Unusual formats (two decimal marks, footnotes in the cell) return
nullso you can fix them by hand.
Happy Coding!
