Find a string in a list of substrings (google sheets) - google-sheets

I have a list of substrings like this (list 1)
ABC
DEF
GHI
JKL
Then I have a list of strings like this (list 2)
ABCLKAKJDAKSDJ
DEFADLKASDKAS
GHIASDKASLDK
I want to find the list of substrings for each item in the list of strings, with an output like this:
ABCLKAKJDAKSDJ ABC
DEFADLKASDKAS DEF
GHIASDKASLDK GHI
You can assume that the strings in list 1 will only be found in the front of strings in list 2.

Try this
=query(arrayformula(split(flatten(iferror(C1:C3 &"~"& A1:A4 & "~" & SEARCH( A1:A4,transpose(C1:C3)))),"~",false)),"select Col1,Col2 where Col3=1")
List 1 in A
List 2 in C
Formula in E1

Related

Google Sheets formula to merge multiple columns into one cell, including headers / label

I want to combine multiple columns in a Google sheet into one cell, including headers / labels. My input sheet looks like this:
Name
Description
Col 1
Col 2
Col 3
Foo
val 1
val 2
val 3
Bar
val x
val y
val z
And I want to concatenate the values in each column into a single cell, including the headers for each value. Desired output:
Name
Description
Foo
Col 1val 1Col 2val 2Col 3val 3
Bar
Col 1val xCol 2val yCol 3val z
This formula concatenates the columns into one, however it doesn't include the headers (note that char(10) produces a linebreak):
=TEXTJOIN(char(10),true,B2:E2)
I can produce a similar result using REDUCE (I can't believe I'm writing a reducer in a Google Sheet):
=REDUCE("", B2:E2, LAMBDA(accumulator, current_value, CONCATENATE(accumulator, current_value, char(10))))
However the lambda function doesn't seem to provide the index of the current iteration, so I'm not sure how to grab the column name above it.
Use this formula
=ARRAYFORMULA(
QUERY({"Description";
TRIM(SUBSTITUTE(FLATTEN(QUERY(TRANSPOSE(
IF(B2:D="",,","&B1:D1&","&B2:D&",")),"",9^9)),",",CHAR(10)))},
" Where Col1 is not null" ) )
try the old way:
=ARRAYFORMULA(TRIM(REGEXREPLACE(TRIM(FLATTEN(QUERY(TRANSPOSE(
IF(B2:D="",,"×"&B1:D1&"×"&B2:D&"×")),,9^9))), "×", CHAR(10))))
or in one cell if that's what you want:
=INDEX(TRIM(REDUCE(, B1:D1&CHAR(10)&B2:D3&CHAR(10),
LAMBDA(a, c, a&c&CHAR(10)))))
Using your current TEXTJOIN() formula you can just include the Column Headers with the dollar sign ($) which means absolute reference so it will not change when copied/dragged down to rows below.
Try:
=TEXTJOIN(char(10),true,C$1,C2,char(10),D$1,D2,char(10),E$1,E2)
Drag down to rows.
Result:
Refer to this link for more info on Absolute Reference: https://www.lido.app/tutorials/absolute-reference-on-google-sheets

Making String Comparisons Using Google Sheets Query Function

I'm having trouble understanding why my query doesn't work. The result is always null unless remove C=D then I get a list of ID's. The formula will be used on a new sheet called IDS and pulls the data from Threads. My Query is:
=QUERY(Threads!A:D,"select A where B='No Label'and C=D",-1)
The table I'm trying to query looks something like this:
| IDS (A) |Labels(B) |Email List(C) | Matching Emails(D)|
|----------------|----------|-------------------|-------------------|
|179cd3g671269f69|No Label |pat#rus.com |bro#rus.com |
|179cd83p7a655449|No Label |SCP.admi#pab.com |mike#rus.com |
|179cb58p79236216|No Label |SCP.admi#pab.com |pat#rus.com |
|179c9er26ca777c8|No Label |dar#rus.com |sed#rus.com |
|179c8l3c5b46e4ga|No Label |dar#rus.com |will#rus.com |
|179c8oe73a13d487|No Label |pat#rus.com |dar#rus.com |
It's not working because there are no rows when values in Col C are the same as Col D.
If you want to check every row in Col C against any value in Col D, then use:
=QUERY(Threads!A:D,"select A where B='No Label' and C matches '"&textjoin("|",true,D2:D)&"' ",1)
textjoin("|",true,D2:D) brings back all the values in Col D:
bro#rus.com|mike#rus.com|pat#rus.com|sed#rus.com|will#rus.com|dar#rus.com|pat#rus.com
The | separator works as OR, so bro#rus.com OR mike#rus.com...
If needed, you could bring back a unique list if you get duplicates in future:
textjoin("|",true,unique(D2:D))
So the formula would be:
=QUERY(Threads!A:D,"select A where B='No Label' and C matches '"&textjoin("|",true,unique(D2:D))&"' ",1)
Doing a check the other way round (Col D matches in Col C):
=QUERY(Threads!A:D,"select A where B='No Label' and D matches '"&textjoin("|",true,unique(C2:C))&"' ",1)

I want to combine multiple REGEXREPLACE in google sheet

I have a google sheet and in 'column A' I have list of fruit names. I want to find and replace 5 types of fruits with code names
"Mango" to be replaced with "001"
"Apple" to be replaced with "002"
"Orange" to be replaced with "003"
"Banana" to be replaced with "004"
"Jackfruit" to be replaced with "005"
I have a formula to replace just one of the word. Below is the formula
=ARRAYFORMULA(REGEXREPLACE(Sheet1!A:A, "Mango", "001"))
How do I combine the formula to replace all fruits with code words?
If it were me, I'd just use SWITCH:
=ArrayFormula(IFERROR(SWITCH(A:A,"mango","001","apple","002","orange","003","banana","004","jackfruit","005"),A:A))

Using Google Spreadsheets QUERY to Filter

I have the following google sheet where:
Col a= quantities
Col b= product codes, which i´ve split between C and H.
I want to know the quantity according to different "filters"... this filters are the fields between C11 and H11, and they are optional. There are 6 possible filters.
It works using =QUERY formula located in H12 and it returns the sum of quantity values where the filters match...
BUT there´s the possibility of leaving a filter empty to get "all"...
the query is as follows:
=QUERY(A1:H7, "select sum(A) where C contains '"&C11&"' and lower(D) contains lower('"&D11&"') and E contains '"&E11&"' and lower(F) contains lower('"&F11&"') and lower(G) contains lower('"&G11&"') and lower(H) contains lower('"&H11&"') ",-1)
My problem is with the match type: where C contains '"&C11&"' and...
instead of using "contains" it should compare using "matches". this way it would count like it should, but then it won´t accept empty filters.
How can I get it to count only if the field is filled??
What´s wrong with the filter right now? It´s counting "4" matches because model matches "1" happens when model is 1 or 21, also with column D where i´m looking for value X and is also counting value XN as it contains "X". if formula is changed to "matches" instead of "contains", it won´t allow me to leave it blank.
Thank you!
Karl_S formula is great, but it does not sum the quantities in column A. Adapting his approach to SUMIFS seems to do it:
=SUMIFS(A2:A7,C2:C7, IF(ISBLANK(C11), "<>" ,C11),D2:D7, IF(ISBLANK(D11), "<>" ,D11),E2:E7, IF(ISBLANK(E11), "<>" ,E11),F2:F7, IF(ISBLANK(F11), "<>" ,F11),G2:G7, IF(ISBLANK(G11), "<>" ,G11),H2:H7, IF(ISBLANK(H11), "<>" ,H11))
Use this formula instead:
=COUNTIFS(C2:C7, IF(ISBLANK(C11), "<>" ,C11), D2:D7, IF(ISBLANK(D11), "*",D11), E2:E7, IF(ISBLANK(E11), "<>",E11), F2:F7, IF(ISBLANK(F11), "*",F11), G2:G7, IF(ISBLANK(G11), "*",G11), H2:H7, IF(ISBLANK(H11), "*",H11))
If one of the options is blank, it will match all items in that column. Otherwise it should do an exact match.

Google spreadsheet get complements

Link here to spreadsheet
As the title says, how can I end up with cells that don't match in value, the difference/complement (set theory)?
I would prefer it with only FUNCTIONS and no script.
{1,2,3,4}\{1,3} = {2, 4}
or with letters
{a,b,c,d}\{a,c} = {b, d}
or with with space.
{xyz zyx, abc cba, qwe ewq}\{xyz zyx, qwe ewq} = {abc cba}
You can add one single formula at the top of your result column like this :
=arrayformula(if(B2:B=A2:A,,A2:A))
Having two arrays:
Array1 Array2
1 1
2 b
b
4
c
you can get resulting array:
Result
2
4
c
Try this formula:
=FILTER(A2:A6,ROUND(MMULT(ArrayFormula(--(A2:A6<>TRANSPOSE(B2:B4))),TRANSPOSE(SPLIT(REPT(1/ROWS(B2:B4)&"/",Rows(B2:B4)),"/"))),5)=1)
May be too complex for this task, could somebody edit it? Please, look at sample sheet
Edit
I've found more simple way to do this, using Regular Expressions:
=FILTER(A2:A6,REGEXMATCH(ARRAYFORMULA("'"&A2:A6),JOIN("|",B2:B3))=false)

Resources