2024-01-09 20:56:20 +08:00
|
|
|
// Copyright 2016 - 2024 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.
|
|
|
|
//
|
2022-02-17 00:09:11 +08:00
|
|
|
// Package excelize providing a set of functions that allow you to write to and
|
|
|
|
// read from XLAM / XLSM / XLSX / XLTM / XLTX files. Supports reading and
|
|
|
|
// writing spreadsheet documents generated by Microsoft Excel™ 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
|
2024-01-18 15:31:43 +08:00
|
|
|
// data. This library needs Go version 1.18 or later.
|
2018-09-14 00:58:48 +08:00
|
|
|
|
2018-09-01 19:38:30 +08:00
|
|
|
package excelize
|
|
|
|
|
2018-12-27 18:51:44 +08:00
|
|
|
import (
|
2024-03-06 09:26:38 +08:00
|
|
|
"fmt"
|
2021-07-31 00:31:51 +08:00
|
|
|
"math"
|
2018-12-27 22:28:28 +08:00
|
|
|
"path/filepath"
|
2019-12-22 00:02:09 +08:00
|
|
|
"strings"
|
2018-12-27 18:51:44 +08:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
2018-09-01 19:38:30 +08:00
|
|
|
|
|
|
|
func TestDataValidation(t *testing.T) {
|
2018-12-27 22:28:28 +08:00
|
|
|
resultFile := filepath.Join("test", "TestDataValidation.xlsx")
|
2018-12-27 18:51:44 +08:00
|
|
|
|
2019-05-05 16:25:57 +08:00
|
|
|
f := NewFile()
|
2018-09-01 19:38:30 +08:00
|
|
|
|
2023-03-01 13:25:17 +08:00
|
|
|
dv := NewDataValidation(true)
|
|
|
|
dv.Sqref = "A1:B2"
|
|
|
|
assert.NoError(t, dv.SetRange(10, 20, DataValidationTypeWhole, DataValidationOperatorBetween))
|
|
|
|
dv.SetError(DataValidationErrorStyleStop, "error title", "error body")
|
|
|
|
dv.SetError(DataValidationErrorStyleWarning, "error title", "error body")
|
|
|
|
dv.SetError(DataValidationErrorStyleInformation, "error title", "error body")
|
|
|
|
assert.NoError(t, f.AddDataValidation("Sheet1", dv))
|
2022-08-27 00:45:46 +08:00
|
|
|
|
|
|
|
dataValidations, err := f.GetDataValidations("Sheet1")
|
|
|
|
assert.NoError(t, err)
|
2023-03-25 13:30:13 +08:00
|
|
|
assert.Len(t, dataValidations, 1)
|
2022-08-27 00:45:46 +08:00
|
|
|
|
2019-12-24 01:09:28 +08:00
|
|
|
assert.NoError(t, f.SaveAs(resultFile))
|
2018-09-01 19:38:30 +08:00
|
|
|
|
2023-03-01 13:25:17 +08:00
|
|
|
dv = NewDataValidation(true)
|
|
|
|
dv.Sqref = "A3:B4"
|
|
|
|
assert.NoError(t, dv.SetRange(10, 20, DataValidationTypeWhole, DataValidationOperatorGreaterThan))
|
|
|
|
dv.SetInput("input title", "input body")
|
|
|
|
assert.NoError(t, f.AddDataValidation("Sheet1", dv))
|
2022-08-27 00:45:46 +08:00
|
|
|
|
|
|
|
dataValidations, err = f.GetDataValidations("Sheet1")
|
|
|
|
assert.NoError(t, err)
|
2023-03-25 13:30:13 +08:00
|
|
|
assert.Len(t, dataValidations, 2)
|
2022-08-27 00:45:46 +08:00
|
|
|
|
2019-12-24 01:09:28 +08:00
|
|
|
assert.NoError(t, f.SaveAs(resultFile))
|
2018-09-01 19:38:30 +08:00
|
|
|
|
Breaking change: changed the function signature for 11 exported functions
* Change
`func (f *File) NewConditionalStyle(style string) (int, error)`
to
`func (f *File) NewConditionalStyle(style *Style) (int, error)`
* Change
`func (f *File) NewStyle(style interface{}) (int, error)`
to
`func (f *File) NewStyle(style *Style) (int, error)`
* Change
`func (f *File) AddChart(sheet, cell, opts string, combo ...string) error`
to
`func (f *File) AddChart(sheet, cell string, chart *ChartOptions, combo ...*ChartOptions) error`
* Change
`func (f *File) AddChartSheet(sheet, opts string, combo ...string) error`
to
`func (f *File) AddChartSheet(sheet string, chart *ChartOptions, combo ...*ChartOptions) error`
* Change
`func (f *File) AddShape(sheet, cell, opts string) error`
to
`func (f *File) AddShape(sheet, cell string, opts *Shape) error`
* Change
`func (f *File) AddPictureFromBytes(sheet, cell, opts, name, extension string, file []byte) error`
to
`func (f *File) AddPictureFromBytes(sheet, cell, name, extension string, file []byte, opts *PictureOptions) error`
* Change
`func (f *File) AddTable(sheet, hCell, vCell, opts string) error`
to
`func (f *File) AddTable(sheet, reference string, opts *TableOptions) error`
* Change
`func (sw *StreamWriter) AddTable(hCell, vCell, opts string) error`
to
`func (sw *StreamWriter) AddTable(reference string, opts *TableOptions) error`
* Change
`func (f *File) AutoFilter(sheet, hCell, vCell, opts string) error`
to
`func (f *File) AutoFilter(sheet, reference string, opts *AutoFilterOptions) error`
* Change
`func (f *File) SetPanes(sheet, panes string) error`
to
`func (f *File) SetPanes(sheet string, panes *Panes) error`
* Change
`func (sw *StreamWriter) AddTable(hCell, vCell, opts string) error`
to
`func (sw *StreamWriter) AddTable(reference string, opts *TableOptions) error`
* Change
`func (f *File) SetConditionalFormat(sheet, reference, opts string) error`
to
`func (f *File) SetConditionalFormat(sheet, reference string, opts []ConditionalFormatOptions) error`
* Add exported types:
* AutoFilterListOptions
* AutoFilterOptions
* Chart
* ChartAxis
* ChartDimension
* ChartLegend
* ChartLine
* ChartMarker
* ChartPlotArea
* ChartSeries
* ChartTitle
* ConditionalFormatOptions
* PaneOptions
* Panes
* PictureOptions
* Shape
* ShapeColor
* ShapeLine
* ShapeParagraph
* TableOptions
* This added support for set sheet visible as very hidden
* Return error when missing required parameters for set defined name
* Update unit test and comments
2022-12-30 00:50:08 +08:00
|
|
|
_, err = f.NewSheet("Sheet2")
|
|
|
|
assert.NoError(t, err)
|
2021-08-26 00:48:18 +08:00
|
|
|
assert.NoError(t, f.SetSheetRow("Sheet2", "A2", &[]interface{}{"B2", 1}))
|
|
|
|
assert.NoError(t, f.SetSheetRow("Sheet2", "A3", &[]interface{}{"B3", 3}))
|
2023-03-01 13:25:17 +08:00
|
|
|
dv = NewDataValidation(true)
|
|
|
|
dv.Sqref = "A1:B1"
|
|
|
|
assert.NoError(t, dv.SetRange("INDIRECT($A$2)", "INDIRECT($A$3)", DataValidationTypeWhole, DataValidationOperatorBetween))
|
|
|
|
dv.SetError(DataValidationErrorStyleStop, "error title", "error body")
|
|
|
|
assert.NoError(t, f.AddDataValidation("Sheet2", dv))
|
2022-08-27 00:45:46 +08:00
|
|
|
dataValidations, err = f.GetDataValidations("Sheet1")
|
|
|
|
assert.NoError(t, err)
|
2023-03-25 13:30:13 +08:00
|
|
|
assert.Len(t, dataValidations, 2)
|
2022-08-27 00:45:46 +08:00
|
|
|
dataValidations, err = f.GetDataValidations("Sheet2")
|
|
|
|
assert.NoError(t, err)
|
2023-03-25 13:30:13 +08:00
|
|
|
assert.Len(t, dataValidations, 1)
|
2021-08-26 00:48:18 +08:00
|
|
|
|
2023-03-01 13:25:17 +08:00
|
|
|
dv = NewDataValidation(true)
|
|
|
|
dv.Sqref = "A5:B6"
|
2021-07-31 00:31:51 +08:00
|
|
|
for _, listValid := range [][]string{
|
|
|
|
{"1", "2", "3"},
|
2023-11-11 00:04:05 +08:00
|
|
|
{"=A1"},
|
2021-11-16 00:40:44 +08:00
|
|
|
{strings.Repeat("&", MaxFieldLength)},
|
|
|
|
{strings.Repeat("\u4E00", MaxFieldLength)},
|
2021-07-31 00:31:51 +08:00
|
|
|
{strings.Repeat("\U0001F600", 100), strings.Repeat("\u4E01", 50), "<&>"},
|
|
|
|
{`A<`, `B>`, `C"`, "D\t", `E'`, `F`},
|
|
|
|
} {
|
2023-03-01 13:25:17 +08:00
|
|
|
dv.Formula1 = ""
|
|
|
|
assert.NoError(t, dv.SetDropList(listValid),
|
2021-07-31 00:31:51 +08:00
|
|
|
"SetDropList failed for valid input %v", listValid)
|
2023-03-01 13:25:17 +08:00
|
|
|
assert.NotEqual(t, "", dv.Formula1,
|
2021-07-31 00:31:51 +08:00
|
|
|
"Formula1 should not be empty for valid input %v", listValid)
|
|
|
|
}
|
2023-11-11 00:04:05 +08:00
|
|
|
assert.Equal(t, `"A<,B>,C"",D ,E',F"`, dv.Formula1)
|
2023-03-01 13:25:17 +08:00
|
|
|
assert.NoError(t, f.AddDataValidation("Sheet1", dv))
|
2022-08-27 00:45:46 +08:00
|
|
|
|
|
|
|
dataValidations, err = f.GetDataValidations("Sheet1")
|
|
|
|
assert.NoError(t, err)
|
2023-03-29 00:00:27 +08:00
|
|
|
assert.Len(t, dataValidations, 3)
|
2022-08-27 00:45:46 +08:00
|
|
|
|
|
|
|
// Test get data validation on no exists worksheet
|
|
|
|
_, err = f.GetDataValidations("SheetN")
|
2022-08-28 00:16:41 +08:00
|
|
|
assert.EqualError(t, err, "sheet SheetN does not exist")
|
This closes #1425, breaking changes for sheet name (#1426)
- Checking and return error for invalid sheet name instead of trim invalid characters
- Add error return for the 4 functions: `DeleteSheet`, `GetSheetIndex`, `GetSheetVisible` and `SetSheetName`
- Export new error 4 constants: `ErrSheetNameBlank`, `ErrSheetNameInvalid`, `ErrSheetNameLength` and `ErrSheetNameSingleQuote`
- Rename exported error constant `ErrExistsWorksheet` to `ErrExistsSheet`
- Update unit tests for 90 functions: `AddChart`, `AddChartSheet`, `AddComment`, `AddDataValidation`, `AddPicture`, `AddPictureFromBytes`, `AddPivotTable`, `AddShape`, `AddSparkline`, `AddTable`, `AutoFilter`, `CalcCellValue`, `Cols`, `DeleteChart`, `DeleteComment`, `DeleteDataValidation`, `DeletePicture`, `DeleteSheet`, `DuplicateRow`, `DuplicateRowTo`, `GetCellFormula`, `GetCellHyperLink`, `GetCellRichText`, `GetCellStyle`, `GetCellType`, `GetCellValue`, `GetColOutlineLevel`, `GetCols`, `GetColStyle`, `GetColVisible`, `GetColWidth`, `GetConditionalFormats`, `GetDataValidations`, `GetMergeCells`, `GetPageLayout`, `GetPageMargins`, `GetPicture`, `GetRowHeight`, `GetRowOutlineLevel`, `GetRows`, `GetRowVisible`, `GetSheetIndex`, `GetSheetProps`, `GetSheetVisible`, `GroupSheets`, `InsertCol`, `InsertPageBreak`, `InsertRows`, `MergeCell`, `NewSheet`, `NewStreamWriter`, `ProtectSheet`, `RemoveCol`, `RemovePageBreak`, `RemoveRow`, `Rows`, `SearchSheet`, `SetCellBool`, `SetCellDefault`, `SetCellFloat`, `SetCellFormula`, `SetCellHyperLink`, `SetCellInt`, `SetCellRichText`, `SetCellStr`, `SetCellStyle`, `SetCellValue`, `SetColOutlineLevel`, `SetColStyle`, `SetColVisible`, `SetColWidth`, `SetConditionalFormat`, `SetHeaderFooter`, `SetPageLayout`, `SetPageMargins`, `SetPanes`, `SetRowHeight`, `SetRowOutlineLevel`, `SetRowStyle`, `SetRowVisible`, `SetSheetBackground`, `SetSheetBackgroundFromBytes`, `SetSheetCol`, `SetSheetName`, `SetSheetProps`, `SetSheetRow`, `SetSheetVisible`, `UnmergeCell`, `UnprotectSheet` and
`UnsetConditionalFormat`
- Update documentation of the set style functions
Co-authored-by: guoweikuang <weikuang.guo@shopee.com>
2022-12-23 00:54:40 +08:00
|
|
|
// Test get data validation with invalid sheet name
|
|
|
|
_, err = f.GetDataValidations("Sheet:1")
|
|
|
|
assert.EqualError(t, err, ErrSheetNameInvalid.Error())
|
2022-08-27 00:45:46 +08:00
|
|
|
|
2019-12-24 01:09:28 +08:00
|
|
|
assert.NoError(t, f.SaveAs(resultFile))
|
2022-08-27 00:45:46 +08:00
|
|
|
|
|
|
|
// Test get data validation on a worksheet without data validation settings
|
|
|
|
f = NewFile()
|
|
|
|
dataValidations, err = f.GetDataValidations("Sheet1")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, []*DataValidation(nil), dataValidations)
|
2024-03-06 09:26:38 +08:00
|
|
|
|
|
|
|
// Test get data validations which storage in the extension lists
|
|
|
|
f = NewFile()
|
|
|
|
ws, ok := f.Sheet.Load("xl/worksheets/sheet1.xml")
|
|
|
|
assert.True(t, ok)
|
|
|
|
ws.(*xlsxWorksheet).ExtLst = &xlsxExtLst{Ext: fmt.Sprintf(`<ext uri="%s" xmlns:x14="%s"><x14:dataValidations><x14:dataValidation type="list" allowBlank="1"><x14:formula1><xm:f>Sheet1!$B$1:$B$5</xm:f></x14:formula1><xm:sqref>A7:B8</xm:sqref></x14:dataValidation></x14:dataValidations></ext>`, ExtURIDataValidations, NameSpaceSpreadSheetX14.Value)}
|
|
|
|
dataValidations, err = f.GetDataValidations("Sheet1")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, []*DataValidation{
|
|
|
|
{
|
|
|
|
AllowBlank: true,
|
|
|
|
Type: "list",
|
|
|
|
Formula1: "Sheet1!$B$1:$B$5",
|
|
|
|
Sqref: "A7:B8",
|
|
|
|
},
|
|
|
|
}, dataValidations)
|
|
|
|
|
|
|
|
// Test get data validations with invalid extension list characters
|
|
|
|
ws.(*xlsxWorksheet).ExtLst = &xlsxExtLst{Ext: fmt.Sprintf(`<ext uri="%s" xmlns:x14="%s"><x14:dataValidations></x14:dataValidation></x14:dataValidations></ext>`, ExtURIDataValidations, NameSpaceSpreadSheetX14.Value)}
|
|
|
|
_, err = f.GetDataValidations("Sheet1")
|
|
|
|
assert.EqualError(t, err, "XML syntax error on line 1: element <dataValidations> closed by </dataValidation>")
|
|
|
|
|
|
|
|
// Test get validations without validations
|
|
|
|
assert.Nil(t, getDataValidations(nil))
|
|
|
|
assert.Nil(t, getDataValidations(&xlsxDataValidations{DataValidation: []*xlsxDataValidation{nil}}))
|
2018-12-27 18:51:44 +08:00
|
|
|
}
|
2018-09-01 19:38:30 +08:00
|
|
|
|
2018-12-27 18:51:44 +08:00
|
|
|
func TestDataValidationError(t *testing.T) {
|
2018-12-27 22:28:28 +08:00
|
|
|
resultFile := filepath.Join("test", "TestDataValidationError.xlsx")
|
2018-12-27 18:51:44 +08:00
|
|
|
|
2019-05-05 16:25:57 +08:00
|
|
|
f := NewFile()
|
2019-12-24 01:09:28 +08:00
|
|
|
assert.NoError(t, f.SetCellStr("Sheet1", "E1", "E1"))
|
|
|
|
assert.NoError(t, f.SetCellStr("Sheet1", "E2", "E2"))
|
|
|
|
assert.NoError(t, f.SetCellStr("Sheet1", "E3", "E3"))
|
2018-12-27 18:51:44 +08:00
|
|
|
|
2023-03-01 13:25:17 +08:00
|
|
|
dv := NewDataValidation(true)
|
|
|
|
dv.SetSqref("A7:B8")
|
|
|
|
dv.SetSqref("A7:B8")
|
|
|
|
dv.SetSqrefDropList("$E$1:$E$3")
|
2018-12-27 18:51:44 +08:00
|
|
|
|
2023-03-01 13:25:17 +08:00
|
|
|
assert.NoError(t, f.AddDataValidation("Sheet1", dv))
|
2018-09-04 13:40:53 +08:00
|
|
|
|
2023-03-01 13:25:17 +08:00
|
|
|
dv = NewDataValidation(true)
|
|
|
|
err := dv.SetDropList(make([]string, 258))
|
|
|
|
if dv.Formula1 != "" {
|
2019-01-23 22:07:11 +08:00
|
|
|
t.Errorf("data validation error. Formula1 must be empty!")
|
|
|
|
return
|
|
|
|
}
|
2022-01-08 10:32:13 +08:00
|
|
|
assert.EqualError(t, err, ErrDataValidationFormulaLength.Error())
|
2023-03-01 13:25:17 +08:00
|
|
|
assert.EqualError(t, dv.SetRange(nil, 20, DataValidationTypeWhole, DataValidationOperatorBetween), ErrParameterInvalid.Error())
|
|
|
|
assert.EqualError(t, dv.SetRange(10, nil, DataValidationTypeWhole, DataValidationOperatorBetween), ErrParameterInvalid.Error())
|
|
|
|
assert.NoError(t, dv.SetRange(10, 20, DataValidationTypeWhole, DataValidationOperatorGreaterThan))
|
|
|
|
dv.SetSqref("A9:B10")
|
2018-09-13 10:38:01 +08:00
|
|
|
|
2023-03-01 13:25:17 +08:00
|
|
|
assert.NoError(t, f.AddDataValidation("Sheet1", dv))
|
2019-12-22 00:02:09 +08:00
|
|
|
|
This closes #1425, breaking changes for sheet name (#1426)
- Checking and return error for invalid sheet name instead of trim invalid characters
- Add error return for the 4 functions: `DeleteSheet`, `GetSheetIndex`, `GetSheetVisible` and `SetSheetName`
- Export new error 4 constants: `ErrSheetNameBlank`, `ErrSheetNameInvalid`, `ErrSheetNameLength` and `ErrSheetNameSingleQuote`
- Rename exported error constant `ErrExistsWorksheet` to `ErrExistsSheet`
- Update unit tests for 90 functions: `AddChart`, `AddChartSheet`, `AddComment`, `AddDataValidation`, `AddPicture`, `AddPictureFromBytes`, `AddPivotTable`, `AddShape`, `AddSparkline`, `AddTable`, `AutoFilter`, `CalcCellValue`, `Cols`, `DeleteChart`, `DeleteComment`, `DeleteDataValidation`, `DeletePicture`, `DeleteSheet`, `DuplicateRow`, `DuplicateRowTo`, `GetCellFormula`, `GetCellHyperLink`, `GetCellRichText`, `GetCellStyle`, `GetCellType`, `GetCellValue`, `GetColOutlineLevel`, `GetCols`, `GetColStyle`, `GetColVisible`, `GetColWidth`, `GetConditionalFormats`, `GetDataValidations`, `GetMergeCells`, `GetPageLayout`, `GetPageMargins`, `GetPicture`, `GetRowHeight`, `GetRowOutlineLevel`, `GetRows`, `GetRowVisible`, `GetSheetIndex`, `GetSheetProps`, `GetSheetVisible`, `GroupSheets`, `InsertCol`, `InsertPageBreak`, `InsertRows`, `MergeCell`, `NewSheet`, `NewStreamWriter`, `ProtectSheet`, `RemoveCol`, `RemovePageBreak`, `RemoveRow`, `Rows`, `SearchSheet`, `SetCellBool`, `SetCellDefault`, `SetCellFloat`, `SetCellFormula`, `SetCellHyperLink`, `SetCellInt`, `SetCellRichText`, `SetCellStr`, `SetCellStyle`, `SetCellValue`, `SetColOutlineLevel`, `SetColStyle`, `SetColVisible`, `SetColWidth`, `SetConditionalFormat`, `SetHeaderFooter`, `SetPageLayout`, `SetPageMargins`, `SetPanes`, `SetRowHeight`, `SetRowOutlineLevel`, `SetRowStyle`, `SetRowVisible`, `SetSheetBackground`, `SetSheetBackgroundFromBytes`, `SetSheetCol`, `SetSheetName`, `SetSheetProps`, `SetSheetRow`, `SetSheetVisible`, `UnmergeCell`, `UnprotectSheet` and
`UnsetConditionalFormat`
- Update documentation of the set style functions
Co-authored-by: guoweikuang <weikuang.guo@shopee.com>
2022-12-23 00:54:40 +08:00
|
|
|
// Test width invalid data validation formula
|
2023-03-01 13:25:17 +08:00
|
|
|
prevFormula1 := dv.Formula1
|
2021-07-31 00:31:51 +08:00
|
|
|
for _, keys := range [][]string{
|
|
|
|
make([]string, 257),
|
|
|
|
{strings.Repeat("s", 256)},
|
|
|
|
{strings.Repeat("\u4E00", 256)},
|
|
|
|
{strings.Repeat("\U0001F600", 128)},
|
|
|
|
{strings.Repeat("\U0001F600", 127), "s"},
|
|
|
|
} {
|
2023-03-01 13:25:17 +08:00
|
|
|
err = dv.SetDropList(keys)
|
|
|
|
assert.Equal(t, prevFormula1, dv.Formula1,
|
2021-07-31 00:31:51 +08:00
|
|
|
"Formula1 should be unchanged for invalid input %v", keys)
|
2022-01-08 10:32:13 +08:00
|
|
|
assert.EqualError(t, err, ErrDataValidationFormulaLength.Error())
|
2021-07-31 00:31:51 +08:00
|
|
|
}
|
2023-03-01 13:25:17 +08:00
|
|
|
assert.NoError(t, f.AddDataValidation("Sheet1", dv))
|
|
|
|
assert.NoError(t, dv.SetRange(
|
2021-07-31 00:31:51 +08:00
|
|
|
-math.MaxFloat32, math.MaxFloat32,
|
|
|
|
DataValidationTypeWhole, DataValidationOperatorGreaterThan))
|
2023-03-01 13:25:17 +08:00
|
|
|
assert.EqualError(t, dv.SetRange(
|
2021-07-31 00:31:51 +08:00
|
|
|
-math.MaxFloat64, math.MaxFloat32,
|
|
|
|
DataValidationTypeWhole, DataValidationOperatorGreaterThan), ErrDataValidationRange.Error())
|
2023-03-01 13:25:17 +08:00
|
|
|
assert.EqualError(t, dv.SetRange(
|
2021-07-31 00:31:51 +08:00
|
|
|
math.SmallestNonzeroFloat64, math.MaxFloat64,
|
|
|
|
DataValidationTypeWhole, DataValidationOperatorGreaterThan), ErrDataValidationRange.Error())
|
|
|
|
assert.NoError(t, f.SaveAs(resultFile))
|
2019-12-22 00:02:09 +08:00
|
|
|
|
This closes #1425, breaking changes for sheet name (#1426)
- Checking and return error for invalid sheet name instead of trim invalid characters
- Add error return for the 4 functions: `DeleteSheet`, `GetSheetIndex`, `GetSheetVisible` and `SetSheetName`
- Export new error 4 constants: `ErrSheetNameBlank`, `ErrSheetNameInvalid`, `ErrSheetNameLength` and `ErrSheetNameSingleQuote`
- Rename exported error constant `ErrExistsWorksheet` to `ErrExistsSheet`
- Update unit tests for 90 functions: `AddChart`, `AddChartSheet`, `AddComment`, `AddDataValidation`, `AddPicture`, `AddPictureFromBytes`, `AddPivotTable`, `AddShape`, `AddSparkline`, `AddTable`, `AutoFilter`, `CalcCellValue`, `Cols`, `DeleteChart`, `DeleteComment`, `DeleteDataValidation`, `DeletePicture`, `DeleteSheet`, `DuplicateRow`, `DuplicateRowTo`, `GetCellFormula`, `GetCellHyperLink`, `GetCellRichText`, `GetCellStyle`, `GetCellType`, `GetCellValue`, `GetColOutlineLevel`, `GetCols`, `GetColStyle`, `GetColVisible`, `GetColWidth`, `GetConditionalFormats`, `GetDataValidations`, `GetMergeCells`, `GetPageLayout`, `GetPageMargins`, `GetPicture`, `GetRowHeight`, `GetRowOutlineLevel`, `GetRows`, `GetRowVisible`, `GetSheetIndex`, `GetSheetProps`, `GetSheetVisible`, `GroupSheets`, `InsertCol`, `InsertPageBreak`, `InsertRows`, `MergeCell`, `NewSheet`, `NewStreamWriter`, `ProtectSheet`, `RemoveCol`, `RemovePageBreak`, `RemoveRow`, `Rows`, `SearchSheet`, `SetCellBool`, `SetCellDefault`, `SetCellFloat`, `SetCellFormula`, `SetCellHyperLink`, `SetCellInt`, `SetCellRichText`, `SetCellStr`, `SetCellStyle`, `SetCellValue`, `SetColOutlineLevel`, `SetColStyle`, `SetColVisible`, `SetColWidth`, `SetConditionalFormat`, `SetHeaderFooter`, `SetPageLayout`, `SetPageMargins`, `SetPanes`, `SetRowHeight`, `SetRowOutlineLevel`, `SetRowStyle`, `SetRowVisible`, `SetSheetBackground`, `SetSheetBackgroundFromBytes`, `SetSheetCol`, `SetSheetName`, `SetSheetProps`, `SetSheetRow`, `SetSheetVisible`, `UnmergeCell`, `UnprotectSheet` and
`UnsetConditionalFormat`
- Update documentation of the set style functions
Co-authored-by: guoweikuang <weikuang.guo@shopee.com>
2022-12-23 00:54:40 +08:00
|
|
|
// Test add data validation on no exists worksheet
|
2019-12-22 00:02:09 +08:00
|
|
|
f = NewFile()
|
2022-08-28 00:16:41 +08:00
|
|
|
assert.EqualError(t, f.AddDataValidation("SheetN", nil), "sheet SheetN does not exist")
|
This closes #1425, breaking changes for sheet name (#1426)
- Checking and return error for invalid sheet name instead of trim invalid characters
- Add error return for the 4 functions: `DeleteSheet`, `GetSheetIndex`, `GetSheetVisible` and `SetSheetName`
- Export new error 4 constants: `ErrSheetNameBlank`, `ErrSheetNameInvalid`, `ErrSheetNameLength` and `ErrSheetNameSingleQuote`
- Rename exported error constant `ErrExistsWorksheet` to `ErrExistsSheet`
- Update unit tests for 90 functions: `AddChart`, `AddChartSheet`, `AddComment`, `AddDataValidation`, `AddPicture`, `AddPictureFromBytes`, `AddPivotTable`, `AddShape`, `AddSparkline`, `AddTable`, `AutoFilter`, `CalcCellValue`, `Cols`, `DeleteChart`, `DeleteComment`, `DeleteDataValidation`, `DeletePicture`, `DeleteSheet`, `DuplicateRow`, `DuplicateRowTo`, `GetCellFormula`, `GetCellHyperLink`, `GetCellRichText`, `GetCellStyle`, `GetCellType`, `GetCellValue`, `GetColOutlineLevel`, `GetCols`, `GetColStyle`, `GetColVisible`, `GetColWidth`, `GetConditionalFormats`, `GetDataValidations`, `GetMergeCells`, `GetPageLayout`, `GetPageMargins`, `GetPicture`, `GetRowHeight`, `GetRowOutlineLevel`, `GetRows`, `GetRowVisible`, `GetSheetIndex`, `GetSheetProps`, `GetSheetVisible`, `GroupSheets`, `InsertCol`, `InsertPageBreak`, `InsertRows`, `MergeCell`, `NewSheet`, `NewStreamWriter`, `ProtectSheet`, `RemoveCol`, `RemovePageBreak`, `RemoveRow`, `Rows`, `SearchSheet`, `SetCellBool`, `SetCellDefault`, `SetCellFloat`, `SetCellFormula`, `SetCellHyperLink`, `SetCellInt`, `SetCellRichText`, `SetCellStr`, `SetCellStyle`, `SetCellValue`, `SetColOutlineLevel`, `SetColStyle`, `SetColVisible`, `SetColWidth`, `SetConditionalFormat`, `SetHeaderFooter`, `SetPageLayout`, `SetPageMargins`, `SetPanes`, `SetRowHeight`, `SetRowOutlineLevel`, `SetRowStyle`, `SetRowVisible`, `SetSheetBackground`, `SetSheetBackgroundFromBytes`, `SetSheetCol`, `SetSheetName`, `SetSheetProps`, `SetSheetRow`, `SetSheetVisible`, `UnmergeCell`, `UnprotectSheet` and
`UnsetConditionalFormat`
- Update documentation of the set style functions
Co-authored-by: guoweikuang <weikuang.guo@shopee.com>
2022-12-23 00:54:40 +08:00
|
|
|
|
|
|
|
// Test add data validation with invalid sheet name
|
|
|
|
f = NewFile()
|
|
|
|
assert.EqualError(t, f.AddDataValidation("Sheet:1", nil), ErrSheetNameInvalid.Error())
|
2018-09-01 19:38:30 +08:00
|
|
|
}
|
2020-03-13 00:48:16 +08:00
|
|
|
|
|
|
|
func TestDeleteDataValidation(t *testing.T) {
|
|
|
|
f := NewFile()
|
|
|
|
assert.NoError(t, f.DeleteDataValidation("Sheet1", "A1:B2"))
|
|
|
|
|
2023-03-01 13:25:17 +08:00
|
|
|
dv := NewDataValidation(true)
|
|
|
|
dv.Sqref = "A1:B2"
|
|
|
|
assert.NoError(t, dv.SetRange(10, 20, DataValidationTypeWhole, DataValidationOperatorBetween))
|
|
|
|
dv.SetInput("input title", "input body")
|
|
|
|
assert.NoError(t, f.AddDataValidation("Sheet1", dv))
|
2020-03-13 00:48:16 +08:00
|
|
|
assert.NoError(t, f.DeleteDataValidation("Sheet1", "A1:B2"))
|
2021-08-06 22:44:43 +08:00
|
|
|
|
2023-03-01 13:25:17 +08:00
|
|
|
dv.Sqref = "A1"
|
|
|
|
assert.NoError(t, f.AddDataValidation("Sheet1", dv))
|
2021-08-06 22:44:43 +08:00
|
|
|
assert.NoError(t, f.DeleteDataValidation("Sheet1", "B1"))
|
|
|
|
assert.NoError(t, f.DeleteDataValidation("Sheet1", "A1"))
|
|
|
|
|
2023-03-01 13:25:17 +08:00
|
|
|
dv.Sqref = "C2:C5"
|
|
|
|
assert.NoError(t, f.AddDataValidation("Sheet1", dv))
|
2021-08-06 22:44:43 +08:00
|
|
|
assert.NoError(t, f.DeleteDataValidation("Sheet1", "C4"))
|
|
|
|
|
2023-03-01 13:25:17 +08:00
|
|
|
dv = NewDataValidation(true)
|
|
|
|
dv.Sqref = "D2:D2 D3 D4"
|
|
|
|
assert.NoError(t, dv.SetRange(10, 20, DataValidationTypeWhole, DataValidationOperatorBetween))
|
|
|
|
dv.SetInput("input title", "input body")
|
|
|
|
assert.NoError(t, f.AddDataValidation("Sheet1", dv))
|
2021-08-06 22:44:43 +08:00
|
|
|
assert.NoError(t, f.DeleteDataValidation("Sheet1", "D3"))
|
|
|
|
|
2020-03-13 00:48:16 +08:00
|
|
|
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestDeleteDataValidation.xlsx")))
|
|
|
|
|
2023-03-01 13:25:17 +08:00
|
|
|
dv.Sqref = "A"
|
|
|
|
assert.NoError(t, f.AddDataValidation("Sheet1", dv))
|
2021-12-07 00:26:53 +08:00
|
|
|
assert.EqualError(t, f.DeleteDataValidation("Sheet1", "A1"), newCellNameToCoordinatesError("A", newInvalidCellNameError("A")).Error())
|
2021-08-06 22:44:43 +08:00
|
|
|
|
2021-12-07 00:26:53 +08:00
|
|
|
assert.EqualError(t, f.DeleteDataValidation("Sheet1", "A1:A"), newCellNameToCoordinatesError("A", newInvalidCellNameError("A")).Error())
|
2021-08-06 22:44:43 +08:00
|
|
|
ws, ok := f.Sheet.Load("xl/worksheets/sheet1.xml")
|
|
|
|
assert.True(t, ok)
|
|
|
|
ws.(*xlsxWorksheet).DataValidations.DataValidation[0].Sqref = "A1:A"
|
2021-12-07 00:26:53 +08:00
|
|
|
assert.EqualError(t, f.DeleteDataValidation("Sheet1", "A1:B2"), newCellNameToCoordinatesError("A", newInvalidCellNameError("A")).Error())
|
2021-08-06 22:44:43 +08:00
|
|
|
|
This closes #1425, breaking changes for sheet name (#1426)
- Checking and return error for invalid sheet name instead of trim invalid characters
- Add error return for the 4 functions: `DeleteSheet`, `GetSheetIndex`, `GetSheetVisible` and `SetSheetName`
- Export new error 4 constants: `ErrSheetNameBlank`, `ErrSheetNameInvalid`, `ErrSheetNameLength` and `ErrSheetNameSingleQuote`
- Rename exported error constant `ErrExistsWorksheet` to `ErrExistsSheet`
- Update unit tests for 90 functions: `AddChart`, `AddChartSheet`, `AddComment`, `AddDataValidation`, `AddPicture`, `AddPictureFromBytes`, `AddPivotTable`, `AddShape`, `AddSparkline`, `AddTable`, `AutoFilter`, `CalcCellValue`, `Cols`, `DeleteChart`, `DeleteComment`, `DeleteDataValidation`, `DeletePicture`, `DeleteSheet`, `DuplicateRow`, `DuplicateRowTo`, `GetCellFormula`, `GetCellHyperLink`, `GetCellRichText`, `GetCellStyle`, `GetCellType`, `GetCellValue`, `GetColOutlineLevel`, `GetCols`, `GetColStyle`, `GetColVisible`, `GetColWidth`, `GetConditionalFormats`, `GetDataValidations`, `GetMergeCells`, `GetPageLayout`, `GetPageMargins`, `GetPicture`, `GetRowHeight`, `GetRowOutlineLevel`, `GetRows`, `GetRowVisible`, `GetSheetIndex`, `GetSheetProps`, `GetSheetVisible`, `GroupSheets`, `InsertCol`, `InsertPageBreak`, `InsertRows`, `MergeCell`, `NewSheet`, `NewStreamWriter`, `ProtectSheet`, `RemoveCol`, `RemovePageBreak`, `RemoveRow`, `Rows`, `SearchSheet`, `SetCellBool`, `SetCellDefault`, `SetCellFloat`, `SetCellFormula`, `SetCellHyperLink`, `SetCellInt`, `SetCellRichText`, `SetCellStr`, `SetCellStyle`, `SetCellValue`, `SetColOutlineLevel`, `SetColStyle`, `SetColVisible`, `SetColWidth`, `SetConditionalFormat`, `SetHeaderFooter`, `SetPageLayout`, `SetPageMargins`, `SetPanes`, `SetRowHeight`, `SetRowOutlineLevel`, `SetRowStyle`, `SetRowVisible`, `SetSheetBackground`, `SetSheetBackgroundFromBytes`, `SetSheetCol`, `SetSheetName`, `SetSheetProps`, `SetSheetRow`, `SetSheetVisible`, `UnmergeCell`, `UnprotectSheet` and
`UnsetConditionalFormat`
- Update documentation of the set style functions
Co-authored-by: guoweikuang <weikuang.guo@shopee.com>
2022-12-23 00:54:40 +08:00
|
|
|
// Test delete data validation on no exists worksheet
|
2022-08-28 00:16:41 +08:00
|
|
|
assert.EqualError(t, f.DeleteDataValidation("SheetN", "A1:B2"), "sheet SheetN does not exist")
|
This closes #1425, breaking changes for sheet name (#1426)
- Checking and return error for invalid sheet name instead of trim invalid characters
- Add error return for the 4 functions: `DeleteSheet`, `GetSheetIndex`, `GetSheetVisible` and `SetSheetName`
- Export new error 4 constants: `ErrSheetNameBlank`, `ErrSheetNameInvalid`, `ErrSheetNameLength` and `ErrSheetNameSingleQuote`
- Rename exported error constant `ErrExistsWorksheet` to `ErrExistsSheet`
- Update unit tests for 90 functions: `AddChart`, `AddChartSheet`, `AddComment`, `AddDataValidation`, `AddPicture`, `AddPictureFromBytes`, `AddPivotTable`, `AddShape`, `AddSparkline`, `AddTable`, `AutoFilter`, `CalcCellValue`, `Cols`, `DeleteChart`, `DeleteComment`, `DeleteDataValidation`, `DeletePicture`, `DeleteSheet`, `DuplicateRow`, `DuplicateRowTo`, `GetCellFormula`, `GetCellHyperLink`, `GetCellRichText`, `GetCellStyle`, `GetCellType`, `GetCellValue`, `GetColOutlineLevel`, `GetCols`, `GetColStyle`, `GetColVisible`, `GetColWidth`, `GetConditionalFormats`, `GetDataValidations`, `GetMergeCells`, `GetPageLayout`, `GetPageMargins`, `GetPicture`, `GetRowHeight`, `GetRowOutlineLevel`, `GetRows`, `GetRowVisible`, `GetSheetIndex`, `GetSheetProps`, `GetSheetVisible`, `GroupSheets`, `InsertCol`, `InsertPageBreak`, `InsertRows`, `MergeCell`, `NewSheet`, `NewStreamWriter`, `ProtectSheet`, `RemoveCol`, `RemovePageBreak`, `RemoveRow`, `Rows`, `SearchSheet`, `SetCellBool`, `SetCellDefault`, `SetCellFloat`, `SetCellFormula`, `SetCellHyperLink`, `SetCellInt`, `SetCellRichText`, `SetCellStr`, `SetCellStyle`, `SetCellValue`, `SetColOutlineLevel`, `SetColStyle`, `SetColVisible`, `SetColWidth`, `SetConditionalFormat`, `SetHeaderFooter`, `SetPageLayout`, `SetPageMargins`, `SetPanes`, `SetRowHeight`, `SetRowOutlineLevel`, `SetRowStyle`, `SetRowVisible`, `SetSheetBackground`, `SetSheetBackgroundFromBytes`, `SetSheetCol`, `SetSheetName`, `SetSheetProps`, `SetSheetRow`, `SetSheetVisible`, `UnmergeCell`, `UnprotectSheet` and
`UnsetConditionalFormat`
- Update documentation of the set style functions
Co-authored-by: guoweikuang <weikuang.guo@shopee.com>
2022-12-23 00:54:40 +08:00
|
|
|
// Test delete all data validation with invalid sheet name
|
|
|
|
assert.EqualError(t, f.DeleteDataValidation("Sheet:1"), ErrSheetNameInvalid.Error())
|
|
|
|
// Test delete all data validations in the worksheet
|
2022-06-15 17:28:59 +08:00
|
|
|
assert.NoError(t, f.DeleteDataValidation("Sheet1"))
|
|
|
|
assert.Nil(t, ws.(*xlsxWorksheet).DataValidations)
|
2020-03-13 00:48:16 +08:00
|
|
|
}
|