Skip to content

Extracting Columns - BareFrame.select

BareFrame.select returns a new BareFrame containing only the columns you specify.

JavaScript
const subset = df.select(["name", "age"]);

Without gaslamp

Extracting specific columns from getValues() output requires index lookups. The column positions must be known in advance and hardcoded:

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

// Extract name and age only
const result = rows.map(row => [row[0], row[1]]);

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


With gaslamp

select uses column names instead of indices. The code remains correct even if the sheet gains new columns or changes order.

JavaScript
const df = gaslamp.BareFrame.fromSheet(sheet);
const subset = df.select(["name", "age"]);

Reordering Columns

The output column order matches the order of the headers array, not the original sheet order. This makes it easy to reorder columns without any additional steps:

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

// Original order: name, age, department, salary
// Reorder to: department, name
const reordered = df.select(["department", "name"]);

Without gaslamp, reordering requires rebuilding each row manually:

JavaScript
// Without gaslamp: swap column order explicitly
const result = rows.map(row => [row[2], row[0]]);  // department, name

Error Handling

select throws an error immediately if a column name does not exist:

JavaScript
df.select(["missing"]);
// Error: BareFrame.select: column "missing" does not exist

This makes typos and schema mismatches visible right away, rather than silently returning wrong data.


Practical Workflow

Read from a sheet, extract the columns you need, and write to another sheet:

JavaScript
function workflow_select() {
  const book = SpreadsheetApp.getActiveSpreadsheet();
  const inputSheet  = book.getSheetByName("Employees");
  const outputSheet = book.getSheetByName("Summary")
    ?? book.insertSheet("Summary");

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

  // Extract only the columns needed for the summary sheet
  const summary = df.select(["name", "department", "salary"]);

  summary.toSheet(outputSheet);
  console.log(`Exported ${summary.length} rows, ${summary.headers.length} columns.`);
}

Next Steps