Return TRUE of OR operation Google Sheets - 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"))

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

Directly return a table entry from a (simplest) function in Lua

I wanted to write the simplest possible function which let me return the desired value in a nameless table and, ideally, it should be something like this:
function RL_MyTool:Version(n)
return {"0.4.0", "20221003-0230", "13.5.5"}[n]
end
But, of course, that's not allowed in Lua...
So, off the top of my head, I can think on these two other possibilities:
1:
function RL_MyTool:Version(n)
local t = {"20221003-0230", "13.5.5"}
return t[n] or "0.4.0"
end
2:
function RL_MyTool:Version(n)
local n, t = n or 1, {"0.4.0", "20221003-0230", "13.5.5"}
return t[n]
end
Both of them slightly different from each other but doing the same, counting with the advantage of returning a default value if no argument is given, which is good. BUT... Do you think I could still have a possibility of writing it like in the very simplest fashion way above? Basically, what I'd like is not even have to use a single variable or table declaration along the function but still let me return the specified table entry when called.
Well, that's all. Of course if it's finally not possible (as I'm afraid) it won't be the end of the world 🙄, but I wanted to be sure I wasn't missing any Lua trick or something that let me do it more like I firstly imagined... Thanks!
P.S. Oh, I don't see how, but of course if it could be achieved without the necessity of even using a table at all, that would be equally valid or even better.
EDIT: BTW, for the record and based in #Piglet (great!) answer, I got to reduce it even more this way:
function RL_MyTool:Version(n)
return ({"0.4.0", "20221003-0230", "13.5.5"})[n or 1]
end
Improving code usability/maintenance a bit at the same time by avoiding duplicated values... Kind of a win-win-win 😁
Just put the table in parenthesis.
function RL_MyTool:Version(n)
return ({"0.4.0", "20221003-0230", "13.5.5"})[n] or "0.4.0"
end
But what is the purpose of this? Code should be easy to read and easy to work on. There is absolutely no reason to not use a local table. You don't have to pay a dollar for each line of code.

Lua: Sort table of numbers with multiple dots

I have a table of strings like this:
{
"1",
"1.5",
"3.13",
"1.2.5.7",
"2.5",
"1.3.5",
"2.2.5.7.10",
"1.17",
"1.10.5",
"2.3.14.9",
"3.5.21.9.3",
"4"
}
And would like to sort that like this:
{
"1",
"1.2.5.7",
"1.3.5",
"1.5",
"1.10.5",
"1.17",
"2.2.5.7.10",
"2.3.14.9",
"2.5",
"3.5.21.9.3",
"3.13",
"4"
}
How do I sort this in Lua? I know that table.sort() will be used, I just don't know the function (second parameter) to use for comparison.
Given your requirements, you probably want something like natural sort order. I described several possible solution as well as their impact on the results in a blog post.
The simplest solution may look like this (below), but there are 5 different solutions listed with different complexity and the results:
function alphanumsort(o)
local function padnum(d) return ("%03d%s"):format(#d, d) end
table.sort(o, function(a,b)
return tostring(a):gsub("%d+",padnum) < tostring(b):gsub("%d+",padnum) end)
return o
end
table.sort sorts ascending by default. You don't have to provide a second parameter then. As you're sorting strings Lua will compare the strings character by character. Hence you must implement a sorting function that tells Lua which comes first.
I just don't know the function (second parameter) to use for
comparison.
That's why people wrote the Lua Reference Manual
table.sort (list [, comp])
Sorts the list elements in a given order, in-place, from list1 to
list[#list]. If comp is given, then it must be a function that
receives two list elements and returns true when the first element
must come before the second in the final order, so that, after the
sort, i <= j implies not comp(list[j],list[i]). If comp is not given,
then the standard Lua operator < is used instead.
The comp function must define a consistent order; more formally, the
function must define a strict weak order. (A weak order is similar to
a total order, but it can equate different elements for comparison
purposes.)
The sort algorithm is not stable: Different elements considered equal
by the given order may have their relative positions changed by the
sort.
Think about how you would do it with pen an paper. You would compare each number segment. As soon as a segment is smaller than the other you know this number comes first.
So a solution would probably require you to get those segments for the strings, convert them to numbers so you can compare their values...

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

Username using * in the criteria countif

i m searching sum of class of each student using countif formula, but any student have unique username like A*di (in the image) and so the calculation is false. And any other student using username like </John>, and 'Angel. and make calculation false
Formula: =COUNTIF('Data Asli'!$A:$A,$A$2)
Use SUMPRODUCT(--EXACT(..)) to run an exact, case-sensitive comparison that ignores wildcards:
=SUMPRODUCT(--EXACT('Data Asli'!$A:$A,$A2))
How it works:
EXACT(Value1, Value2) will return TRUE or FALSE, depending on whether the 2 values exactly match (same capitals, no wildcards, et cetera)
-- will convert TRUE/FALSE into 1/0
SUMPRODUCT(Array1[,Array2]) will run down the arrays, multiply the numbers together, then add them. It also forces many functions to both treat a Range as an array, and output an array.
So, as an example, the steps run like this:
=SUMPRODUCT(--EXACT(A1:A5, A2))
=SUMPRODUCT(--EXACT({Value1,Value2,Value3,Value4,Value2}, Value2))
a.k.a.
=SUMPRODUCT(--{EXACT(Value1,Value2),EXACT(Value2,Value2),EXACT(Value3,Value2),EXACT(Value4,Value2),EXACT(Value2,Value2)})
=SUMPRODUCT(--{FALSE,TRUE,FALSE,FALSE,TRUE})
=SUMPRODUCT({0,1,0,0,1})
=2

Resources