How can I make my IF statement do the following? - google-sheets

If I am using all of column A for example,
I want to do this:
IF cell value is between 30-34 then * 1.5, IF cell value is between 35-39 then * 2, IF cell value is between 40-44 then * 2.5???

You need to use nested if statements ...
If(and(A1>=30,A1<=34), A1*1.5, if(And(a1>=36,A1<=39),A1*2, if(and(A1>=40,A1<=44),A1*2.5,"")))
Paste the above into cell H1 and drag the formula down.

At some point, nested IF statements become unwieldy and a lookup to hard-coded values or a lookup table is more efficient.
=A2*lookup(A2, {0, 30, 35, 40}, {1, 1.5, 2, 2.5})

Related

Changing Sparkline color based on another cell's value

I have a sparkline that shows how complete an event is. I would like the color to change based on the value in another cell.
If the value is less than 50, I want it to be red;
If the value is greater than 51, but less than 75, I want it to be yellow; and - If the value is greater than 76, I want it to be green.
What would be the syntax for that?
I've tried nested if statements, but receive the message that only 3 arguments are allowed.
This is the formula I've tried:
=SPARKLINE((COUNTIF(H4:H14,"yes")/counta(H4:H14)*100),{"charttype","bar";"color1",if(E2<50, "red",if(E2>51, but E2<75, "yellow",if(E2>76, "green")));"max",100})
=SPARKLINE((COUNTIF(H4:H14,"yes")/COUNTA(H4:H14)*100),
{"charttype", "bar";
"color1", IF(E2>76, "green",
IF(E2>51, "yellow", "red"));
"max", 100})

Shonobi Data Grid scroll to a specific row

I have two grid let say grid a and grid b. Value of grid a is (1, 2, 3, 4, 5, ... 50) and in grid b (3, 5, 10, 25) .
I need when I click row with value 10 in grid b, then grid a will automatically scroll to row with value 10 too. Below code I have been try :
gridRoomStatus?.setContentOffset(CGPoint.init(x: 0, y: 0), animated: true)
self.gridRoomStatus?.reloadColumns(self.gridRoomStatus?.columns)
I have that code to make grid a go to the top when grid b is clicking. But it's still not solving my problem. I can't get a specific point for row with specific value. In UITableView there are tableView.rectForRow(at: indexPath) but I can't find similar function with that in Shinobi data Grid. How to do that?
Here I solved this problem, example :
let rowHeight = 50
let rowIndexToSnapTo = 20
grid.setContentOffset(CGPoint(x: 0, y: rowHeight * rowIndexToSnapTo))

Can a table view row stay put while its section is moving?

I am trying to create a general diffing function for doing batch updates in table views. Basically, it takes two arrays of sections, each containing an identifier for each section and an identifier for each row in each section, and calculates which sections to delete, insert or move and which individual rows to delete, insert or move. I.e., suitable input for deleteSections(_:with:), insertSections(_:with:), moveSection(_:toSection:), deleteRows(at:with:), insertRows(at:with:) and moveRow(at:to:).
I thought this could be done quite generally, but it seems I've found limits to what can be done, and I wanted to just check if I'm missing something.
So let's say I have two sections, "Fruits" and "Vegetables", containing one element each: "Banana" in Fruits, "Carrot" in Vegetables.
Let's say I want to switch so that suddenly a banana is a vegetable and a carrot is a fruit. Easy enough, I generate a moveRow(at: [0, 0], to: [1, 0] and a moveRow(at: [1, 0], to: [0, 0]) and update my data source accordingly; the rows will switch place.
Now, let's say instead I want the two sections to switch places. I will do a moveSection(0, toSection: 1) (or I can do moveSection(1, toSection: 0, or both - it doesn't matter in this case). Ok. The carrot row now moves along with the vegetables sections and the banana with the fruits.
But now... I'd like to do both of these things at the same time. The sections should switch places, but the items should stay put - or, to put it differently, they should switch which logical section they belong to but keep their physical row. This does not seem possible.
I've tried to do the moveSection and at the same time (== within the same beginUpdate/endUpdate) do moveRow(at: [0, 0], to: [0, 0]) and moveRow(at: [1, 0], to: [1, 0]), but these calls to moveRow are just the no-ops they appear to be.*
I've also tried to do deleteRows/insertRows to make the rows stay where they are; that instead gives me a crash:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Attempt to create two animations for cell'
reloadRows doesn't work either; afaik that does the same as an deleteRows/insertRows pair.
So, basically my question is:
Am I missing something? Is this possible?
(Not interested in workarounds like reconfiguring the cells or headers with the new data, I don't actually have a situation where I need to do this, I just want to know!)
Demo code if anyone wants to play around.
(* Actually, they are just no-ops when combined with both moveSection(0, toSection:1) and moveSection(1, toSection:0); if only one of these are performed, we get that internal consistancy crash!)
Change your switchBothNoOp() into this:
func switchBothNoOp() {
switch state {
case .first:
sections = [("Fruits", ["Banana"]),
("Vegetables", ["Carrot"])]
case .second:
sections = [("Vegetables", ["Banana"]),
("Fruits", ["Carrot"])]
}
tableView.moveSection(0, toSection: 1)
}
Then you move rows later using another begin/end updates block. For example:
func update() {
tableView.beginUpdates()
// Pick on of these:
// switchItems()
// switchSections()
switchBothNoOp()
// switchBothCrash()
tableView.endUpdates()
tableView.beginUpdates()
tableView.moveRow(at: [0, 0], to: [1, 0])
tableView.moveRow(at: [1, 0], to: [0, 0])
tableView.endUpdates()
}

UICollectionView Horizontal Scrolling - cells ordered into columns instead of rows

I have a collection View that I made horizontal scrolling. It has 3 rows and 5 columns. Once I enabled horizontal scrolling the cells fill up going down the columns instead of across the rows. For example i have an array 1,2,3,4,5,6,8,9,10,11,12,13,14,15,16 that i am using to fill my collection view. The cells would look like this
1,4,7,10,13,16
2,5,8,11,14
3,6,9,12,15
How can I fix this.
That's the default behavior of horizontal.If you want you can sort the array in some way so it'll be displayed the way you want it.
Like this:
var array = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
var horizontalSortedArray = [Int]()
let columns = 5
let rows = 3
for i in 0..<columns {
for j in 0..<rows {
horizontalSortedArray.append(array[j*columns+i])
}
}
print(horizontalSortedArray)
// [1, 6, 11, 2, 7, 12, 3, 8, 13, 4, 9, 14, 5, 10, 15]
Now if you use this new array as your data source in horizontal mode it'll be displayed like this:
1 ,2 ,3 ,4 ,5
6 ,7 ,8 ,9 ,10
11,12,13,14,15

highcharts column graph with dynamic data- column alignment off

i'm hoping that this quick description and image will ring a bell with someone who has had a similar issue and therefore a suggestion/fix.
i have a column graph that i am adding data to dynamically (via jQuery parsing an XML file).
for some reason, after the data is added, the alignment of the different series gets a little off. the issue fixes itself after i toggle one of the series by being visible/invisible (by clicking the series in the legend).
when i add the data via hardcoding the numbers, just to ensure it works, it works great.
here is the image:
the yellow series is the last series added to the chart, the red and purple series line up ok after toggling the visibility of one of the 5 series.
any help would be greatly appreciated!
UPDATE with info on the data:
i have 5 series of data and 10 x-axis categories
i am building a multidimensional array of data as i parse the XML file
the array length is 5, with each of those 5 index's containing an array of length 10
this is what the array looks like after it has been populated with data:
index#: 0 value: 0,0,0,0,0,0,0,0,0,0
index#: 1 value: 180,210,0,0,0,0,0,0,180,210
index#: 2 value: 22,4,0,0,0,0,0,0,22,4
index#: 3 value: 0,0,0,0,0,0,0,0,0,0
index#: 4 value: 200,30,0,0,0,0,0,0,4,0
i am adding the data to the chart with the following JS code:
for (var c_ary_bs = 0; c_ary_bs < ary_bs_schedule_orig.length; c_ary_bs++) {
chart.series[c_ary_bs].setData(ary_bs_schedule_orig[c_ary_bs]);
}
hopefully that will help, thank you!
UPDATE 2, some more info
i've hard coded the data being added to the array, to help pinpoint the issue:
chart.series[0].setData([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
chart.series[1].setData([180, 210, 0, 0, 0, 0, 0, 0, 180, 210]);
chart.series[2].setData([22, 4, 0, 0, 0, 0, 0, 0, 22, 4]);
chart.series[3].setData([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
chart.series[4].setData([200, 30, 0, 0, 0, 0, 0, 0, 4, 0]);
alert('done')
when the alert fires, the graph columns are aligned properly, after I click "ok" to dismiss the alert, the alignment issue happens, as the previous image depicts.
I found an imperfect fix:
-setting the marginLeft of the chart to 70 alleviates the issue with the columns not aligning
-for some reason the y-axis title text is displaying over the y-axis ticks, so i am using the following to make it visible:
yAxis: {
title: {
x:-20,
text: 'Schedule Days'
}
}
(note the x: -20)
Whats's odd is that when I toggle one of the series (by clicking it in the legend) the yAxis title text reverts to where it should be (which is now 20px off because of the above fix).
The perfect fix would put the yAxis text where it is after I toggle one of the series, but at least this way it is now visible regardless of whether or not the user toggles the series.

Resources