I have a cell that contains a number range, say 50-60
that I would like to apply custom number formatting to. Ideally, I'd like to be able to format that to output 50Hz-60Hz
.
The syntax that works for a regular integer is #"Hz"
, but I can't find an in-built way to do this for dashed ranges, and I suspect it isn't possible.
As you suspected, this isn't possible to do.
As Tanaike has said in his above comment, the cell input 50-60
is a string - purely read as text as it contains the -
character. Resultantly, Sheets does not have the functionality to use Number formatting to change the way this is displayed.
Disclaimer: These suggestions are not perfect workarounds and depending on how the data in the cells is processed elsewhere in the sheet, these may not work. They do however provide solutions if you are looking to affect the UI only.
Workaround 1: Using a custom Number Format
You can use the format ##"Hz-"##"Hz"
which will display 50Hz-60Hz
for the example you give, if the input of the cell is 5060
rather than 50-60
. You will however need to change the format to contain three #
characters if the frequency range of the cell goes above 100:
##"Hz-"##"Hz"
will make the number 5060
display as 50Hz-60Hz
###"Hz-"###"Hz"
will make the number 120130
display as 120Hz-130Hz
####"Hz-"####"Hz"
will make the number 14201430
display as 1420Hz-1430Hz
Workaround 2: Using an onEdit(e)
Trigger
If inputting the -
yourself is important, then you can use an onEdit()
Apps Script trigger to change the format of the cell to include the Hz
unit after-the-fact.
For this workaround, I will assume that the column your frequency ranges are in is column C.
From the Tools > Script editor
menu item, you can create a function with the following code:
function onEdit(e) {
if (e.range.getColumn() != 3) {
return;
}
else {
var f = e.value.split("-");
e.range.setValue(f = f[0] + "Hz-" + f[1] + "Hz");
}
}
Make sure to change the value on the line if (e.range.getColumn() != 3)
to be whichever column your frequency ranges are: this example uses the value 3 because the column is assumed to be column C, but column D would be 4, E would be 5, etc.
Save the script with the save icon, press the run button (►), and confirm the authentication of running the script.
This will automatically run whenever a value like 50-60
is inputted into column C, and will change to display 50Hz-60Hz
instead.
Workaround 3: Using a Custom Function
Google Sheets allow you to write custom formulae that work in a similar way to the built in formulae like =SUM()
or =COUNT()
.
Following the same steps as in workaround 2 to open the Script editor, create the following function:
function hertzify(f) {
f = f.split("-");
return f[0] + "Hz-" + f[1] + "Hz";
}
This does a similar thing as workaround 2, but instead of automatically changing the values of whatever is in a specific column, you call the function by entering the following formula in a cell:
=HERTZIFY("50-60")
This will change the cell's display value to 50Hz-60Hz
like before.
You can also use this on other cells; for example if cell C3
has the text 120-130
and in cell D3
you input =HERTZIFY(C3)
, then D3
will display 120Hz-130Hz
.
As the above workarounds either process the cell data as if they are text or require the number to be formatted in a specific way, they might not be perfect workarounds for all situations.
In this case I suggest filing a feature request with Google for the ability to define a number format for a range of values in a specific cell.
You can do this by either following the Help > Report a problem
menu item from the Google Sheets user interface, or make a feature request on Google's Issue Tracker asking to implement this as a feature. The link to the Issue Tracker is here