Skip to content

Filtering Rows - BareFrame.filter

BareFrame.filter returns a new BareFrame containing only the rows for which the predicate function returns true.

JavaScript
const adults = df.filter(row => row.get("age") >= 18);

Without gaslamp

Filtering rows from getValues() output requires index lookups to access each value. The column positions must be known in advance and hardcoded:

JavaScript
// Sheet columns: name(0), age(1), department(2)
const values = sheet.getDataRange().getValues();
const headers = values[0];  // first row is the header
const rows = values.slice(1);  // remaining rows are data

// Keep only rows where age >= 18
const adults = rows.filter(row => row[1] >= 18);  // age is at index 1

If a column is added or reordered in the sheet, every index must be updated manually.


With gaslamp

filter uses column names instead of indices. The predicate receives each row as a Map, and you access values by name with row.get:

JavaScript
const df = gaslamp.BareFrame.fromSheet(sheet);
const adults = df.filter(row => row.get("age") >= 18);

Using the Row Index

The predicate also receives the 0-based row index as a second argument. Use it when you need to filter by position:

JavaScript
// Keep only the first 10 rows
const top10 = df.filter((row, i) => i < 10);

Using Expression

The same condition can also be written with Expression for a more declarative style:

JavaScript
const adults = df.filter(gaslamp.Expression.col("age").ge(18).toFunction());

Use an arrow function for simple or custom logic, and Expression when combining multiple conditions with AND / OR.

See Expression Filtering for the full syntax.


Chaining Filters

filter returns a new BareFrame, so you can chain multiple conditions:

JavaScript
const df = gaslamp.BareFrame.fromSheet(sheet);

const result = df
  .filter(row => row.get("status") === "active")
  .filter(row => row.get("age") >= 18);

Note: Each filter call internally converts the data from column-oriented to row-oriented and back. Chaining many filter calls adds conversion overhead on each step. If you need to apply multiple conditions at once, combine them in a single predicate or use LazyFrame to batch operations:

JavaScript
// Preferred: single predicate for multiple conditions
const result = df.filter(row =>
  row.get("status") === "active" && row.get("age") >= 18
);

Empty Results

If no rows match the predicate, filter returns an empty BareFrame — it does not throw an error. Always check .length before processing the result if an empty frame is unexpected:

JavaScript
const result = df.filter(row => row.get("score") > 100);

if (result.length === 0) {
  console.warn("No matching rows.");
}

Practical Workflow

Read from a sheet, filter rows, and write the result to another sheet:

JavaScript
function workflow_filter() {
  const book = SpreadsheetApp.getActiveSpreadsheet();
  const inputSheet  = book.getSheetByName("Employees");
  const outputSheet = book.getSheetByName("Active Seniors")
    ?? book.insertSheet("Active Seniors");

  const df = gaslamp.BareFrame.fromSheet(inputSheet);

  const result = df
    .filter(row => row.get("status") === "active")
    .filter(row => row.get("age") >= 40);

  result.toSheet(outputSheet);
  console.log(`Exported ${result.length} rows.`);
}

Next Steps