What is the meaning of max and offset pagination params in createcriteria? - grails

In the Grails documentation, we read:
def results = Book.list(max: 10, offset: 100)
max - The maximum number to list
offset - The offset from the first result to list from
Also, from this MySQL tutorial, we read: the offset specifies the offset of the first row to return.
Questions:
Is max the maximium number of rows per page?
Could you give me an example about the offset meaning?

Hello GORM, when you return me a list of Books give me only 10 (max) results but do not start from the Zeroth (offset = 0) place, instead start listing results from 100th (offset) result.

Related

Google Sheets; Finding the first cell to contain 0 in a column where the row number is > the row number generated in another formula

I'm making a document in Google sheets that produces the start and end times where a number of people are in a section. I was able to produce a grid, but I can't seem to find a formula for calculating end times that occur after the start time.
Time
Section1
Section2
9:00am
1
0
9:30am
1
0
10:00am
1
1
10:30am
0
1
I want to make a series of formulas that list the start and end times when people are present in a section.
(Start time) Find the first cell to not contain 0 and return the value in column A {I have found a formula for this step}
(End time) Find the first cell to contain 0 where the row number is greater than the row number of the previous start time and return the value in column A
For the first part, I can find the start time with the formula
=IFNA(INDEX($A$2:$A,(MATCH(TRUE,INDEX($B$2:$B>0,0),0))))
But when I try to adapt it to the end time by changing the greater than (>) to equals (=), it gives me the first time there is a zero regardless of if it occurs before the start time. I feel like adding some kind of row check will be necessary to fix this, but I'm not sure how. I would really rather not learn Google API if at all possible,
If it's just to find the first 0 after a 1, you could benefit from the use of INDEX and MATCH to create a range that goes from the first 1 to the last row and look the 0 in that range. That is: INDEX(B2:B,MATCH(1,B2:B,0)):INDEX(B2:B,ROWS(B2:B)) So, inserted in the whole formula:
=INDEX(A2:A,MATCH(1,B2:B,0) + MATCH(0, INDEX(B2:B,MATCH(1,B2:B,0)):INDEX(B2:B,ROWS(B2:B)) ,0)-1)

In Google Sheets add all the rows above the current one

I want to add all the numbers in a given column of all the rows before the total row. Let's assume my column H contains number. If I have 15 rows, I'd do sum(H1:H15) in my sixteenth (total) row.
But when I add rows in between, my formula doesn't get updated, so I have to think of updating it to include all the rows.
I thought of using indirect to determine the index of the total row and doing the sum up to that row minus 1. That gave me:
=round(sum(indirect(concatenate("H2:H",row()-1))))
We can ignore round() here - it's just to given the nearest integer for my sum
sum adds all the rows
indirect lets me convert a string into an address so it can be used in my sum function
concatenate adds all the strings together
"H2:H" is the beginning part of my range
row() returns my current row and so row()-1 represents the last row I want to sum
Is there a more elegant way?
Thanks
You can use OFFSET for that like this:
=SUM(OFFSET(B6, 0, 0, ROW() - ROW(B6), 1))

How to add offset'ed individual fields in a google sheet

I am working on adding the time I spend on my habits using google sheets. If you look at this example sheet, I am keeping my individual habits in columns 3-8 (see the offsets on the first row).
To add the food related habits times (columns 5 and 6), I can use the range in offset function (see formulae in D17 below "Food").
The question is: how do I add the numbers for exercise and sleep (column offsets 4, 7, and 8)? The number of columns here could be 2, 3, or more! And they might not be consecutive.
Thanks for any pointers.
To sum entries of the rows whose columns are in the given array, I would use
=SUMPRODUCT(COUNTIF({5,8,9},COLUMN(D3:J3))*(D3:J3))
This is the formula for E18 in your spreasheet.
Since the columns might not be consecutive and there can be a variable number of them, I think it is appropriate to use an Apps Script custom function, and use the spread syntax to account for the variable number of columns.
Just open the script bound to your file, copy this function and save the project:
function HABIT_TOTALS(...habitIndexes) {
const sheet = SpreadsheetApp.getActiveSheet();
const headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];
let output = [];
for (let dayIndex = 0; dayIndex < 7; dayIndex++) {
let dayValue = 0;
habitIndexes.forEach(habitIndex => {
const columnIndex = headers.indexOf(habitIndex) + 1;
const dailyHabitValue = sheet.getRange(3, columnIndex).getValue();
const dayHabitValue = sheet.getRange(4 + dayIndex, columnIndex).getValue();
dayValue = Number(dayValue) + Number(dailyHabitValue) + Number(dayHabitValue);
});
output.push([dayValue]);
}
return output;
}
Notes:
This function can be used as any in-built formula from Sheets (e.g. =HABIT_TOTALS(4,7,8)).
This function gets, as arguments, the indexes of the habits to retrieve (in this case 4, 7, 8), to be found on the first row in the sheet.
It loops through all days of the week (dayIndex), returning the total amount for each day. Because of this, there's no need to drag the formula down.
For each day, it finds the column index based on the habit index provided as an argument, and adds the values for Daily and for the current day to the total value for the day.
After retrieving the total amount for the day, this value is pushed to output, the value returned by this function.
This function could be used for the Food habits, just changing the arguments: =HABIT_TOTALS(5,6), or for any other combination.
Reference:
Custom Functions in Google Sheets
Spread syntax (...)
For the calculation concerning food you can try in cell D18
=sum(filter(filter($D$3:$I$11, regexmatch($C$3:$C$11, "Daily|"&text($C18, "ddd"))), regexmatch($D$1:$I$1&"", "5|6")))
and fill down.
The numbers at the end refer to the colum numbers you have in row 1. So in E18 (Sleep and excercise) you would have
=sum(filter(filter($D$3:$I$11, regexmatch($C$3:$C$11, "Daily|"&text($C18, "ddd"))), regexmatch($D$1:$I$1&"", "4|7|8")))
Of course, it is also possible to write the last part in a cell and then refer to that cell. That would mean you can enter in E18
=sum(filter(filter($D$3:$I$11, regexmatch($C$3:$C$11, "Daily|"&text($C18, "ddd"))), regexmatch($D$1:$I$1&"", D$17)))
and fill down AND to the right.
See if that helps?

How to dynamically increase or decrease limit value in Query of Google sheet

So my task is to get last x rows based on some count calculation and for that, I'm using limit offset in a query. To elaborate more, I have this formula that works fine
=COUNTIF(QUERY(A:A,"limit 5 offset "&(COUNT(A:A)-5),A1)>1
The problem here is that I don't want to "Hardcode" that value of "5" since my range changes based on the count of some timestamp function I'm using on another column. SO I made some changes in above formula by adding another count function that checks the other column and changes range each time.
=COUNTIF(QUERY(A:A,"limit COUNT(FILTER(D:D,D:D>NOW()-6/24)) offset "&(COUNT(A:A)-COUNT(FILTER(D:D,D:D>NOW()-6/24)))),A1)>1
The main issue in this formula is this line
"limit COUNT(FILTER(D:D,D:D>NOW()-6/24)) offset "
as I can't use count function in limit+offset apparently. How can I change the syntax to run limit, offset query?
Edit: This doesn't work as well
"limit"+COUNT(FILTER(D:D,D:D>NOW()-6/24))+"offset"
try like this:
=COUNTIF(QUERY(A:A,
"limit "&COUNT(FILTER(D:D,D:D>NOW()-6/24))&"
offset "&(COUNT(A:A)-COUNT(FILTER(D:D,D:D>NOW()-6/24)))),A1)>1

Google sheets query to remove header not working with offset

Can't figure out why some of these queries work and some don't. Just trying to build a table of my own
=query(TRANSPOSE(ImportHtml(C7, "table",1)),"select * limit 1 offset 1")
=query(TRANSPOSE(ImportHtml(C2, "table",1)),"select * limit 1 offset 1")
Where c2 = http://www.vitalmtb.com/product/compare/2819 and C7 = http://www.vitalmtb.com/product/compare/2775
The ones not working bring back the row of headers, when I want to remove the headers and leave the data.
The query formula has the third, optional parameter: the number of header rows. If it's not provided, a guess is made to what rows are headers (usually the top one is). In your first example, its guess is that there is 1 header row.
The header row is always a part of the array returned by query, which uses it to label the returned columns (unless you override that by providing different headers with label). This is why you can't get rid of it by changing offset: the offset determines with data rows to return; the header row is present regardless.
It you want to get rid of headers, set the third parameter to 0, so that the headers are treated as data. Then offset will offset past them:
=query(TRANSPOSE(ImportHtml(C2, "table", 1)), "select * limit 1 offset 1", 0)

Resources