Skip to content

Feature request: Option to expand merged cell values in GetRows/Rows #2364

Description

@mcschmitz

Description

Add an option to GetRows and Rows (streaming iterator) that expands merged cell values — filling all cells in a merged range with the anchor cell's value instead of returning empty strings for non-anchor cells.

Use case

When reading a sheet with merged cells, the current behavior returns the value only in the top-left (anchor) cell of the merged range. All other cells in the range return empty strings. To get the "visual" representation of the spreadsheet (what a user sees), callers must:

  1. Call GetRows(sheet)
  2. Call GetMergeCells(sheet)
  3. Parse each range string (e.g., "A1:C3") into coordinates
  4. Expand: for each cell in each merged range, fill with anchor value
  5. Merge the expanded values back into the row data

This is ~30 lines of boilerplate that every caller doing data extraction needs. Addimidedly, this is mostly useful right now for parsing excel files as mardkown (i.e. to pass it to an LLM)

Proposed API

type Options struct {
    // ... existing fields ...
    
    // ExpandMergedCells fills all cells in a merged range with the anchor cell's value.
    // Default: false (current behavior preserved).
    ExpandMergedCells bool
}

Usage:

rows, err := f.GetRows("Sheet1", excelize.Options{ExpandMergedCells: true})
// Now merged ranges have the anchor value in every cell, not just the top-left

Current workaround

// Pseudocode of what every caller does today:
rows, _ := f.GetRows(sheet)
merged, _ := f.GetMergeCells(sheet)
for _, mc := range merged {
    startAxis := mc.GetStartAxis()
    endAxis := mc.GetEndAxis()
    value := mc.GetCellValue()
    // Parse axes into row/col coordinates
    // Fill all cells in range with value
}

Why this should live in excelize

  • The merge metadata is already loaded during file parsing
  • Expanding during row iteration avoids a second pass over the merge list
  • Every data extraction use case needs the "visual" representation, not the raw storage representation
  • The Options struct already exists for controlling row reading behavior — this fits naturally

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