Google Apps Script

Class ScriptDbResult

An object that provides iterator-like access to the results of a database query. Use the methods hasNext() and next() in a loop to access all the results.

 
var db = ScriptDb.getMyDb();
 var results = db.query({type: 'person'});
 while (results.hasNext()) {
   var item = results.next();
   // Do something with the item.
 }
 

Methods

MethodReturn typeBrief description
getSize()IntegerReturns the total number of items in the query results.
hasNext()BooleanReturns true if there are more items left in the query results.
limit(number)ScriptDbResultLimits the number of items in the query results.
next()ScriptDbMapReturns the next item in the query results.
paginate(pageNumber, pageSize)ScriptDbResultLimits the query results to a single page of items, using the passed in page number and size.
sortBy(fieldPath)ScriptDbResultSorts the query results by the specified field, lexically and ascending.
sortBy(fieldPath, direction)ScriptDbResultSorts the query results by the specified field, lexically and using the passed in direction.
sortBy(fieldPath, direction, strategy)ScriptDbResultSorts the query results by the specified field, using the passed in direction and strategy.
sortBy(fieldPath, strategy)ScriptDbResultSorts the query results by the specified field, ascending and using the passed in strategy.
startAt(number)ScriptDbResultStarts the query results with the item at the passed in index.

Detailed documentation

getSize()

Returns the total number of items in the query results.

 
var db = ScriptDb.getMyDb();
 var results = db.query({type: 'person'});
 Logger.log('Number of people: ' + results.getSize());
 
Please note that after calling this method you will no longer be able to adjust the sorting, paging, startAt, or limit settings of the results.

Return

Integer — the total number of items in the query results


hasNext()

Returns true if there are more items left in the query results.

 
var db = ScriptDb.getMyDb();
 var results = db.query({type: 'person'});
 while (results.hasNext()) {
   var item = results.next();
   // Do something with the item.
 }
 
Please note that after calling this method you will no longer be able to adjust the sorting, paging, startAt, or limit settings of the results.

Return

Boolean — true if there are more items left in the query results, false if all items have been retrieved


limit(number)

Limits the number of items in the query results.

 
var db = ScriptDb.getMyDb();
 // Get the first ten people.
 var results = db.query({type: 'person'}).limit(10);
 while (results.hasNext()) {
   var item = results.next();
   // Do something with the item.
 }
 
The default limit is 200 and the maximum limit allowed is 50,000.

Parameters

NameTypeDescription
numberIntegerthe maximum number of items to return

Return

ScriptDbResult — the results, for chaining


next()

Returns the next item in the query results. Once this method called it's not possible to go back to a previous item.

 
var db = ScriptDb.getMyDb();
 var results = db.query({type: 'person'});
 while (results.hasNext()) {
   var item = results.next();
   // Do something with the item.
 }
 
Please note that after calling this method you will no longer be able to adjust the sorting, paging, startAt, or limit settings of the results.

Return

ScriptDbMap — the next item in the query results


paginate(pageNumber, pageSize)

Limits the query results to a single page of items, using the passed in page number and size. Please note that page numbers start at zero.

 
var db = ScriptDb.getMyDb();
 var pageNumber = 0;
 var pageSize = 10;
 var page;
 do {
   page = db.query({type: 'person'}).paginate(pageNumber, pageSize);
   var items = [];
   while (page.hasNext()) {
     items.push(page.next());
   }
   // Do something with the page of items.
   pageNumber++;
 } while (page.getSize() == pageSize);
 

Parameters

NameTypeDescription
pageNumberIntegerthe page of results to return
pageSizeIntegerthe number of items per page

Return

ScriptDbResult — the results, for chaining


sortBy(fieldPath)

Sorts the query results by the specified field, lexically and ascending. To sort by an inner field specify the dot-separated path to the field (for example "business.address.city").

 
var db = ScriptDb.getMyDb();
 // Get people sorted by name.
 var results = db.query({type: 'person'}).sortBy('name');
 while (results.hasNext()) {
   var item = results.next();
   // Do something with the item.
 }
 
Please note, sorting by multiple fields is not supported, and only the last call to sortBy will be used.

Parameters

NameTypeDescription
fieldPathStringthe name or path of the field to sort by

Return

ScriptDbResult — the results, for chaining


sortBy(fieldPath, direction)

Sorts the query results by the specified field, lexically and using the passed in direction.

 
var db = ScriptDb.getMyDb();
 // Get people sorted by name, in reverse alphabetical order.
 var results = db.query({type: 'person'}).sortBy('name', db.DESCENDING);
 while (results.hasNext()) {
   var item = results.next();
   // Do something with the item.
 }
 

Parameters

NameTypeDescription
fieldPathStringthe name or path of the field to sort by
directionSortDirectionthe direction (ascending or descending) of the sort

Return

ScriptDbResult — the results, for chaining


sortBy(fieldPath, direction, strategy)

Sorts the query results by the specified field, using the passed in direction and strategy.

 
var db = ScriptDb.getMyDb();
 // Get people sorted by age, oldest to youngest.
 var results = db.query({type: 'person'}).sortBy('age', db.DESCENDING, db.NUMERIC);
 while (results.hasNext()) {
   var item = results.next();
   // Do something with the item.
 }
 

Parameters

NameTypeDescription
fieldPathStringthe name or path of the field to sort by
directionSortDirectionthe direction (ascending or descending) of the sort
strategySortStrategythe strategy used (lexical or numeric) to order the values of the field

Return

ScriptDbResult — the results, for chaining


sortBy(fieldPath, strategy)

Sorts the query results by the specified field, ascending and using the passed in strategy.

 
var db = ScriptDb.getMyDb();
 // Get people sorted by age.
 var results = db.query({type: 'person'}).sortBy('age', db.NUMERIC);
 while (results.hasNext()) {
   var item = results.next();
   // Do something with the item.
 }
 

Parameters

NameTypeDescription
fieldPathStringthe name or path of the field to sort by
strategySortStrategythe strategy used (lexical or numeric) to order the values of the field

Return

ScriptDbResult — the results, for chaining


startAt(number)

Starts the query results with the item at the passed in index.

 
var db = ScriptDb.getMyDb();
 // Get all but the first ten people.
 var results = db.query({type: 'person'}).startAt(10);
 while (results.hasNext()) {
   var item = results.next();
   // Do something with the item.
 }
 

Parameters

NameTypeDescription
numberIntegerthe index to start the results at

Return

ScriptDbResult — the results, for chaining

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.