Google Sheets' operators - google-sheets

Do Google Sheets have a || operator like most of the languages?
I'm trying to use it inside a switch function to nest some values.
=SWITCH(G5;"BLACKOLIVES";"BLACK";"JABOTICABA";"BLACK";"MULLBERIES";"BLACK")
this is how I'm doing it now, I want to do it somewhat like whats next
=SWITCH(G5;"BLACKOLIVES"||"JABOTICABA"||"MULLBERIES";"BLACK")
but this statement returns an error and I can't find a ||-like operator in the documentation.
Does anyone have an idea how to solve this?

Maybe you could use something like
=if(regexmatch(G5, "BLACKOLIVES|JABOTICABA|MULLBERIES"),"BLACK",)
In regular expressions the "pipe"-character means 'or'...

an alternative syntax would be:
=IF(OR(G5="BLACKOLIVES", G5="JABOTICABA", G5="MULLBERIES"), "BLACK", )

Related

How to shorten this syntax in Google Sheet?

Can this (Google Sheet) =IFS syntax be improved?
=IFS(and(E42>E38;E42>E34;E42>E30;E42>E26;E42>E22;E42>E18); "Cattleman";
and(E38>E42;E38>E34;E38>E30;E38>E26;E38>E22;E38>E18); "Naturalist";
and(E34>E42;E34>E38;E34>E30;E34>E26;E34>E22;E34>E18); "Farmer";
and(E30>E42;E30>E38;E30>E34;E30>E26;E30>E22;E30>E18); "Carpenter";
and(E26>E42;E26>E38;E26>E30;E26>E34;E26>E22;E26>E18); "Blacksmith";
and(E22>E42;E22>E38;E22>E30;E22>E34;E22>E26;E22>E18); "Miner";
and(E18>E42;E18>E38;E18>E30;E18>E34;E18>E22;E18>E26); "Builder")
And how can I add a default value so that if this syntax returns FALSE it doesn't say #N/A! in the cell, but "No class" or something similar instead (or empty)?
One obvious way would be to replace the ANDs with a MAX. Why? In the first line, if E42 is greater than all of the other cells, it must be greater than the MAX of them. So the condition in this line
E42 > MAX(E38; E34; E30; E26; E22; E18)
which looks much cleaner. Repeat for the other lines.
Trying to simplify it more, the logic of the formula seems to be that depending on which of the cells is the greatest, you choose a particular literal value. There is a function for that! I'd try this (can't test it though without access to your data)
=CHOOSE(
MATCH(
MAX(E42; E38; E34; E30; E26; E22; E18);
{E42; E38; E34; E30; E26; E22; E18});
"Cattleman"; "Naturalist"; "Farmer"; "Carpenter"; "Blacksmith"; "Miner"; "Builder")
wrap it in IFERROR like this:
=IFERROR(IFS(
AND(E42>E38;E42>E34;E42>E30;E42>E26;E42>E22;E42>E18);"Cattleman";
AND(E38>E42;E38>E34;E38>E30;E38>E26;E38>E22;E38>E18);"Naturalist";
AND(E34>E42;E34>E38;E34>E30;E34>E26;E34>E22;E34>E18);"Farmer";
AND(E30>E42;E30>E38;E30>E34;E30>E26;E30>E22;E30>E18);"Carpenter";
AND(E26>E42;E26>E38;E26>E30;E26>E34;E26>E22;E26>E18);"Blacksmith";
AND(E22>E42;E22>E38;E22>E30;E22>E34;E22>E26;E22>E18);"Miner";
AND(E18>E42;E18>E38;E18>E30;E18>E34;E18>E22;E18>E26);"Builder");
"No class")

Joining more than two WHERE statements in Query Language

So I am trying to use a simple QUERY function in Google Sheets where I want to select based on TWO parameters. Simple logic, and documentation says use the AND operator. The problem is I am searching for text via Cell Reference.
So here is my function
=QUERY(A1:D6,"select A where C='" &K1&'"" & "and D='" &K2"'")
Unfortunately it throws up an ERROR. I understand that Cell References that are text based need to be in single quotes (which themselves need to be in double quotes), but I am unable to join two WHERE statements.
What is the right syntax for this?
Very close indeed, please try:
=query(A1:D6,"select A where C='"&K1&"' and D='"&K2&"' ")
Welp! I was missing an concatenation symbol (&) at the end of the final cell reference K2.
=QUERY(A1:D6,"select A where C='" &K1&'"" & "and D='" &K2&"'")

DBAccess format structure of whereWithFormat for like queries

I want to make sure I am formatting the right way the clause like in a query. For example, I have the following code:
int count = [[[MyDBObject query] whereWithFormat:#"url like '%%#%'", mySearchString] count];
My question is: am I using the correct formatting for like clause ?
Thank you and happy coding :)
Because the string formatter hates this we had to adapt and make a special parameter type, so we can identify it as a like parameter. Because string formatting is one part, but then the parameters are then supplied to the query the formatting gets quite screwed up and you have to get quite inventive with the escape characters.
So the thing you need is dbMakeLike()
Example:
whereWithFormat:#"lower(text) LIKE %#“, dbMakeLike(searchString.lowercaseString)
Thanks

Google Spreadsheet long IF statement?

I have this statement:
=if(
F1B!D3="1",50+FLOOR(D2/10,1),
if(F1B!D3="2",40),
if(F1B!D3="3",30),
if(F1B!D3="4",25),
if(F1B!D3="5",20),
if(F1B!D3="6",19),
if(F1B!D3="7",18),
if(F1B!D3="8",17),
if(F1B!D3="9",16),
if(F1B!D3="10",15),
if(F1B!D3="11",14),
if(F1B!D3="12",13),
if(F1B!D3="13",12),
if(F1B!D3="14",11),
if(F1B!D3="15",10),
if(F1B!D3="16",9),
if(F1B!D3="17",8),
if(F1B!D3="18",7),
if(F1B!D3="19",6),
if(F1B!D3="20",5),
if(F1B!D3="21",4),
if(F1B!D3="22",3),
if(F1B!D3="23",2),
if(F1B!D3="24",1));
But GoogleDocs return me "error: Wrong number of arguments to IF"
What I'am doing wrong?
You can't pass infinitely many arguments to IF. There's a single condition, a single "THEN", and a single "ELSE". You need to "nest" your IF statements, where each new IF() in part of the previous IF statement's ELSE. Something like this (abbreviated):
=if(
F1B!D3="1",50+FLOOR(D2/10,1),
if(F1B!D3="2",40,
if(F1B!D3="3",30,
if(F1B!D3="4",25,
if(F1B!D3="5",20,
if(F1B!D3="6",19,
if(F1B!D3="7",18)))))))
Trying to apply too many IFs, far more than necessary:
=IF(F1B!D3=1,50+FLOOR(F1B!D2/10,1),iferror(CHOOSE(F1B!D3-1,40,30,25),25-F1B!D3))
Also do not append ;.

Ruby: Case-Insensitive Array Comparison

Just found out that this comparison is actually case-sensitive..Anyone know a case-insensitive way of accomplishing the same comparison?
CardReferral.all.map(&:email) - CardSignup.all.map(&:email)
I don't think there is any "direct" way like the minus operator, but if you don't mind getting all your results in lowercase, you can do this:
CardReferral.all.map(&:email).map(&:downcase) - CardSignup.all.map(&:email).map(&:downcase)
Otherwise you'll have to manually do the comparison using find_all or reject:
signups = CardSignup.all.map(&:email).map(&:downcase)
referrals = CardReferral.all.map(&:email).reject { |e| signups.include?(e.downcase) }
I'd suggest that reading a reference of Ruby's standard types might help you come up with code like this. For example, "Programming Ruby 1.9" has all methods of the Enumerable object explained starting on page 487 (find_all is on page 489).

Resources