Reputation: 63
I would like to programatically apply a custom number format so that I can use custom currency symbols in my cells (such as for cryptocurrency) and keep them as numbers so that I can do math with them or chart the values.
I have looked at the following options already:
If I use the spreadsheet function "CONCATENATE", I get a string and cannot do math or chart with the final value.
Spreadsheet function TEXT() can get the number format to display how I want but not all custom currency symbols "work" - the following for instance turns the value into a text string and I cannot do math/charts with it.
Ex: =TEXT(SUM(C5:C7),"Ƀ#,##0.00000000")
reference on TEXT(): https://support.google.com/docs/answer/3094139?hl=en
format(cell)
, and use JS to add any text strings to the value, it's no longer a number type.Ex: function GBP(amt) { return '£' + parseFloat(amt).toFixed(2)); }
This won't work either as resulting string is not a number:
function BTC(amt) { return parseFloat('Ƀ' + parseFloat(amt).toFixed(2)); }
setNumberFormat(format)
cannot be used in a custom function to be called via spreadsheet formulas.
Copying another cells formatting? I also see this as a workaround but it looks really not efficient if I have to copy and paste formatting for every cell I want the proper formatting : Applying "Automatic" number formatting
Copying the values into another column just for conditional formatting and the original numbers only range used for maths/charting. This is more a workaround a not a clean solution.
In short, I just want to add custom currency symbols to my numbers and still be able to do math and charts with them. Does anyone know a working, direct way to do so via Google Apps Scripts or spreadsheet functions/menus?
Upvotes: 1
Views: 3762
Reputation: 38284
To programmatically apply a currency format to a single number and to keep it as a number in order to do arithmetic operations with it use setNumberFormat(string).
NOTES:
Example:
The setNumberFormat is a partially implemented Google Apps Script function that currently applies Pound sterling currency format ([$£-809]#,##0.00
) or a default format (#,##0.00
) . The test function is used to call it assigning 'Pound sterling' to the format parameter.
function test(){
setNumberFormat('Pound sterling');
}
function setNumberFormat(format) {
var range = SpreadsheetApp.getActiveRange();
var numberFormat = '';
try {
switch (format){
case 'Pound sterling':
numberFormat = '[$£-809]#,##0.00';
break;
default:
numberFormat = '#,##0.00';
break;
}
range.setNumberFormat(numberFormat);
} catch (e){
throw new Error('There was an error: ' + e);
}
}
Related Q&A
References
Upvotes: 4