func (f *File) writeCustomFilter(filter *xlsxAutoFilter, operator int, val string) {
	operators := map[int]string{
		1:  "lessThan",
		2:  "equal",
		3:  "lessThanOrEqual",
		4:  "greaterThan",
		5:  "notEqual",
		6:  "greaterThanOrEqual",
		22: "equal",
	}
	customFilter := xlsxCustomFilter{
		Operator: operators[operator],
		Val:      val,
	}
	if filter.FilterColumn.CustomFilters != nil {
		filter.FilterColumn.CustomFilters.CustomFilter = append(filter.FilterColumn.CustomFilters.CustomFilter, &customFilter)
	} else {
		customFilters := []*xlsxCustomFilter{}
		customFilters = append(customFilters, &customFilter)
		filter.FilterColumn.CustomFilters = &xlsxCustomFilters{CustomFilter: customFilters}
	}
}
writeCustomFilter provides function to write the <customFilter> element.
package excelize
import (
	"encoding/json"
	"encoding/xml"
	"fmt"
	"regexp"
	"strconv"
	"strings"
)
func parseFormatTableSet(formatSet string) *formatTable {
	format := formatTable{
		TableStyle:     "",
		ShowRowStripes: true,
	}
	json.Unmarshal([]byte(formatSet), &format)
	return &format
}
func (f *File) AddTable(sheet, hcell, vcell, format string) {
	formatSet := parseFormatTableSet(format)
	hcell = strings.ToUpper(hcell)
	vcell = strings.ToUpper(vcell)
	hcol := string(strings.Map(letterOnlyMapF, hcell))
	hrow, _ := strconv.Atoi(strings.Map(intOnlyMapF, hcell))
	hyAxis := hrow - 1
	hxAxis := TitleToNumber(hcol)
	vcol := string(strings.Map(letterOnlyMapF, vcell))
	vrow, _ := strconv.Atoi(strings.Map(intOnlyMapF, vcell))
	vyAxis := vrow - 1
	vxAxis := TitleToNumber(vcol)
	if vxAxis < hxAxis {
		vxAxis, hxAxis = hxAxis, vxAxis
	}
	if vyAxis < hyAxis {
		vyAxis, hyAxis = hyAxis, vyAxis
	}
	tableID := f.countTables() + 1
	sheetRelationshipsTableXML := "../tables/table" + strconv.Itoa(tableID) + ".xml"
	tableXML := strings.Replace(sheetRelationshipsTableXML, "..", "xl", -1)
	rID := f.addSheetRelationships(sheet, SourceRelationshipTable, sheetRelationshipsTableXML, "")
	f.addSheetTable(sheet, rID)
	f.addTable(sheet, tableXML, hxAxis, hyAxis, vxAxis, vyAxis, tableID, formatSet)
	f.addContentTypePart(tableID, "table")
}
func (f *File) countTables() int {
	count := 0
	for k := range f.XLSX {
		if strings.Contains(k, "xl/tables/table") {
			count++
		}
	}
	return count
}
func (f *File) addSheetTable(sheet string, rID int) {
	xlsx := f.workSheetReader(sheet)
	table := &xlsxTablePart{
		RID: "rId" + strconv.Itoa(rID),
	}
	if xlsx.TableParts == nil {
		xlsx.TableParts = &xlsxTableParts{}
	}
	xlsx.TableParts.Count++
	xlsx.TableParts.TableParts = append(xlsx.TableParts.TableParts, table)
}
func (f *File) addTable(sheet, tableXML string, hxAxis, hyAxis, vxAxis, vyAxis, i int, formatSet *formatTable) {
	if hyAxis == vyAxis {
		vyAxis++
	}
	ref := ToAlphaString(hxAxis) + strconv.Itoa(hyAxis+1) + ":" + ToAlphaString(vxAxis) + strconv.Itoa(vyAxis+1)
	tableColumn := []*xlsxTableColumn{}
	idx := 0
	for i := hxAxis; i <= vxAxis; i++ {
		idx++
		cell := ToAlphaString(i) + strconv.Itoa(hyAxis+1)
		name := f.GetCellValue(sheet, cell)
		if _, err := strconv.Atoi(name); err == nil {
			f.SetCellStr(sheet, cell, name)
		}
		if name == "" {
			name = "Column" + strconv.Itoa(idx)
			f.SetCellStr(sheet, cell, name)
		}
		tableColumn = append(tableColumn, &xlsxTableColumn{
			ID:   idx,
			Name: name,
		})
	}
	name := formatSet.TableName
	if name == "" {
		name = "Table" + strconv.Itoa(i)
	}
	t := xlsxTable{
		XMLNS:       NameSpaceSpreadSheet,
		ID:          i,
		Name:        name,
		DisplayName: name,
		Ref:         ref,
		AutoFilter: &xlsxAutoFilter{
			Ref: ref,
		},
		TableColumns: &xlsxTableColumns{
			Count:       idx,
			TableColumn: tableColumn,
		},
		TableStyleInfo: &xlsxTableStyleInfo{
			Name:              formatSet.TableStyle,
			ShowFirstColumn:   formatSet.ShowFirstColumn,
			ShowLastColumn:    formatSet.ShowLastColumn,
			ShowRowStripes:    formatSet.ShowRowStripes,
			ShowColumnStripes: formatSet.ShowColumnStripes,
		},
	}
	table, _ := xml.Marshal(t)
	f.saveFileList(tableXML, table)
}
func parseAutoFilterSet(formatSet string) *formatAutoFilter {
	format := formatAutoFilter{}
	json.Unmarshal([]byte(formatSet), &format)
	return &format
}
func (f *File) AutoFilter(sheet, hcell, vcell, format string) error {
	formatSet := parseAutoFilterSet(format)
	hcell = strings.ToUpper(hcell)
	vcell = strings.ToUpper(vcell)
	hcol := string(strings.Map(letterOnlyMapF, hcell))
	hrow, _ := strconv.Atoi(strings.Map(intOnlyMapF, hcell))
	hyAxis := hrow - 1
	hxAxis := TitleToNumber(hcol)
	vcol := string(strings.Map(letterOnlyMapF, vcell))
	vrow, _ := strconv.Atoi(strings.Map(intOnlyMapF, vcell))
	vyAxis := vrow - 1
	vxAxis := TitleToNumber(vcol)
	if vxAxis < hxAxis {
		vxAxis, hxAxis = hxAxis, vxAxis
	}
	if vyAxis < hyAxis {
		vyAxis, hyAxis = hyAxis, vyAxis
	}
	ref := ToAlphaString(hxAxis) + strconv.Itoa(hyAxis+1) + ":" + ToAlphaString(vxAxis) + strconv.Itoa(vyAxis+1)
	refRange := vxAxis - hxAxis
	err := f.autoFilter(sheet, ref, refRange, hxAxis, formatSet)
	return err
}
func (f *File) autoFilter(sheet, ref string, refRange, hxAxis int, formatSet *formatAutoFilter) error {
	xlsx := f.workSheetReader(sheet)
	if xlsx.SheetPr != nil {
		xlsx.SheetPr.FilterMode = true
	}
	xlsx.SheetPr = &xlsxSheetPr{FilterMode: true}
	filter := &xlsxAutoFilter{
		Ref: ref,
	}
	xlsx.AutoFilter = filter
	if formatSet.Column == "" || formatSet.Expression == "" {
		return nil
	}
	col := TitleToNumber(formatSet.Column)
	offset := col - hxAxis
	if offset < 0 || offset > refRange {
		return fmt.Errorf("Incorrect index of column '%s'", formatSet.Column)
	}
	filter.FilterColumn = &xlsxFilterColumn{
		ColID: offset,
	}
	re := regexp.MustCompile(`"(?:[^"]|"")*"|\S+`)
	token := re.FindAllString(formatSet.Expression, -1)
	if len(token) != 3 && len(token) != 7 {
		return fmt.Errorf("Incorrect number of tokens in criteria '%s'", formatSet.Expression)
	}
	expressions, tokens, err := f.parseFilterExpression(formatSet.Expression, token)
	if err != nil {
		return err
	}
	f.writeAutoFilter(filter, expressions, tokens)
	xlsx.AutoFilter = filter
	return nil
}
// writeAutoFilter provides function to check for single or double custom filters
// as default filters and handle them accordingly.
func (f *File) writeAutoFilter(filter *xlsxAutoFilter, exp []int, tokens []string) {
	if len(exp) == 1 && exp[0] == 2 {
		// Single equality.
		filters := []*xlsxFilter{}
		filters = append(filters, &xlsxFilter{Val: tokens[0]})
		filter.FilterColumn.Filters = &xlsxFilters{Filter: filters}
	} else if len(exp) == 3 && exp[0] == 2 && exp[1] == 1 && exp[2] == 2 {
		// Double equality with "or" operator.
		filters := []*xlsxFilter{}
		for _, v := range tokens {
			filters = append(filters, &xlsxFilter{Val: v})
		}
		filter.FilterColumn.Filters = &xlsxFilters{Filter: filters}
	} else {
		expRel := map[int]int{0: 0, 1: 2}
		andRel := map[int]bool{0: true, 1: false}
		for k, v := range tokens {
			f.writeCustomFilter(filter, exp[expRel[k]], v)
			if k == 1 {
				filter.FilterColumn.CustomFilters.And = andRel[exp[k]]
			}
		}
	}
}