How to use a conditional array formula in Google Sheets? - google-apps-script

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.

Related

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

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

How to use cell value instead of number

Im sorry I dont even know how to correctly phrase this question.
The line of code I need to edit is this;
sheet.getRange(targetcell).setValue('=INDEX(QUERY(IMPORTHTML("http://www.futbin.com/consumables/Position%20Change","table",2),"select Col2"),1)');
However instead of row 1, I want to use the value from cell G1. So, something like this;
sheet.getRange(targetcell).setValue('=INDEX(QUERY(IMPORTHTML("http://www.futbin.com/consumables/Position%20Change","table",2),"select Col2"),G1)');
However, this throws an error.
Could anyone advise how I can use the value in G1 instead of it being fixed?
Thank you.
Try .setFormula instead of .setValue ...
sheet.getRange(targetcell).setFormula('=INDEX(QUERY(IMPORTHTML("http://www.futbin.com/consumables/Position%20Change","table",2),"select Col2"),G1)');
If there were a column G from the table that results from that IMPORTHTML function then you would use this:
sheet.getRange(targetcell).setValue('=INDEX(QUERY(IMPORTHTML("http://www.futbin.com/consumables/Position%20Change","table",2),"select Col7"),1)');
Because G is column 7
However, there is only 3 columns from that IMPORTHTML. You can check this by entering the formular into a Google Sheet:
=IMPORTHTML("http://www.futbin.com/consumables/Position%20Change","table",2)
I suggest playing around with your formula in SHeets and once you've got what you want, go back into AppsScript.

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

(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

google spreadsheet importxml, chop off part of query

I have a simple ImportXml function in Google spreadsheet:
=ImportXml( "http://www.google.com/finance?q=0322.hk", "//span[#class='chr']" )
but it is giving me 2 cells of data, the cell direction underneath it is a =Continue(blah blah)... automatically placed by Google.
Here is the HTML code associated with this page. Looks like it has 2 items. I've replaced "<" & ">" with "(" & ")"
(span class="ch bld")(span class="chr" id="ref_673324_c")-0.45(/span)
(span class="chr" id="ref_673324_cp")(-2.02%)(/span)
How do I change my ImportXml function so that I'm only importing the "-0.45" without the "(-2.02%)?
I've also found that the INDEX command works... useful too if you want the 2nd (-2.02%) item in the results. One important note, though, is that you've got to first transpose the output of ImportXML or it won't work.
=INDEX( TRANSPOSE (ImportXML (...)), {num}) where num is the desired result.
I found a work around... I put =NOEXPAND function infront of it and it works. but if anybody has another solution let me know.