Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable/Disable Auto-Filters on Tables #1796

Open
edwardfward opened this issue Jan 27, 2024 · 3 comments
Open

Enable/Disable Auto-Filters on Tables #1796

edwardfward opened this issue Jan 27, 2024 · 3 comments
Labels
enhancement New feature or request

Comments

@edwardfward
Copy link
Contributor

edwardfward commented Jan 27, 2024

Recommend providing an AutoFilters flag on excelize.Table to disable auto-filters on table creation to allow a custom selection of columns to enable filters using AutoFilter.

Here is an example of how this could work.

// add data to "A1:J10"

err := f.AddTable("Sheet2", &excelize.Table{
    Range:               "A1:J10",
    Name:                "table",
    StyleName:           "TableStyleMedium2",
    ShowFirstColumn:     true,
    ShowLastColumn:      true,
    ShowRowStripes:      &disable,
    ShowColumnStripes:   true,
    DisableAutoFilters:  true,  // filters disabled.
})

// Create filters on first three columns.
err := f.AutoFilter("Sheet1", "A1:C1", []excelize.AutoFilterOptions{})

Sample output.

Screenshot 2024-01-27 at 1 54 48 PM

I achieved the above using the following:

	if err := f.AutoFilter("Sheet2", "A1:C1", nil); err != nil {
		fmt.Println(err)
	}

	// Create table.
	show := true
	if err = f.AddTable("Sheet2", &excelize.Table{
		Name:          "Table1",
		Range:         fmt.Sprintf("%s:%s", firstCell, currentCell),
		ShowHeaderRow: &show,
	}); err != nil {
		fmt.Println(err)
	}

But the generated XLSX file was corrupted with the following error:

<removedFeatures summary="Following is a list of removed features:">
<removedFeature>Removed Feature: AutoFilter from /xl/tables/table1.xml part (Table)</removedFeature>
<removedFeature>Removed Feature: Table from /xl/tables/table1.xml part (Table)</removedFeature></removedFeatures></recoveryLog>

The file recovered and the filters work, but I do not want to scare users with a "corrupt" XLSX error.

Screenshot 2024-01-27 at 2 00 43 PM
@edwardfward
Copy link
Contributor Author

edwardfward commented Jan 27, 2024

I made the following adjustments and recommend providing a slice of column indexes where a
filter should be visible.

// xmlTable.go

// Table directly maps the format settings of the table.
type Table struct {
	tID               int
	rID               string
	tableXML          string
	Range             string
	Name              string
	StyleName         string
	ShowColumnStripes bool
	ShowFirstColumn   bool
	ShowHeaderRow     *bool
	ShowLastColumn    bool
	ShowRowStripes    *bool
	FilterColumns     []int  // column indexes to enable filter button
}
        // table.go

	_ = f.setTableColumns(sheet, !hideHeaderRow, x1, y1, x2, &t)
	if hideHeaderRow {
		t.AutoFilter = nil
		t.HeaderRowCount = intPtr(0)
	}

        // add filter columns (ln 371)

	if !hideHeaderRow && opts.FilterColumns != nil {
		width := x2 - x1 + 1
		ff := make([]*xlsxFilterColumn, width)

		cc := make(map[int]bool)
		for _, c := range opts.FilterColumns {
			cc[c] = true
		}

		for i := 0; i < width; i++ {
			showFilter := false
			if cc[i] {
				showFilter = true
			}

			ff[i] = &xlsxFilterColumn{
				ColID:      i,
				HiddenButton: !showFilter,
			}
		}

		t.AutoFilter = &xlsxAutoFilter{
			Ref:          ref,
			FilterColumn: ff,
		}
	}
   // main.go

	// Create table.
	show := true
	if err = f.AddTable("Sheet2", &excelize.Table{
		Name:          "Table1",
		Range:         fmt.Sprintf("%s:%s", firstCell, currentCell),
		ShowHeaderRow: &show,
		FilterColumns: []int{0, 1, 2},
	}); err != nil {
		fmt.Println(err)
	}

Outcome (no corruption errors):

Screenshot 2024-01-27 at 5 12 31 PM

@xuri xuri added the enhancement New feature or request label Jan 31, 2024
@edwardfward
Copy link
Contributor Author

@xuri Happy to take this and submit a PR if you feel it merits an enhancement.

@xuri
Copy link
Member

xuri commented Feb 4, 2024

Contributions are welcome. I need some time to clarify exactly how to support this feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants