Skip to content

Feature request: GetNonEmptyCells(sheet) — sparse cell access without iterating empty cells #2363

Description

@mcschmitz

Description

Add a method that returns only non-empty cells as structured data (row, column, value), skipping empty cells entirely. This avoids forcing callers to iterate through potentially millions of empty cells just to build a sparse grid.

Use case

When building data extraction pipelines (e.g., converting spreadsheets to structured data for LLM processing), the typical pattern is:

  1. GetRows(sheet)
  2. Iterate in caller's language, skip empty cells
  3. Build a sparse map: (row, col) -> value

For a sheet with dimension A1:Z5000 (130k potential cells) but only 20k populated cells, we're iterating 110k empty cells for nothing. The Go library already knows which cells have values during XML parsing.

Proposed API

type CellInfo struct {
    Row   int
    Col   int
    Value string
}

// GetNonEmptyCells returns all non-empty cells in a worksheet as a slice of CellInfo.
// Options control formatting behavior (same as GetRows).
func (f *File) GetNonEmptyCells(sheet string, opts ...Options) ([]CellInfo, error)

Or alternatively, a streaming version:

// NonEmptyCells returns an iterator over non-empty cells in a worksheet.
func (f *File) NonEmptyCells(sheet string, opts ...Options) (*CellIterator, error)

Why this should live in excelize

  • Go already parses XML and knows which cells have values — filtering in Go avoids crossing CGo boundary for empty cells
  • Reduces memory: callers don't need to hold [][]string with all the empty padding
  • Common pattern: every user building sparse representations does this filtering manually
  • Complements the existing Rows streaming iterator for cases where you want sparse access

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions