How to convert a string to camelcase in Google Spreadsheet formula - google-sheets

Trying to create a formula to turn a string of words separated by spaces into camelcase

Much smaller version:
=SUBSTITUTE(PROPER(TRIM(A1))," ","")
We just use PROPER to upper case and TRIM and SUBSTITUTE to remove spaces.
If we want lowerCamelCase,
By just REPLACEing the first character with lower case, We have:
=REPLACE(SUBSTITUTE(PROPER(TRIM(A1))," ",),1,1,LEFT(LOWER(TRIM(A1))))
Using REGEX:
=REGEXREPLACE(REGEXREPLACE(PROPER(A1),"\s*",),"^(\w)",LEFT(LOWER(TRIM(A1))))
=LOWER(LEFT(TRIM(A1)))&REGEXREPLACE(PROPER(TRIM(A1)),"(\w|\s)(\w*)","$2")

This should work:
=JOIN("",ArrayFormula(UPPER(LEFT(SPLIT(A3," ")))&LOWER(MID(SPLIT(A3," "),2,500))))
or to be more precise:
=JOIN("",ArrayFormula(UPPER(LEFT(SPLIT(A3," ")))&LOWER(REGEXEXTRACT(SPLIT(A3," "),".(.*)"))))

To do this the following formula works (where A3 is the cell)
tl;dr:
=IF(IFERROR(FIND(" ",A3)), CONCAT(SUBSTITUTE(LEFT(LOWER(A3), FIND(" ", A3)), " ", ""), SUBSTITUTE(PROPER(SUBSTITUTE(A3, LEFT(A3, FIND(" ", A3)), "")), " ", "")), LOWER(A3))
Annotated:
=IF( // if a single word
IFERROR( // test if NOT an error
FIND( // looking for a space
" ",
A3
)
),
CONCAT( // concat the first word with the rest
SUBSTITUTE( // remove the space
LEFT( // left of the find
LOWER( // lowercase the string
A3
),
FIND( // find the space in the string
" ",
A3
)
),
" ",
""
),
SUBSTITUTE( // remove spaces
PROPER( // convert string to capitals
SUBSTITUTE( // remove first word
A3,
LEFT( // left of the find
A3,
FIND( // find first space
" ",
A3
)
),
""
)
),
" ",
""
)
),
LOWER( // lowercase rest of the word
A3
)
)

Taking cue from all answers, sharing what I did as no one has mentioned it in the following exact way (transformation e.g. created_at => createdAt)
=REPLACE(SUBSTITUTE(PROPER(A5), "_", ""), 1, 1, LOWER(LEFT(A5, 1)))
The above formula is
Easy => uses simple to understand popular functions
Correct => works on empty cells too
Efficient => parses the "whole" string only ONCE
Most of the work is done using PROPER function except the last part to replace first character with its lowercase version

Related

Google sheets get empty cells next to each other

Link: https://docs.google.com/spreadsheets/d/1hCG6jjGSGWMn5kBkDfcoTJkZ2eCC5SRvHWMFTBp_6OE/edit?usp=sharing
I want to return "Yes" or "No" results in column N when there are empty cells next to each other in the range B:M
And column O counts the number of times there are empty cells next to each other.
Thank you.
use in O2:
=INDEX(LEN(SUBSTITUTE(FLATTEN(QUERY(TRANSPOSE(IF(REGEXMATCH(TRIM(
SPLIT(FLATTEN(QUERY(TRANSPOSE(IF(B2:M5="", "×", "¤")),,9^9)),
"¤")), "× ×"), 1, )),,9^9)), " ", )))
use in N2:
=INDEX(IF(O2:O=0, "No", "Yes"))

Query/filter out blank non-specified columns from range

I would like this multiple-criteria query not to show empty columns.
=QUERY({H3:M11}, "select * WHERE
"&TEXTJOIN(" and ", 1,
IF(C3<>"", "Col2 = "&C3&"", ),
IF(B3<>"", "Col3 = '"&B3&"'", )), 1)
Besides, I would also like to know if it's possible to filter it outside a query formula. Currently, I have this formula made by #player0 which is excluding columns with values greater than 0, but I didn't manage to make it work for text.
=FILTER(FILTER(H3:M11, LEN(TRIM(QUERY(IFERROR(1/(1/H4:M11)),,9^9)))>0), {9;
LEN(TRIM(FLATTEN(QUERY(TRANSPOSE(IFERROR(1/(1/H4:M11))),,9^9))))}>0)
Link to the question where this filter formula was found.
Here's the sheet.
Thanks a lot.
try:
=ARRAYFORMULA(QUERY({H3:K11,
FILTER(L3:M11, TRIM(QUERY(L4:M11,,9^9))<>"")},
"where "&TEXTJOIN(" and ", 1,
IF(C3<>"", "Col2 = "&C3&"", ),
IF(B3<>"", "Col3 = '"&B3&"'", )), 1))

Array formula to join header values in a Google Sheet when row values equal "yes"

I saw this question but was not able to get it to work for me. Since it is already answered and from an year ago I felt it best to post a new question.
I have a sample sheet at: https://docs.google.com/spreadsheets/d/1EqNfTWcNFgkv2gdovnH7JpqU9V2HG5Vkoup8bhNqI7k/edit#gid=0
In F1 I want an array formula that will join the values of B1:E1 if B2:E = "yes".
Column G has the expected output.
This is what I came up with but it's giving error:
={
"output";
ARRAYFORMULA(
IF(
A2:A = "",
,
IF(
B2:E = "yes",
TEXTJOIN(", ", TRUE, B1:E1),
)
)
)
}
Try this out
=index(substitute(trim(transpose(query(transpose(if(B2:E<>"yes",,B1:E1)),,9^9)))," ",", "))
If the header contain spaces, use:
=index(substitute(substitute(trim(transpose(query(transpose(if(B2:E<>"yes",,substitute(B1:E1," ","❄️"))),,9^9)))," ",", "),"❄️"," "))
use:
=INDEX(REGEXREPLACE(TRIM(FLATTEN(QUERY(TRANSPOSE(
IF(B2:E="yes", B1:E1&",", )),,9^9))), ",$", ))

Google sheets - Extract numbers with their units of measurment

I want a function that can extract numbers with their units of measurment from a text.
For example in A2 i have:
This box weights 5kg and the other box weights 10 kg.
So i want a function that will return:
5kg 10kg
NOTE: I want the function to work with any unit of measurment, not just "kg".
I am a begginer in google sheets so it would be really helpful if you could provide me with a working function.
You can use this sample custom function that extracts words that starts with a number followed by a character/s.
/**
* #customfunction
*/
function EXTRACTMEASUREMENT(input) {
// match all words which starts with a number
var result = input.match(/\d+[a-zA-Z]+\S*/g)
// combine array into a string separated with spaces
result = result.join(' ');
// Remove special characters(except whitespace) in the string
result = result.replace(/[^\/a-zA-Z0-9\s]/g, '')
return result;
}
Output:
Limitations:
Measurements with spaces between the value and the unit cannot be detected. (See result in cell A5 when space exist in 10 kg.)
Regardless whether the character/s after the number is a valid unit or not, it will be extracted. (See result in cell A5 where 20yy is not a valid measurement unit)
If you want to exempt particular characters not to be removed, you can add them in the braces [^\/a-zA-Z0-9\s] (example / will not be removed).
Note:
This can be improved if you can list valid measurement units that should be supported.
Try
=arrayformula(substitute(transpose(query(flatten(split(
REGEXREPLACE(A1,"([0-9.,/]+[ ]{0,1}[a-z1-3/.\-""]+)","♣♦$1♣")
,"♣")),"select * where Col1 like '♦%' ")),"♦",""))
One more option:
=ArrayFormula(IF(LEN(A:A),
SPLIT(
REGEXREPLACE(
REGEXREPLACE(A:A,"("&
REGEXREPLACE(
REGEXREPLACE(
REGEXREPLACE(A:A,"(\d+[.,/]*\d*(?:\w+|\s\w+)[\""./\-\w+]*)",""),
"\s+\.","|\\."),
"\s+","|")
&")",""),
"(\d)\s","$1")
," ",,1)
,))
try:
=INDEX(SPLIT(FLATTEN(QUERY(TRANSPOSE(IFERROR(SUBSTITUTE(
REGEXEXTRACT(SPLIT(REGEXREPLACE(A1:A, "(\d+.\d+|\d+)", "×$1"), "×"),
TEXTJOIN("|", 1, {"\d+.\d+ ","\d+.\d+","\d+ ","\d+"}&
SORT({"nm";"mm";"cm";"dm";"km";"m";"t";"kg";"dg";"g";"l";"ml";"dl"},
LEN({"nm";"mm";"cm";"dm";"km";"m";"t";"kg";"dg";"g";"l";"ml";"dl"}), 0))),
" ", ))),,9^9)), " "))
update:
all in one cell:
=INDEX(TRIM(FLATTEN(QUERY(TRANSPOSE(IFERROR(SUBSTITUTE(
REGEXEXTRACT(SPLIT(REGEXREPLACE(A1:A, "(\d+.\d+|\d+)", "×$1"), "×"),
TEXTJOIN("|", 1, {"\d+.\d+ ","\d+.\d+","\d+ ","\d+"}&
SORT({"nm";"mm";"cm";"dm";"km";"m";"t";"kg";"dg";"g";"l";"ml";"dl"},
LEN({"nm";"mm";"cm";"dm";"km";"m";"t";"kg";"dg";"g";"l";"ml";"dl"}), 0))),
" ", ))),,9^9))))

Easier Way to VLOOKUP to the left w/ IMPORTRANGE?

I'm using a formula in Google Sheets that combines vlookup and importrange to look to the left in a separate spreadsheet. Here's the formula...
={
"Staff Name";
arrayformula(
iferror(
VLOOKUP(
A2:A,
{
IMPORTRANGE("1Ri5k_WNei8X9XJVWxrblSuEKS3680YcH3LobflvmzuALcsY","Directory!D2:D"),
IMPORTRANGE("1Ri5k_WNei8X9XJVWxrblSuEKS3680YcH3LobflvmzuALcsY","Directory!C2:C")
},
2,
0
),
""
)
)
}
But, let's say I need to look 6 columns to the left, instead of just 2. Do I need to do an importrange for every single column like the above formula? Or is there an easier way?
In other words, will my formula need to change to look like this below?
={
"Staff Name";
arrayformula(
iferror(
VLOOKUP(
A2:A,
{
IMPORTRANGE("1Ri5k_WNei8X9XJVWxrblSuEKS3680YcH3LobflvmzuALcsY","Directory!H2:H"),
IMPORTRANGE("1Ri5k_WNei8X9XJVWxrblSuEKS3680YcH3LobflvmzuALcsY","Directory!G2:G"),
IMPORTRANGE("1Ri5k_WNei8X9XJVWxrblSuEKS3680YcH3LobflvmzuALcsY","Directory!F2:F"),
IMPORTRANGE("1Ri5k_WNei8X9XJVWxrblSuEKS3680YcH3LobflvmzuALcsY","Directory!E2:E"),
IMPORTRANGE("1Ri5k_WNei8X9XJVWxrblSuEKS3680YcH3LobflvmzuALcsY","Directory!D2:D"),
IMPORTRANGE("1Ri5k_WNei8X9XJVWxrblSuEKS3680YcH3LobflvmzuALcsY","Directory!C2:C")
},
6,
0
),
""
)
)
}
Thanks for your help!

Resources