Remove characters in Googlesheets - google-sheets

I want to remove the first and last characters of a text in a cell. I know how to remove just the first or the last with formulas such as =LEFT(A27, LEN(A27)-1) but i want to combine two formulas at the same time for first and last character or maybe there is a formula that I'm not aware of which removes both (first and last characters) at the same time.
I know about Power Tool but i want to avoid using this tool and I'm trying to realize this simply by formulas.

You could use the REGEXREPLACE() function:
=REGEXREPLACE(A27, "^.|.$", "")
The regular expression used here matches:
^. the first character after the start of the string
| OR
.$ the last character of the string

better not to use this but it works too:
=RIGHT(LEFT(A27, LEN(A27)-1), LEN(LEFT(A27, LEN(A27)-1))-1)
=LAMBDA(x, RIGHT(LEFT(A27, x), LEN(LEFT(A27, x))-1))(LEN(A27)-1)
=LAMBDA(x, LAMBDA(y, RIGHT(y, LEN(y)-1))(LEFT(A27, x)))(LEN(A27)-1)
=LAMBDA(x, RIGHT(x, LEN(x)-1))(LEFT(A27, LEN(A27)-1))

Related

Capitalize only words with more than 2 characters on Google Sheets

How can I capitalize only words with more than a specific count of characters? Google Sheets formula PROPER capitalizes all the words without any exclusions. For example, I would like to omit the capitalization of an acronym such as "PC" or "RAM" that is contained within a string in a cell.
Capitalize All and exclude from a list
To be able to PROPER()/Capitalize the input of words and exclude others:
Use this formula, but first we need the excluded list "Acronyms excluded".
=ArrayFormula(IF(
REGEXMATCH(TEXTJOIN("^_~",,UNIQUE($D$2:$D)), UPPER(A2:A))<>TRUE,PROPER(A2:A),A2:A))
Capitalize only words more than 2 charachters long
=ArrayFormula(IF(A2:A="",,
IF(LEN(UPPER(A2:A))<=2,
UPPER(A2:A),PROPER(A2:A))))
I may have been overthinking this by a mile, but try:
Formula in B1:
=INDEX(SUBSTITUTE(TEXTJOIN("",0,IF(LEFT(SPLIT(REGEXREPLACE(A1,"\b(RAM|[A-Za-z]{1,2})\b","|♣$1|"),"|"))="♣",SPLIT(REGEXREPLACE(A1,"\b(RAM|[A-Za-z]{1,2})\b","|♣$1|"),"|"),PROPER(SPLIT(REGEXREPLACE(A1,"\b(RAM|[A-Za-z]{1,2})\b","|♣$1|"),"|")))),"♣",""))
The point here is that \b(RAM|[A-Za-z]{1,2})\b would capture any 1-2 character word made of word-characters between word-boundaries or RAM. Now you can add any exclusion into the alternation of the pattern through concatenating more |. The replacement includes a backreference to the capture group to encapsulate the substring between a delimiter to split on and a leading unique character. The IF() will then check whether or not any element from the resulting SPLIT() needs to be processed with Proper() or not.
Note: Word-boundaries like \b may not be safe when you'd have data like hello-pc and you'd want this to be processed with PROPER(). A small adjustment to the formula is then needed.

Extract substring after '-' character in Google Sheets

I am using the following formula to extract the substring venue01 from column C, the problem is that when value string in column C is shorter it only extracts the value 1 I need it to extract anything straight after the - (dash) no matter the length of the value text in column c
={"VenueID";ARRAYFORMULA(IF(ISBLANK(A2:A),"",RIGHT(C2:C,SEARCH("-",C2:C)-21)))}
There is a much simpler solution using regular expressions.
=REGEXEXTRACT(A1,".*-(.*)")
In case you are no familiar with Regular Expressions what this means is, get me every string of characters ((.*)) after a dash (-).
Example
Reference
REGEXTRACT
Test regular expressions
Cheat sheet for regular expressions
To answer bomberjackets question in the comment of Raserhin:
To select the part of the string before the "-"
=REGEXEXTRACT(A1,"(.*)-.*")
EXAMPLE
example of code
Adding to your original formula. I think if you'd use RIGHT and inside it reverse the order of the string with ARRAY then that may work.
=Right(A1,FIND("-",JOIN("",ARRAYFORMULA(MID(A1,LEN(A1)-ROW(INDIRECT("1:"&LEN(A1)))+1,1))))-1)
It takes string from the right side up to X number of characters.
Number of character is fetched from reversing the text, then finding
the dash "-".
It adds one more +1 of the text as it will take out so it accounts
for the dash itself, if no +1 is added, it will show the dash on
the extracted string.
The REGEX on the other answer works great too, however, you can control a number of character to over or under trim. E.g. if there is a space after the dash and you would like to always account for one more char.

How to convert conditional formatting formula to cell-based formula for cell range?

Here's my next challenge, and it's related to the previous one (found here: This works for one cell - now how can I apply it to a range?).
I've ended up with a godawful ugly formula for conditional formatting, and somehow (perhaps by dumb luck) it seems to work...
=OR(ARRAYFORMULA(IF(ISNUMBER(SEARCH($B$18,D7)),SIGN(SEARCH($B$18,D7)),IF(ISNUMBER(SEARCH(SPLIT($B$19,","),D7)),SEARCH(SPLIT($B$19,","),D7)))))
It returns true for any single target cell (D7 in this example), checking whether it contains either the string in B18 or one of two or more string values, separated by commas, in B19.
As with the previous scenario, I can't work out how to turn this into a formula (array formula?) which I can apply to a range (D3:D12) and count how many cells meet the criteria.
Or maybe the better question is, what would be the correct way to tackle this in preference to my Frankenstein's Monster of a kludged-up formula quoted above!
Any and all insights appreciated :)
Assuming the values in B19 are separated by a comma, followed by a space, try:
=sum(ArrayFormula(--(REGEXMATCH(D3:D12, B18&"|"&SUBSTITUTE(B19, ", ", "|")))))
If there is no space after the comma use "," instead of ", ".
If you want the match to be case-insensitive, try:
=sum(ArrayFormula(--(REGEXMATCH(D3:D12, "(?i)"&B18&"|"&SUBSTITUTE(B19, ", ", "|")))))
See if that works?

Google Spreadsheet - Not calculating numbers with space

I am trying to do a calculation of two cells, where one of them contains a number like this: 1 250.
If the number is written like that, and not 1250, then I cannot get the spreadsheet to do any calculations with it. Google suddenly do not treat it as a legit number anymore.
Why not just type 1250 instead of 1 250?
Well, I am getting the cell values from a html import function.
Any good advice on how to get around this?
Try something like this:
=Substitute(A2," ","")
In this formula, A2 is a cell. You are finding any spaces in that cell and then replacing it with a "non-space".
Use the substitute function to transform your number before using it in a formula. For instance, let's say you wanted to multiple F8 by 2, but F8 may contain spaces. You would then do:
=substitute(F8, " ","") * 2
Substitute didn't work form me. But these steps did:
Select one or several columns of data
Press Ctrl + H to get the "Find and Replace" dialog
Make sure "Search using regular expressions" is checked ✅
Enter \s to the "Find" field, and leave "Replace with" empty
Click on the "Replace all" button
Explanation:
\s is a regular expression matching any kind of whitespace character. There may have been some other kind of whitespace in my spreadsheet, not a regular " " (space) character, and that's why regex worked for me, while SUBSTITUTE() didn't.
I've also tried the REGEXREPLACE(A2, "\s", "") function, but it didn't seem to to anything in my case.

extract number from cell in openoffice calc

I have a column in open office like this:
abc-23
abc-32
abc-1
Now, I need to get only the sum of the numbers 23, 32 and 1 using a formula and regular expressions in calc.
How do I do that?
I tried
=SUMIF(F7:F16,"([:digit:].)$")
But somehow this does not work.
Starting with LibreOffice 6.4, you can use the newly added REGEX function to generically extract all numbers from a cell / text using a regular expression:
=REGEX(A1;"[^[:digit:]]";"";"g")
Replace A1 with the cell-reference you want to extract numbers from.
Explanation of REGEX function arguments:
Arguments are separated by a semicolon ;
A1: Value to extract numbers from. Can be a cell-reference (like A1) or a quoted text value (like "123abc"). The following regular expression will be applied to this cell / text.
"[^[:digit:]]": Match every character which is not a decimal digit. See also list of regular expressions in LibreOffice
The outer square brackets [] encapsulate the list of characters to search for
^ adds a NOT, meaning that every character not included in the search list is matched
[:digit:] represents any decimal digit
"": replace matching characters (every non-digit) with nothing = remove them
"g": replace all matches (don't stop after the first non-digit character)
Unfortunately Libre-Office only supports regex in find/replace and in search.
If this is a once-only deal, I would copy column A to column to B, then use [data] [text to columns] in B and use the - as a separator, leaving you with all the text in column B and the numbers in column C.
Alternatively, you could use =Right(A1,find("-",A1,1)+1) in column B, then sum Column C.
I think that this is not exactly what do you want, but maybe it can help you or others.
It is all about substring (in Calc called [MID][1] function):
First: Choose your cell (for example with "abc-23" content).
Secondly: Enter the start length ("british" --> start length 4 = tish).
After that: To print all remaining text, you can use the [LEN][2] function (known as length) with your cell ("abc-23") in parameter.
Code now looks like this:
D15="abc-23"
=MID(D15; 5; LEN(D15))
And the output is: 23
When you edit numbers (in this example 23), no problem. However, if you change anything before (text "abc-"), the algorithm collapses because the start length is defined to "5".
Paste the string in a cell, open search and replace dialog (ctrl + f) extended search option mark regular expression search for ([\s,0-9])([^0-9\s])+ and replace it with $1
adjust regex to your needs
I didn't figure out how to do this in OpenOffice/LibreOffice directly. After frustrations in searching online and trying various formulas, I realised my sheet was a simple CSV format, so I opened it up in vim and used vim's built-in sed-like feature to find/replace the text in vim command mode:
:%s/abc-//g
This only worked for me because there were no other columns with this matching text. If there are other columns with the same text, then the solution would be a bit more complex.
If your sheet is not a CSV, you could copy the column out to a text file and use vim to find/replace, and then paste the data back into the spreadsheet. For me, this was a lot less frustrating than trying to figure this out in LibreOffice...
I won't bother with a solution without knowing if there really is interest, but, you could write a macro to do this. Extract all the numbers and then implement the sum by checking for contained numbers in the text.

Resources