Use regexmatch with Switch case - google-sheets

I have a list of text on col A and I like to run a switch based on if there is a regexmatch in that cell..
A1 = "https://www.amazon.es/dp/B07PHPXHQS/ref=gw_es_desk_h1_aucc_cr_vd20?pf_rd_p=b1bd6d90-90a7-419a-8a6e-8c943ef52b62&pf_rd_r=XKJEDGQX6TSJ91JF0B6Y"
=switch(A1,REGEXMATCH(A1, "amazon."),"amazon",REGEXMATCH(A1, "lifehacker."),"LFH",REGEXMATCH(A1, "engadget."),"ENG","other")
If the link is amazon.com/whatever it should return "amazon" and so on
I get no error.. but I always get "other" as if regexmatch would not work here.
Any other way to do this?

Try this:
=ifs(
REGEXMATCH(A1,"this"),"that",
REGEXMATCH(A1,"this2"),"that2",
REGEXMATCH(A1,"this3"),"this3",
TRUE,elseGoesHere)
That last line is the ticket don't forget its a new case,value pair so don't forget the preceding coma

It's kind of a category error because in your switch statement you are trying to compare the whole string in A1 to the result of the REGEXMATCH function calls which would actually be TRUE, TRUE and FALSE. To make this work using a switch statement you would need to do something like this:
=iferror(switch(regexextract(A1,"amazon.|lifehacker.|engadget."),"amazon.","amazon","lifehacker.","LFH","engadget.","ENG"),"other")
but I suspect that there are more elegant ways of doing this.

Related

Return TRUE if two conditions are met but one of them has two options

Google sheets issue. I want a formula that will return TRUE or FALSE if two conditions are met. However for one of the conditions there are two possibilities.
IF A2="Pizza OR Banana" AND B2="Food" then return TRUE
How can I go about it? For now I did the following but I am missing one option (banana):
=IF(AND($A2="Pizza"),$B2="Food")
Thank you!
You could use a formula like
=IF(AND(OR(A2="Pizza", A2="Banana"), B2="Food"), TRUE, FALSE)
Edit:
As pointed out by MattKing, you don't need the IF here at all since AND will give you TRUE/FALSE, you can just use:
=AND(OR(A2="Pizza", A2="Banana"), B2="Food")
try:
=((A2="Pizza")+(A2="Banana"))*(B2="Food")
What if I wanted it to not be case sensitive and to work with Food-FOOD-food and other variants?
=AND(REGEXMATCH($A$2,"(?i)^pizza$|^banana$"),REGEXMATCH($B$2,"(?i)food"))
Use REGEXMATCH instead gives you more control on the conditions.
In this example,
the first REGEXMATCH will do non-case-sensitive check on A2 to see if it is a word started with 'p' end with 'a' spell as 'pizza' or started with 'b' end with 'a' spell as 'banana' (if anything is added before or after 'pizza' and 'banana', such as 'ppizzaa', it returns false),
the second REGEXMATCH do non-case-sensitive check on B2 to see if it is any variants which includes 'food' (even something like 'xFooDy' will return true).

Create a function that returns really nothing in Google Sheets

So I would like to create a function or something that returns absolutely nothing in a cell.
When I say nothing, I mean that if the cell before returns an array of value, it can write in this cell and doesn't returns #REF (Cannot expand results).
The idea is that I have a function sort() that get me a list of keys. Then I retrieve the values with a filter function like so :
=FILTER(B$2:B$7, A$2:A$7=D2)
But sometimes the keys (which are dates) can be duplicated, and that makes that the FILTER function with return 2 times 2 rows, creating a #REF error (cannot expand results).
If I create a condition :
=IF(D1<>D2, FILTER(/*...*/), "")
The second cell is empty but I still get the #REF error, because it's not really empty.
Is there a way to make that work ?
to create a function or something that returns absolutely nothing in a cell
try:
=IFERROR(0/0)
or:
=IF(;;)
but what you actually need is:
=UNIQUE(FILTER(B$2:B$7, A$2:A$7=D2))
or:
=INDEX(FILTER(B$2:B$7, A$2:A$7=D2), 1)
or:
=ARRAY_CONSTRAIN(FILTER(B$2:B$7, A$2:A$7=D2), 1, 1)

Return TRUE of OR operation Google Sheets

Is there any way that I can get the true value of an OR operation? Something like this?
=OR(value1, value2, value3) return value that is true
try:
=OR(value1, value2, value3)=TRUE
example:
=OR(A1=1, A1=2, A1=3)=TRUE
Taking this question's title literally, the value of any true Or function is always True. However that's probably not what OP is seeking but there's a fundamental misunderstanding that an Or function can have values. Specifically, this question makes a nonsensical assumption that an OR function can have value1, value2, value3. An Or function contains evaluations that return true or false -- no third value possible.
To illustrate, what value would be returned from this Or formula?
=Or(Not(Isnumber(A1)),len(A1)<5,left(C1,1)="G")
However, I understand that this is probably specific to some customized situation. The solution is that some other formula, functionality is needed. If you're evaluating a single cell (i.e. if cell A1 has any three values), then Erik Tyler's proposal of just using an if statement is your answer
=ArrayFormula(IF(OR(A1={5,10,15}),A1,))
(a less elegant but simplified formula that returns the same would be: =if(Or(A1=5,A1=10,A1=15),A1,)
For finding some true values in a range, there are all kinds of possibilities. However to get you started, you might consider using a filter function combined with boolean logic of (evaluation1)+(evaluation2).
See below formula will list off cell addresses of all true values in this Filter function.
=FILTER(Address(ROW(A:A),COLUMN(A:A),4),(A:A=3)+(A:A=5)+(A:A="Hello"))

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")

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 ;.

Resources