proper IF statement issue causing parse error - if-statement

I am searching for a formula to combine three things. To capture the value of Cell A into cell B. If it has has a number then it should capture that number. If the value of cell A1 is - then it should change to blank in cell B1 and if it has a blank cell in A1 Then it should be blank in B1.
I have tried the formula in google sheet with IF, And OR combining but it is giving an error.
=if(OR(A1="-"," "),(A1=" ", " "))
=if(And(A1="-"," "),(A1=" ", " "))
=IF(A1="-"," ",IF(A1="","",IF(A1=" "," ")))
The expected results are giving an error or it gives the answer as false.

You should take a look at IF documentation to understand it better, but the main thing here is to remember that IF is IF(logical_expression, value_if_true, value_if_false)
The best option here would be to go with player0 solution, because it covers up almost every case: =IF(ISNUMBER(A1), A1, )
But just to help you out on understanding what was wrong with your formulas, even though those solutions don't check if A1 is a number:
=if(OR(A1="-"," "),(A1=" ", " "))
The correct way would be: =if( OR(A1="-",A1=" "), " ", A1)
=if(And(A1="-"," "),(A1=" ", " "))
This one is actually conceptually wrong, because you want to check if A1 = "-" AND A1 =" ", which is impossible and will never be true, because a cell can't be equal to "-" and " " at the same time.
=IF(A1="-"," ",IF(A1="","",IF(A1=" "," ")))
The correct way would be: =IF(A1="-"," ",IF(A1="","",IF(A1=" "," ",A1)))

that is not how you should use IF function.
If the value of cell A1 is - then it should change to blank in cell B1 and if it has blank cell in A1 Then it should be blank in B1
which could be solved like this in B1 cell:
=IF(ISNUMBER(A1), A1, )
and arrayformula of that would be:
=ARRAYFORMULA(IF(ISNUMBER(A1:A*1), A1:A, ))

Related

How can I fix a for loop that checks dictionary values?

I know it may sound like a newbie question, but i'm having a hard time trying to make a loop work.
I'm using Julia language to create a simple regular expressions that checks if a telephone number is valid and return the estate of the number according to the code area. Here are the details:
1-User enters the phone number;
2-Regex tests if it's a valid number;
3-The area code is parsed and checked if present in the dictionary values. If yes, then it should return the key owning that value. Otherwise a simple message saying that area code doesn't exist should be printed.
The problem is: the loop goes for as long as possible, printing if the value is available or not everytime it checks for it in the dictionary. A break wouldn't help much: as soon as it got the first check, it would simply stop.
Then ofc I noticed I had to take the final print statement out of the loop and even maybe assign a variable to get the value copied, but it still overwrites with the very last "Number doesn't exist" result.
How can I rewrite this code so it works?
regexTel = r"^(\+55)?[\s]?\(?(\d{2})?\)?[\s-]?(9?\d{4}[\s-]?\d{4})$"
areaCode = Dict("City A"=> [68],
"City B"=> [82], [...] (and so on)
print("Type the phone number:\n")
telNum = readline()
validTel = (match(regexTel, telNum))
fnlAreaCd = parse(Int32, validTel[2])
for (cityCode, availbCode) in areaCode
if fnlAreaCd in availbCode
println("Phone number: ", validTel[3], "\n",
"Area code: ", fnlAreaCd, "\n",
"City: ", cityCode)
else
println("Area code doesn't exist")
end
end
Your usage of Dict seems strange to me. I think you should use it the other way around.
I believe that the area codes are unique, then the varaible areaCode would be like this:
areaCode = Dict(68 => "City A", 82 => "City B", ...) # do you need to wrap them in an array?
It'll allow you to write much simplier code:
if haskey(areaCode, fnlAreaCd)
cityCode = areaCode[fnlAreaCd]
println("Phone number: ", validTel[3], "\n",
"Area code: ", fnlAreaCd, "\n",
"City: ", cityCode)
else
println("Area code doesn't exist")
end
The matching city won't necessarily be the first entry in the dictionary, but you have instructed the code to say it doesn't exist any time it finds a non-match! Try this instead of your bare loop:
function lookup_areacode(acode)
code_exists = false
for (cityCode, availbCode) in areaCode
if acode in availbCode
code_exists = true
println("Phone number: ", tel, "\n",
"Area code: ", acode, "\n",
"City: ", cityCode)
end
end
if !code_exists
println("Area code doesn't exist")
end
end
lookup_areacode(fnlAreaCd)
You may want to move the dictionary inside the function for neatness.

How to create result from multiple IF results neatly in Google Sheets

I want to combine multiple results of if statement into a form of a sentence.
Code:
=CONCAT("Fail column", IF($T3="No", " T", "")& IF($U3="No", ", U", "") & IF($W3<7, ", W", "") & IF($X3>3, ", X", "") & IF($AE3="No", ", AE", "") & IF($AF3="No", ", AF", ""))
Sample data :
If the first statement returns blank, the next statement would not show the comma at the beginning. And let say all pass, they would be shown as "Yes".
My expected output can be:
Fail column T, U, W, X, AE, AF
Fail column U, W, X, AE, AF
Fail column T
Fail column W, X
Yes
I'm thinking you could try:
Formula in R3:
=IF(OR(T3="No",U3="No",W3<7,X3>3,AE3="No",AF3="No"),"Fail column: "&TEXTJOIN(", ",TRUE,IF(T3="No","T",""),IF(U3="No","U",""),IF(W3<7,"W",""),IF(X3>3,"X",""),IF(AE3="No","AE",""),IF(AF3="No","AF","")),"Yes")
The key here is TEXTJOIN instead of CONCAT to exclude any empty values from the concatenated string.
Note: Excel and Google Spreadsheets are two different apps and the functions are not always exchangeable. Your question's title suggests that you are actually using Excel, however your tags include GS.
correct formula would be:
=ARRAYFORMULA(REGEXREPLACE(IF(
(T3:T="yes")*(U3:U="yes")*((W3:W<7)*(W3:W<>""))*(X3:X>3)*(AE3:AE="yes")*(AF3:AF="yes"),
"yes", "Fail column: "&
IF(T3:T="no", "T, ", )&
IF(U3:U="no", "U, ", )&
iF(W3:W>=7, "W, ", )&
IF((X3:X<=3)*(X3:X<>""), "X, ", )&
IF(AE3:AE="no", "AE, ", )&
IF(AF3:AF="no", "AF, ", )), ", $|Fail column: $", ))

Google Sheets RegexpReplace with computable replacers

I'm trying to replace a pattern with some string computed with other GSheets functions. For example, I want to make all the int numbers in the string ten times larger: "I want to multiply 2 numbers in this string by 10" should turn into "I want to multiply 20 numbers in this string by 100".
Assuming for short, that my string is in A1 cell, I've tried a construction
REGEXREPLACE(A1, "([0-9]+)", TEXT(10*VALUE("$1"),"###"))
But it seems REGEXREPLACE firstly computes the arguments and only after that yields regular expression rules. So it converts 3rd argument
TEXT(10*VALUE("$1"),"###") => TEXT(10*1,"###") => "10"
and then just replaces all integers in the string with 10.
It turns out, I need to substitute the group $1 BEFORE implementing outer functions in the 3rd argument. Is there any way to do such a thing?
Maybe there's another way. See if this works
=join(" ", ArrayFormula(if(isnumber(split(A1, " ")), split(A1, " ")*10, split(A1, " "))))
try:
=ARRAYFORMULA(JOIN(" ", IFERROR(SPLIT(A1, " ")*10, SPLIT(A1, " "))))
or:
=ARRAYFORMULA(JOIN(" ", IF(ISNUMBER(SPLIT(A1, " ")), SPLIT(A1, " ")*10, SPLIT(A1, " "))))

Return true/false on [strings in a cell] being found in [strings in another cell]?

Cell A1 contains multiple strings, eg "CAT DOG RAT GNU";
Cell B1 contains multiple strings, eg "RAT CAT";
How can I run a test (using formula in C1) to find if all the strings in B1 are present in cell A1?
Returning true/false would be good
Strings not necessarily in the same order, as example above
The number of items can vary
Multiple instances not a problem, so long as they're there
But returns true only if all items in cell B1 are present in cell A1.
So far I've tried transposed-split arrays with vlookups and matches, counts, etc, but nothing working for me. (And maybe regex won't do it as can't loop for each string?)
you can try:
=ARRAYFORMULA(IF(PRODUCT(N(NOT(ISNA(REGEXEXTRACT(SPLIT(B1, " "),
SUBSTITUTE(A1, " ", "|"))))))=1, TRUE))
for more precision you can do:
=ARRAYFORMULA(IF(PRODUCT(N(NOT(ISNA(REGEXEXTRACT(SPLIT(B1, " "),
"^"&SUBSTITUTE(A1, " ", "$|^")&"$")))))=1, TRUE))
then for case insensivity:
=ARRAYFORMULA(IF(PRODUCT(N(NOT(ISNA(REGEXEXTRACT(SPLIT(LOWER(B1), " "),
"^"&SUBSTITUTE(LOWER(A1), " ", "$|^")&"$")))))=1, TRUE))
and true ArrayFormula would be:
=ARRAYFORMULA(IF((A1:A<>"")*(B1:B<>""), IF(REGEXMATCH(TRANSPOSE(QUERY(TRANSPOSE(IFERROR(
REGEXMATCH(IF(SPLIT(B1:B, " ")<>"", SPLIT(LOWER(B1:B), " "), 0),
"^"&SUBSTITUTE(LOWER(A1:A), " ", "$|^")&"$"))),,999^99)), "FALSE"), FALSE, TRUE), ))

Reference text from cell and return remaining text to other cell

So my question is... I have 2 columns, A and B. In B I want to read the text from A (which has a list of texts: eg. Normal Car, Lorry, Sports Car, Bike) and I would type some text specified from the list in 'A' (eg. Bike) and in B would be the text that is left from that list from 'A' (So meaning "Normal Car", "Lorry" and "Sports Car" would be shown in 'B')
How do I do that?
This is the spreadsheet example:
https://docs.google.com/spreadsheets/d/16o75-R-U3zY0vajg1pO0N7-t9uGCtt_B2QiZLW_sXUM/edit#gid=0
Here are all the words that will be used: Lyrics, Visualizer, Bass Boost, 8D, Nightcore, Image Only
What I want to achieve is if I fill one or more of the words in Column A, then all the rest of the words will be filled up in B automatically. I left them filled up already so you can see the example. Thanks.
for one word:
=ARRAYFORMULA(REGEXREPLACE(TRIM(IF(LEN(A1:A),
REGEXREPLACE("Lyrics, Visualizer, Bass Boost, 8D, Nightcore, Image Only,",
A1:A&",", ), )), ",$", ))
for multiple words:
=ARRAYFORMULA(SUBSTITUTE(SUBSTITUTE(TRIM(TRANSPOSE(QUERY(TRANSPOSE(IF(LEN(A1:A), SUBSTITUTE(
REGEXREPLACE({"Lyrics", "Visualizer", "Bass Boost", "8D", "Nightcore", "Image Only"},
REGEXREPLACE(A1:A, ", ", "|"), ), " ", "♦"), )),,999^99))), " ", ", "), "♦", " "))
I would type some text specified from the list in 'A' (eg. Bike) and in B would be the text that is left from that list from
try like this:
=ARRAYFORMULA(REGEXREPLACE(TRIM(REGEXEXTRACT(A1:A, "(.*)"&B1:B)), ",$", ))