How can I make a formula in Google Spreadsheet to return a value if a cell contains a text in a list - google-apps-script

I'm currently interested in building a custom function/formula in Google Spreadsheet where the function will intake a cell and return a value that is related to a specific text in a list.
As you can see in the screenshot, which is a demo that I drafted to give a better idea of what I'm trying to accomplish, column A contains a list of items that has random texts. In the texts, there are some keywords that I would like to identify into different categories.
Column B is just a demo of how I envision to use my custom function that I want to create. Column C is the expected result that I would get by using the custom function.
Column E is the list of keywords that I would like the custom function to search for in cells and Column F is the category that I would like the custom function to return once it finds the relevant keyword in a text.
Thank you!
I tried searching for this topic, but I only came across how to set up an array formula to achieve what I want to do. If there was any post about this, I would really appreciate if you could just link it in the comment.

Given your scenario, you can try with this formula:
=FILTER(F:F,INDEX(REGEXMATCH(A2,"(?i)"&E:E)),E:E<>"")
If you wish to have it for the whole row:
=BYROW(A2:A,LAMBDA(each,IF(each="","",FILTER(F:F,INDEX(REGEXMATCH(each,"(?i)"&E:E)),E:E<>""))))
If you could have more than one result, you can add JOIN:
=BYROW(A2:A,LAMBDA(each,IF(each="","",JOIN(", ",FILTER(F:F,INDEX(REGEXMATCH(each,"(?i)"&E:E)),E:E<>"")))))

Related

Extract a single row from a table

I’m trying to extract a single row from a table.
I'm using google sheet to create the links and in cell D3 it contains this url.
https://www.wsj.com/market-data/quotes/AAPL/options
I have several links in cell D3 to go through.
The word "Last Trade" appears several times in different tables but I'M ONLY INTERESTED IN THE VERY FIRST TABLE FROM THE TOP.
with this word and once this word is found i'm looking to extract the ROW just above it.
Below is the IMPORTXML, and its needs modification and it should be able to pull that last row.
=IMPORTXML(D3,"//tr[td1/#class='acenter inthemoney'][last()]")
Any help would be greatly appreciated.
Thanks.
For that row you will need:
(//tr[#class='last_trade_row'])[1]/preceding-sibling::tr[1]
And then pick the wright td...it's unclear which td you want. So if you wanted the third td the XPath would be:
(//tr[#class='last_trade_row'])[1]/preceding-sibling::tr[1]/td[3]
Its always the first table that ends with the word LAST TRADE and the row above it that i'm looking to extract, so in this case this is the row that i'm looking to extract, below is the picture.
https://www.wsj.com/market-data/quotes/AAPL/options
In the above case where you want the first td the XPath will then be
(//tr[#class='last_trade_row'])[1]/preceding-sibling::tr[1]/td[1]

Do I need a script or a formula?

I am trying to have the formula look in Column A for not empty cells. (That part of the formula works fine.) Then return the name from Column B that's in the same row as the not empty cell.
Column A Column B
text/date Kelly
So if B has anything in it, tell me the name Kelly. I 've tried combining formulas, but I'm either not doing it correctly or maybe I need a script?
Here's the part that's working: =IF(A24="","don't meet","meet")
Of course, I want it to search the whole column, but I know for sure 24 has the text in the cell with a name so I was just playing around that line.
Answer is based on the description and document you provided and should give you something to work with.
This formula simple checks the entire Column A and returns values from the corresponding row from Col B if the Cell from the Col A row is empty/blank.
=ARRAYFORMULA((IF(A1:A<>"",B1:B,"")))
Here are example screenshots of your example data + end result
Though I'm not entirely sure what you'd like to happen if A has values on them. This formula retains A if it has content but you could always change the (A2:A="",B2:B,A2:A) part of the formula if you want something else to happen if A is not empty.
Give this a try, I put it in your sheet in the green area.
=ARRAYFORMULA(IF(A1:A15 = "", "", B1:B15))
Arrayformula applies the IF function to the entire range. So substitute A1:A15 with what ever the whole range is, A1:A600 or A1:A if you want to do the whole column. Make srue the second column has the same values but with B.
You can use filter() to filter your Column B if both column A and column B is not empty.
Formula:
=filter(B1:B,A1:A<>"",B1:B<>"")
Output:

Javascript working with empty cells

I am working with Google Sheets. I want to build a custom function that adds 5 cells together. This code worked just fine:
function addData(a,b,c,d,e){
return a+b+c+d+e
}
When I put the values: 80,80,80,80,80...I appropriately get 400.
The problem happens when one of the cells in empty. When I have the values:
___,80,80,80,80 I get 80808080
80,___,80,80,80 I get 808080
80,80,___,80,80 I get 16080
80,80,80,___,80 I get 24080
80,80,80,80,___ I get 320 <--- correct answer
The function appears to add correctly until it hits an empty cell and then just tacks on the remaining values instead of continuing to add them.
I am very new to JavaScripting so I really do not know where to begin my research. Any help would be greatly appreciated. I asked this question before but I feel like this is more concise as to what I am trying to accomplish.
you get different results because each column has different format
try to convert string type to integer
or let script like that and change the column format to number
function addData(a,b,c,d,e){
return parseInt(a)+parseInt(b) +parseInt(c) +parseInt(d) +parseInt(e)
}

How to use a conditional array formula in Google Sheets?

I want my spreadsheet user to be able to choose between two different options for an array formula. I have tried to put it in an IF statement but I can't get it to work. I do not want to cut and paste it down the column because I want it to be applies when a new row is added. I don't really know what I'm doing but I've been fiddling with it for a couple of hours now. Here is the code I currently have:
=IF($B$6="alternating days",ARRAYFORMULA(IF(ISBLANK(indirect("OVERVIEW!$A" & row())),IF($C2:C="l","l","d"),IF($C2:C="l","d","l"))),IF($B$6="weekdays/weekends",ARRAYFORMULA(IF(ISBLANK(indirect("OVERVIEW!$A" & row())),IF($C2:C="l","l","d"),IF(OR(WEEKDAY
(indirect("OVERVIEW!$A" & row()))=1,WEEKDAY(indirect("OVERVIEW!$A" & row()))=7),"l","d"))),"none"))
It's a long formula so please scroll along.
I attempted to have the ARRAYFORMULA at the beginning but it wouldn't let me reference just $B$6.
Thanks for any help you can provide.
I recommend something like this:
=ARRAYFORMULA(IF($B$6="alternating days",if(isodd(row(indirect("C4:C"&counta(OVERVIEW!A3:A)+3))),"l","d"),IF($B$6="weekdays/weekends",ARRAYFORMULA(IF(WEEKDAY(OVERVIEW!A3:A)=1,"d",if(WEEKDAY(OVERVIEW!A3:A)=7,"d","l"))))))
If the days are alternating, use odd rows to alternate the letter, otherwise just combine 2 IF statements to determine if it is a weekday or not.

(SSRS) if cell contains specific text then divide another cell by a number say 2

I'm having trouble trying to divide a cell by (A NUMBER) only if another cell contains specific text and if the cell contains another text i want it to be divide by a different number in SSRS
This is what i have but it is not working.
=IIF(Fields!PARTNO_LOT.Value=("A1001"),Fields!LOCSTOCK.Value/200)
I have added a example but in excel just to show my intent in SSRS.
If the above answer doesn't work, try this:
=Switch(
Fields!PARTNO_LOT.Value="A1001",Fields!LOCSTOCK.Value/200,
Fields!PARTNO_LOT.Value="A1002",Fields!LOCSTOCK.Value/285,
Fields!PARTNO_LOT.Value="A1003",Fields!LOCSTOCK.Value/89
)
Let me know if this helps.
If I understand correctly, you are trying to execute multiple conditions. You can use nested IIF statements:
=IIF(Fields!PARTNO_LOT.Value=("A1001"),Fields!LOCSTOCK.Value/200,
IIF(Fields!PARTNO_LOT.Value=("A1002"),Fields!LOCSTOCK.Value/285, Fields!LOCSTOCK.Value/89))
If your values are only few, this would work. If they are more than 2-3, I would look into creating a function. Reference: https://msdn.microsoft.com/en-us/library/ms156028.aspx