2019-12-29 16:02:31 +08:00
|
|
|
// Copyright 2016 - 2020 The excelize Authors. All rights reserved. Use of
|
2018-09-14 00:44:23 +08:00
|
|
|
// this source code is governed by a BSD-style license that can be found in
|
|
|
|
// the LICENSE file.
|
|
|
|
//
|
|
|
|
// Package excelize providing a set of functions that allow you to write to
|
2020-06-22 00:14:56 +08:00
|
|
|
// and read from XLSX / XLSM / XLTM files. Supports reading and writing
|
|
|
|
// spreadsheet documents generated by Microsoft Exce™ 2007 and later. Supports
|
|
|
|
// complex components by high compatibility, and provided streaming API for
|
|
|
|
// generating or reading data from a worksheet with huge amounts of data. This
|
|
|
|
// library needs Go version 1.10 or later.
|
2018-09-14 00:58:48 +08:00
|
|
|
|
2017-04-28 15:49:41 +08:00
|
|
|
package excelize
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"encoding/xml"
|
2017-06-08 11:11:11 +08:00
|
|
|
"fmt"
|
|
|
|
"regexp"
|
2017-04-28 15:49:41 +08:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2018-08-06 10:21:24 +08:00
|
|
|
// parseFormatTableSet provides a function to parse the format settings of the
|
2017-04-28 15:49:41 +08:00
|
|
|
// table with default value.
|
2018-05-27 11:25:55 +08:00
|
|
|
func parseFormatTableSet(formatSet string) (*formatTable, error) {
|
2017-04-28 15:49:41 +08:00
|
|
|
format := formatTable{
|
|
|
|
TableStyle: "",
|
|
|
|
ShowRowStripes: true,
|
|
|
|
}
|
2018-07-13 17:40:47 +08:00
|
|
|
err := json.Unmarshal(parseFormatSet(formatSet), &format)
|
2018-05-27 11:25:55 +08:00
|
|
|
return &format, err
|
2017-04-28 15:49:41 +08:00
|
|
|
}
|
|
|
|
|
2017-09-13 22:17:40 +08:00
|
|
|
// AddTable provides the method to add table in a worksheet by given worksheet
|
|
|
|
// name, coordinate area and format set. For example, create a table of A1:D5
|
2017-04-28 15:49:41 +08:00
|
|
|
// on Sheet1:
|
|
|
|
//
|
2019-04-20 14:57:50 +08:00
|
|
|
// err := f.AddTable("Sheet1", "A1", "D5", ``)
|
2017-04-28 15:49:41 +08:00
|
|
|
//
|
|
|
|
// Create a table of F2:H6 on Sheet2 with format set:
|
|
|
|
//
|
2019-04-20 14:57:50 +08:00
|
|
|
// err := f.AddTable("Sheet2", "F2", "H6", `{"table_name":"table","table_style":"TableStyleMedium2", "show_first_column":true,"show_last_column":true,"show_row_stripes":false,"show_column_stripes":true}`)
|
2017-04-28 15:49:41 +08:00
|
|
|
//
|
2020-02-07 00:25:01 +08:00
|
|
|
// Note that the table must be at least two lines including the header. The
|
|
|
|
// header cells must contain strings and must be unique, and must set the
|
|
|
|
// header row data of the table before calling the AddTable function. Multiple
|
|
|
|
// tables coordinate areas that can't have an intersection.
|
2018-05-04 11:20:51 +08:00
|
|
|
//
|
|
|
|
// table_name: The name of the table, in the same worksheet name of the table should be unique
|
2017-04-28 15:49:41 +08:00
|
|
|
//
|
|
|
|
// table_style: The built-in table style names
|
|
|
|
//
|
|
|
|
// TableStyleLight1 - TableStyleLight21
|
|
|
|
// TableStyleMedium1 - TableStyleMedium28
|
|
|
|
// TableStyleDark1 - TableStyleDark11
|
|
|
|
//
|
2018-05-27 11:25:55 +08:00
|
|
|
func (f *File) AddTable(sheet, hcell, vcell, format string) error {
|
|
|
|
formatSet, err := parseFormatTableSet(format)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-04-28 15:49:41 +08:00
|
|
|
// Coordinate conversion, convert C1:B3 to 2,0,1,2.
|
2019-03-23 20:08:06 +08:00
|
|
|
hcol, hrow, err := CellNameToCoordinates(hcell)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
vcol, vrow, err := CellNameToCoordinates(vcell)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-04-28 15:49:41 +08:00
|
|
|
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
if vcol < hcol {
|
|
|
|
vcol, hcol = hcol, vcol
|
2017-04-28 15:49:41 +08:00
|
|
|
}
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
|
|
|
|
if vrow < hrow {
|
|
|
|
vrow, hrow = hrow, vrow
|
2017-04-28 15:49:41 +08:00
|
|
|
}
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
|
2017-04-28 15:49:41 +08:00
|
|
|
tableID := f.countTables() + 1
|
|
|
|
sheetRelationshipsTableXML := "../tables/table" + strconv.Itoa(tableID) + ".xml"
|
|
|
|
tableXML := strings.Replace(sheetRelationshipsTableXML, "..", "xl", -1)
|
|
|
|
// Add first table for given sheet.
|
2019-12-23 00:07:40 +08:00
|
|
|
sheetRels := "xl/worksheets/_rels/" + strings.TrimPrefix(f.sheetMap[trimSheetName(sheet)], "xl/worksheets/") + ".rels"
|
2019-09-16 01:17:35 +08:00
|
|
|
rID := f.addRels(sheetRels, SourceRelationshipTable, sheetRelationshipsTableXML, "")
|
2020-07-18 15:15:16 +08:00
|
|
|
if err = f.addSheetTable(sheet, rID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
f.addSheetNameSpace(sheet, SourceRelationship)
|
|
|
|
if err = f.addTable(sheet, tableXML, hcol, hrow, vcol, vrow, tableID, formatSet); err != nil {
|
2019-03-23 20:08:06 +08:00
|
|
|
return err
|
|
|
|
}
|
2017-05-24 14:17:35 +08:00
|
|
|
f.addContentTypePart(tableID, "table")
|
2018-05-27 11:25:55 +08:00
|
|
|
return err
|
2017-04-28 15:49:41 +08:00
|
|
|
}
|
|
|
|
|
2018-08-06 10:21:24 +08:00
|
|
|
// countTables provides a function to get table files count storage in the
|
|
|
|
// folder xl/tables.
|
2017-04-28 15:49:41 +08:00
|
|
|
func (f *File) countTables() int {
|
|
|
|
count := 0
|
|
|
|
for k := range f.XLSX {
|
|
|
|
if strings.Contains(k, "xl/tables/table") {
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
2018-08-06 10:21:24 +08:00
|
|
|
// addSheetTable provides a function to add tablePart element to
|
2017-09-13 22:00:33 +08:00
|
|
|
// xl/worksheets/sheet%d.xml by given worksheet name and relationship index.
|
2020-07-18 15:15:16 +08:00
|
|
|
func (f *File) addSheetTable(sheet string, rID int) error {
|
|
|
|
ws, err := f.workSheetReader(sheet)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-04-28 15:49:41 +08:00
|
|
|
table := &xlsxTablePart{
|
|
|
|
RID: "rId" + strconv.Itoa(rID),
|
|
|
|
}
|
2020-07-18 15:15:16 +08:00
|
|
|
if ws.TableParts == nil {
|
|
|
|
ws.TableParts = &xlsxTableParts{}
|
2017-04-28 15:49:41 +08:00
|
|
|
}
|
2020-07-18 15:15:16 +08:00
|
|
|
ws.TableParts.Count++
|
|
|
|
ws.TableParts.TableParts = append(ws.TableParts.TableParts, table)
|
|
|
|
return err
|
2017-04-28 15:49:41 +08:00
|
|
|
}
|
|
|
|
|
2018-08-06 10:21:24 +08:00
|
|
|
// addTable provides a function to add table by given worksheet name,
|
|
|
|
// coordinate area and format set.
|
2019-06-12 08:10:33 +08:00
|
|
|
func (f *File) addTable(sheet, tableXML string, x1, y1, x2, y2, i int, formatSet *formatTable) error {
|
2017-04-28 15:49:41 +08:00
|
|
|
// Correct the minimum number of rows, the table at least two lines.
|
2019-06-12 08:10:33 +08:00
|
|
|
if y1 == y2 {
|
|
|
|
y2++
|
2017-04-28 15:49:41 +08:00
|
|
|
}
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
|
2017-04-28 15:49:41 +08:00
|
|
|
// Correct table reference coordinate area, such correct C1:B3 to B1:C3.
|
2019-06-12 08:10:33 +08:00
|
|
|
ref, err := f.coordinatesToAreaRef([]int{x1, y1, x2, y2})
|
2019-03-23 20:08:06 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
|
2019-03-23 20:08:06 +08:00
|
|
|
var tableColumn []*xlsxTableColumn
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
|
2017-04-28 15:49:41 +08:00
|
|
|
idx := 0
|
2019-06-12 08:10:33 +08:00
|
|
|
for i := x1; i <= x2; i++ {
|
2017-04-28 15:49:41 +08:00
|
|
|
idx++
|
2019-06-12 08:10:33 +08:00
|
|
|
cell, err := CoordinatesToCellName(i, y1)
|
2019-03-23 20:08:06 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
name, _ := f.GetCellValue(sheet, cell)
|
2017-04-28 15:49:41 +08:00
|
|
|
if _, err := strconv.Atoi(name); err == nil {
|
2019-12-24 01:09:28 +08:00
|
|
|
_ = f.SetCellStr(sheet, cell, name)
|
2017-04-28 15:49:41 +08:00
|
|
|
}
|
|
|
|
if name == "" {
|
|
|
|
name = "Column" + strconv.Itoa(idx)
|
|
|
|
f.SetCellStr(sheet, cell, name)
|
|
|
|
}
|
|
|
|
tableColumn = append(tableColumn, &xlsxTableColumn{
|
|
|
|
ID: idx,
|
|
|
|
Name: name,
|
|
|
|
})
|
|
|
|
}
|
2018-05-04 11:20:51 +08:00
|
|
|
name := formatSet.TableName
|
|
|
|
if name == "" {
|
|
|
|
name = "Table" + strconv.Itoa(i)
|
|
|
|
}
|
2017-04-28 15:49:41 +08:00
|
|
|
t := xlsxTable{
|
2020-07-18 15:15:16 +08:00
|
|
|
XMLNS: NameSpaceSpreadSheet.Value,
|
2017-04-28 15:49:41 +08:00
|
|
|
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)
|
2018-05-07 16:12:51 +08:00
|
|
|
f.saveFileList(tableXML, table)
|
2019-03-23 20:08:06 +08:00
|
|
|
return nil
|
2017-04-28 15:49:41 +08:00
|
|
|
}
|
2017-06-08 11:11:11 +08:00
|
|
|
|
2018-08-06 10:21:24 +08:00
|
|
|
// parseAutoFilterSet provides a function to parse the settings of the auto
|
2017-06-08 11:11:11 +08:00
|
|
|
// filter.
|
2018-05-27 11:25:55 +08:00
|
|
|
func parseAutoFilterSet(formatSet string) (*formatAutoFilter, error) {
|
2017-06-08 11:11:11 +08:00
|
|
|
format := formatAutoFilter{}
|
2018-05-27 11:25:55 +08:00
|
|
|
err := json.Unmarshal([]byte(formatSet), &format)
|
|
|
|
return &format, err
|
2017-06-08 11:11:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// AutoFilter provides the method to add auto filter in a worksheet by given
|
2018-05-03 10:01:41 +08:00
|
|
|
// worksheet name, coordinate area and settings. An autofilter in Excel is a
|
|
|
|
// way of filtering a 2D range of data based on some simple criteria. For
|
|
|
|
// example applying an autofilter to a cell range A1:D4 in the Sheet1:
|
2017-06-08 11:11:11 +08:00
|
|
|
//
|
2019-04-20 14:57:50 +08:00
|
|
|
// err := f.AutoFilter("Sheet1", "A1", "D4", "")
|
2017-06-08 11:11:11 +08:00
|
|
|
//
|
|
|
|
// Filter data in an autofilter:
|
|
|
|
//
|
2019-04-20 14:57:50 +08:00
|
|
|
// err := f.AutoFilter("Sheet1", "A1", "D4", `{"column":"B","expression":"x != blanks"}`)
|
2017-06-08 11:11:11 +08:00
|
|
|
//
|
2017-06-14 15:01:49 +08:00
|
|
|
// column defines the filter columns in a autofilter range based on simple
|
|
|
|
// criteria
|
2017-06-08 11:11:11 +08:00
|
|
|
//
|
2018-05-03 10:01:41 +08:00
|
|
|
// It isn't sufficient to just specify the filter condition. You must also
|
|
|
|
// hide any rows that don't match the filter condition. Rows are hidden using
|
|
|
|
// the SetRowVisible() method. Excelize can't filter rows automatically since
|
|
|
|
// this isn't part of the file format.
|
2017-06-08 11:11:11 +08:00
|
|
|
//
|
|
|
|
// Setting a filter criteria for a column:
|
|
|
|
//
|
2018-05-03 10:01:41 +08:00
|
|
|
// expression defines the conditions, the following operators are available
|
|
|
|
// for setting the filter criteria:
|
2017-06-08 11:11:11 +08:00
|
|
|
//
|
|
|
|
// ==
|
|
|
|
// !=
|
|
|
|
// >
|
|
|
|
// <
|
|
|
|
// >=
|
|
|
|
// <=
|
|
|
|
// and
|
|
|
|
// or
|
|
|
|
//
|
2018-05-03 10:01:41 +08:00
|
|
|
// An expression can comprise a single statement or two statements separated
|
|
|
|
// by the 'and' and 'or' operators. For example:
|
2017-06-08 11:11:11 +08:00
|
|
|
//
|
|
|
|
// x < 2000
|
|
|
|
// x > 2000
|
|
|
|
// x == 2000
|
|
|
|
// x > 2000 and x < 5000
|
|
|
|
// x == 2000 or x == 5000
|
|
|
|
//
|
2017-06-14 15:01:49 +08:00
|
|
|
// Filtering of blank or non-blank data can be achieved by using a value of
|
|
|
|
// Blanks or NonBlanks in the expression:
|
2017-06-08 11:11:11 +08:00
|
|
|
//
|
|
|
|
// x == Blanks
|
|
|
|
// x == NonBlanks
|
|
|
|
//
|
|
|
|
// Excel also allows some simple string matching operations:
|
|
|
|
//
|
|
|
|
// x == b* // begins with b
|
|
|
|
// x != b* // doesnt begin with b
|
|
|
|
// x == *b // ends with b
|
|
|
|
// x != *b // doesnt end with b
|
|
|
|
// x == *b* // contains b
|
|
|
|
// x != *b* // doesn't contains b
|
|
|
|
//
|
2017-06-14 15:01:49 +08:00
|
|
|
// You can also use '*' to match any character or number and '?' to match any
|
|
|
|
// single character or number. No other regular expression quantifier is
|
|
|
|
// supported by Excel's filters. Excel's regular expression characters can be
|
|
|
|
// escaped using '~'.
|
2017-06-08 11:11:11 +08:00
|
|
|
//
|
2017-06-14 15:01:49 +08:00
|
|
|
// The placeholder variable x in the above examples can be replaced by any
|
|
|
|
// simple string. The actual placeholder name is ignored internally so the
|
|
|
|
// following are all equivalent:
|
2017-06-08 11:11:11 +08:00
|
|
|
//
|
|
|
|
// x < 2000
|
|
|
|
// col < 2000
|
|
|
|
// Price < 2000
|
|
|
|
//
|
|
|
|
func (f *File) AutoFilter(sheet, hcell, vcell, format string) error {
|
2019-03-23 20:08:06 +08:00
|
|
|
hcol, hrow, err := CellNameToCoordinates(hcell)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
vcol, vrow, err := CellNameToCoordinates(vcell)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-06-08 11:11:11 +08:00
|
|
|
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
if vcol < hcol {
|
|
|
|
vcol, hcol = hcol, vcol
|
2017-06-08 11:11:11 +08:00
|
|
|
}
|
|
|
|
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
if vrow < hrow {
|
|
|
|
vrow, hrow = hrow, vrow
|
2017-06-08 11:11:11 +08:00
|
|
|
}
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
|
|
|
|
formatSet, _ := parseAutoFilterSet(format)
|
2020-07-19 00:10:42 +08:00
|
|
|
cellStart, _ := CoordinatesToCellName(hcol, hrow)
|
|
|
|
cellEnd, _ := CoordinatesToCellName(vcol, vrow)
|
2020-05-15 14:03:02 +08:00
|
|
|
ref, filterDB := cellStart+":"+cellEnd, "_xlnm._FilterDatabase"
|
2020-05-14 22:36:00 +08:00
|
|
|
wb := f.workbookReader()
|
2020-05-15 14:03:02 +08:00
|
|
|
sheetID := f.GetSheetIndex(sheet)
|
|
|
|
filterRange := fmt.Sprintf("%s!%s", sheet, ref)
|
2020-05-14 22:36:00 +08:00
|
|
|
d := xlsxDefinedName{
|
2020-05-15 14:03:02 +08:00
|
|
|
Name: filterDB,
|
2020-05-14 22:36:00 +08:00
|
|
|
Hidden: true,
|
2020-05-15 14:03:02 +08:00
|
|
|
LocalSheetID: intPtr(sheetID),
|
|
|
|
Data: filterRange,
|
2020-05-14 22:36:00 +08:00
|
|
|
}
|
2020-05-15 14:03:02 +08:00
|
|
|
if wb.DefinedNames == nil {
|
2020-05-14 22:36:00 +08:00
|
|
|
wb.DefinedNames = &xlsxDefinedNames{
|
|
|
|
DefinedName: []xlsxDefinedName{d},
|
|
|
|
}
|
2020-05-15 14:03:02 +08:00
|
|
|
} else {
|
|
|
|
var definedNameExists bool
|
|
|
|
for idx := range wb.DefinedNames.DefinedName {
|
|
|
|
definedName := wb.DefinedNames.DefinedName[idx]
|
|
|
|
if definedName.Name == filterDB && *definedName.LocalSheetID == sheetID && definedName.Hidden {
|
|
|
|
wb.DefinedNames.DefinedName[idx].Data = filterRange
|
|
|
|
definedNameExists = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !definedNameExists {
|
|
|
|
wb.DefinedNames.DefinedName = append(wb.DefinedNames.DefinedName, d)
|
|
|
|
}
|
2020-05-14 22:36:00 +08:00
|
|
|
}
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
refRange := vcol - hcol
|
|
|
|
return f.autoFilter(sheet, ref, refRange, hcol, formatSet)
|
2017-06-08 11:11:11 +08:00
|
|
|
}
|
|
|
|
|
2018-08-06 10:21:24 +08:00
|
|
|
// autoFilter provides a function to extract the tokens from the filter
|
2017-06-08 11:11:11 +08:00
|
|
|
// expression. The tokens are mainly non-whitespace groups.
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
func (f *File) autoFilter(sheet, ref string, refRange, col int, formatSet *formatAutoFilter) error {
|
2019-04-15 11:22:57 +08:00
|
|
|
xlsx, err := f.workSheetReader(sheet)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-06-08 11:11:11 +08:00
|
|
|
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
|
|
|
|
}
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
|
2019-03-23 20:08:06 +08:00
|
|
|
fsCol, err := ColumnNameToNumber(formatSet.Column)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
offset := fsCol - col
|
2017-06-08 11:11:11 +08:00
|
|
|
if offset < 0 || offset > refRange {
|
2018-09-12 15:47:56 +08:00
|
|
|
return fmt.Errorf("incorrect index of column '%s'", formatSet.Column)
|
2017-06-08 11:11:11 +08:00
|
|
|
}
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
|
2017-06-08 11:11:11 +08:00
|
|
|
filter.FilterColumn = &xlsxFilterColumn{
|
|
|
|
ColID: offset,
|
|
|
|
}
|
|
|
|
re := regexp.MustCompile(`"(?:[^"]|"")*"|\S+`)
|
|
|
|
token := re.FindAllString(formatSet.Expression, -1)
|
|
|
|
if len(token) != 3 && len(token) != 7 {
|
2018-09-12 15:47:56 +08:00
|
|
|
return fmt.Errorf("incorrect number of tokens in criteria '%s'", formatSet.Expression)
|
2017-06-08 11:11:11 +08:00
|
|
|
}
|
|
|
|
expressions, tokens, err := f.parseFilterExpression(formatSet.Expression, token)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
f.writeAutoFilter(filter, expressions, tokens)
|
|
|
|
xlsx.AutoFilter = filter
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-08-06 10:21:24 +08:00
|
|
|
// writeAutoFilter provides a function to check for single or double custom
|
|
|
|
// filters as default filters and handle them accordingly.
|
2017-06-08 11:11:11 +08:00
|
|
|
func (f *File) writeAutoFilter(filter *xlsxAutoFilter, exp []int, tokens []string) {
|
|
|
|
if len(exp) == 1 && exp[0] == 2 {
|
|
|
|
// Single equality.
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
var filters []*xlsxFilter
|
2017-06-08 11:11:11 +08:00
|
|
|
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 {
|
|
|
|
// Non default custom filter.
|
|
|
|
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]]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-06 10:21:24 +08:00
|
|
|
// writeCustomFilter provides a function to write the <customFilter> element.
|
2017-06-08 11:11:11 +08:00
|
|
|
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}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-06 10:21:24 +08:00
|
|
|
// parseFilterExpression provides a function to converts the tokens of a
|
|
|
|
// possibly conditional expression into 1 or 2 sub expressions for further
|
|
|
|
// parsing.
|
2017-06-08 11:11:11 +08:00
|
|
|
//
|
|
|
|
// Examples:
|
|
|
|
//
|
|
|
|
// ('x', '==', 2000) -> exp1
|
|
|
|
// ('x', '>', 2000, 'and', 'x', '<', 5000) -> exp1 and exp2
|
|
|
|
//
|
|
|
|
func (f *File) parseFilterExpression(expression string, tokens []string) ([]int, []string, error) {
|
|
|
|
expressions := []int{}
|
|
|
|
t := []string{}
|
|
|
|
if len(tokens) == 7 {
|
|
|
|
// The number of tokens will be either 3 (for 1 expression) or 7 (for 2
|
|
|
|
// expressions).
|
|
|
|
conditional := 0
|
|
|
|
c := tokens[3]
|
|
|
|
re, _ := regexp.Match(`(or|\|\|)`, []byte(c))
|
|
|
|
if re {
|
|
|
|
conditional = 1
|
|
|
|
}
|
|
|
|
expression1, token1, err := f.parseFilterTokens(expression, tokens[0:3])
|
|
|
|
if err != nil {
|
|
|
|
return expressions, t, err
|
|
|
|
}
|
|
|
|
expression2, token2, err := f.parseFilterTokens(expression, tokens[4:7])
|
|
|
|
if err != nil {
|
|
|
|
return expressions, t, err
|
|
|
|
}
|
|
|
|
expressions = []int{expression1[0], conditional, expression2[0]}
|
|
|
|
t = []string{token1, token2}
|
|
|
|
} else {
|
|
|
|
exp, token, err := f.parseFilterTokens(expression, tokens)
|
|
|
|
if err != nil {
|
|
|
|
return expressions, t, err
|
|
|
|
}
|
|
|
|
expressions = exp
|
|
|
|
t = []string{token}
|
|
|
|
}
|
|
|
|
return expressions, t, nil
|
|
|
|
}
|
|
|
|
|
2018-08-06 10:21:24 +08:00
|
|
|
// parseFilterTokens provides a function to parse the 3 tokens of a filter
|
2017-06-08 11:11:11 +08:00
|
|
|
// expression and return the operator and token.
|
|
|
|
func (f *File) parseFilterTokens(expression string, tokens []string) ([]int, string, error) {
|
|
|
|
operators := map[string]int{
|
|
|
|
"==": 2,
|
|
|
|
"=": 2,
|
|
|
|
"=~": 2,
|
|
|
|
"eq": 2,
|
|
|
|
"!=": 5,
|
|
|
|
"!~": 5,
|
|
|
|
"ne": 5,
|
|
|
|
"<>": 5,
|
|
|
|
"<": 1,
|
|
|
|
"<=": 3,
|
|
|
|
">": 4,
|
|
|
|
">=": 6,
|
|
|
|
}
|
|
|
|
operator, ok := operators[strings.ToLower(tokens[1])]
|
|
|
|
if !ok {
|
|
|
|
// Convert the operator from a number to a descriptive string.
|
2018-09-12 15:47:56 +08:00
|
|
|
return []int{}, "", fmt.Errorf("unknown operator: %s", tokens[1])
|
2017-06-08 11:11:11 +08:00
|
|
|
}
|
|
|
|
token := tokens[2]
|
|
|
|
// Special handling for Blanks/NonBlanks.
|
|
|
|
re, _ := regexp.Match("blanks|nonblanks", []byte(strings.ToLower(token)))
|
|
|
|
if re {
|
|
|
|
// Only allow Equals or NotEqual in this context.
|
|
|
|
if operator != 2 && operator != 5 {
|
2018-09-12 15:47:56 +08:00
|
|
|
return []int{operator}, token, fmt.Errorf("the operator '%s' in expression '%s' is not valid in relation to Blanks/NonBlanks'", tokens[1], expression)
|
2017-06-08 11:11:11 +08:00
|
|
|
}
|
|
|
|
token = strings.ToLower(token)
|
|
|
|
// The operator should always be 2 (=) to flag a "simple" equality in
|
|
|
|
// the binary record. Therefore we convert <> to =.
|
|
|
|
if token == "blanks" {
|
|
|
|
if operator == 5 {
|
|
|
|
token = " "
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if operator == 5 {
|
|
|
|
operator = 2
|
|
|
|
token = "blanks"
|
|
|
|
} else {
|
|
|
|
operator = 5
|
|
|
|
token = " "
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// if the string token contains an Excel match character then change the
|
|
|
|
// operator type to indicate a non "simple" equality.
|
|
|
|
re, _ = regexp.Match("[*?]", []byte(token))
|
|
|
|
if operator == 2 && re {
|
|
|
|
operator = 22
|
|
|
|
}
|
|
|
|
return []int{operator}, token, nil
|
|
|
|
}
|