Is it possible to get the last row filled? - gspread

In gspread, is it possible to get the last row or cell number that is filled?
The API reference page doesn't seem to help.

Gspread has "row_count", but I am not sure it returns all rows in a spreadsheet, or just the filled ones. If that doesn't help, there is a slightly less direct, but completely functional way to do it:
1.) let's assume your data is in column A, and it has been filled in consecutively row by row (i.e. no skipped rows)
2.) in another free cell in your spreadsheet--let's assume cell B1--use the native Google Sheets function COUNTA, which will count the number of values in a dataset, and specify column A as the target, i.e. "=COUNTA(A:A)"
3.) now just request the value of cell B1 with gspread's acell, i.e. "last_row_updated = myWorksheet.acell("B1").value"

You can use the following code, taken from this answer to this similar question:
def last_filled_row(worksheet):
str_list = list(filter(None, worksheet.col_values(1)))
return len(str_list)

Related

Writing a formula in a cell in Google Sheets that averages the results from a column derived from expected values in multiple columns

I'm an average user of Google sheets and I've tried writing/looking up the formula I'm going for, but I haven't had any luck yet.
I have a spreadsheet that details multiple values that I need to display in a single cell the average of a certain set of values derived from a specific set of those values from multiple columns.
The flow of information would look something along the lines of:
if value in Column D=L
then
if value in Column J<$1.20
then
Find Avg of all Values in Column N
I'd need the formula to narrow it's field of data each time so the final result was the average of all the values in Column N that had a value in column J<$1.20 with a value in Column D=L.
I feel like a dummy over here because I just can't narrow down how I should write this flow and get it to work right without adding multiple extra hidden columns. Can anyone help on this one?
I've tried writing the formula multiple different ways but haven't kept it written down to pass on.

Unable to use INDEX COUNT for referencing all of the rows in a Google Sheet

I am trying to use the format/formula A:INDEX(B:B, COUNTA(A:A)) to reference all of the rows until the first empty one and it is not working.
If I skip the first row, then the formula works: A2:INDEX(B2:B, COUNTA(A2:A)).
What am I doing wrong?
I believe because you're using the number of rows to define the end of the data (using INDEX), you have to include a defined starting row. I think this should work for you:
=QUERY(A1:INDEX(B:B, COUNTA(A:A)), "SELECT *")
That is assuming you don't have data that exists beyond the first blank row.

Google Sheets Row Number of nth Instance from a column in another spreadsheet

I have used the following google sheets formulas to obtain the row number for the nth instance of cell k3 within column A.
=ArrayFormula(SMALL(IF($A$4:$A$20000=K3,ROW($A$4:$A$20000)-MIN(ROW($A$4:$A$20000))+4),n))
I also used the following formula but it only works for the 1st instance.
MATCH(K3,A4:A20000,0)+ROW(A4:A20000)-1
I need to get the row number of the nth instance but for a range in another spreadsheet. I have tried replacing every instance of $A$4:$A$20000 in the 1st formula above with the string below but to no avail. I also tried it without the IMPORTRANGE().
IMPORTRANGE("myurl","mysheetname!$A$4:$A$20000")
Please help me to get that row number. Cheers.
Try:
=filter(row(A4:A2000),A4:A2000=K3,countifs(A4:A2000,A4:A2000,row(A4:A2000),"<="&row(A4:A2000))=K4)
Where cell K3 is what you're looking for and K4 is the instance number (which you can hard code if you want).
Update for your sample sheet:
To bring in the data from another sheet, load it into your final sheet rather than trying to put the importrange() in the formula:
Then the formula in cell A9 can reference the imported range (identifying the row number for the second instance of "BAG1"):
=filter(row(A2:A6),A2:A6="BAG1",countifs(A2:A6,A2:A6,row(A2:A6),"<="&row(A2:A6))=2)
I got this to work...
=VLOOKUP("BAG1"&3,IMPORTRANGE(url,Sheet1!A2:C6),3).
It finds the instance of BAG1 that has 3 in the neighbouring column and then retrieves the value in column 3.

Google Sheets: How can I count the number of rows in a 2 column range where both cells are not empty?

I have a range where I want to count the amount of rows where both cells in this row are not empty. I found a way using a third colum, but I want a cleaner function, preferably one I can contain in one cell.
For example: example
The function should return 1, as there is only one row where both of the cells are not blank.
Thanks for you input!
MSE
This should do what you want
=COUNTIFS(A1:A20,"<>"&"",B1:B20,"<>"&"")

Excel 2017 Formula - Average data by month, while being filterable

I'm not a VBA coder, and I would prefer an excel formula if possible, the easiest solution will be the best one.
Test workbook screenshot
As you can see, I have plenty of columns, which are filterable.
I am attempting to retrieve an average of Column L, but I want the data to be calculated for the correct month in G3:R3.
The resulting calculation needs to be recalculated when filtered, between customers, sites, status, job type etc.
I am referencing the resulting cells in another sheet, which gives an idea of trends I can glance at, as such filtering by month in each sheet, is not an option.
=AVERAGE(IF(MONTH(E9:E1833)=1,(J9:J1833)))
This one does not update with the filtered data.
=SUM(IF(MONTH(E9:E1833)=1,J9:J1833,0)) /SUM(IF(MONTH(E9:E1833)=1,1))
This one does not update with the filtered data.
I have tried 5 different SUBTOTAL formulas, some with OFFSET, none of these produce the same result I get when checking manually.
Each worksheet has over 1,500 hundred rows, the largest is 29148 rows. The data goes back as far as 2005.
Please can someone help me find a solution?
One possible solution is to create a helper column which returns 1 if the row is visible and returns 0 if the row is invisible (or blank). This allows a bit more freedom in your formulas.
For example, if you want to create a helper column in column X, type this into cell X9 and drag down:
= SUBTOTAL(103,A9)
Now you can create a custom average formula, for example:
= SUMPRODUCT((MONTH(E9:E1833)=1)*(X9:X1833)*(J9:J1833))/
SUMPRODUCT((MONTH(E9:E1833)=1)*(X9:X1833))
Not exactly pretty but it gets the job done. (Note this is an array formula, so you must press Ctrl+Shift+Enter on your keyboard instead of just Enter after typing this formula.)
With even more helper columns you could avoid SUMPRODUCT altogether and just accomplish this by doing a single AVERAGEIFS.
For example if you type into cell Y9 and drag down:
= MONTH(E9)
Then your formula could be:
= AVERAGEIFS(J9:J1833,X9:X1833,1,Y9:Y1833,1)
There isn't a clean way to do this without at least one helper function (if you want to avoid VBA).

Resources