2021-03-30 23:02:22 +08:00
|
|
|
// Copyright 2016 - 2021 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
|
2021-08-15 00:06:40 +08:00
|
|
|
// and read from XLSX / XLSM / XLTM 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 data. This
|
|
|
|
// library needs Go version 1.15 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 (
|
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
|
|
|
|
|
|
|
dvRange := NewDataValidation(true)
|
|
|
|
dvRange.Sqref = "A1:B2"
|
2019-12-24 01:09:28 +08:00
|
|
|
assert.NoError(t, dvRange.SetRange(10, 20, DataValidationTypeWhole, DataValidationOperatorBetween))
|
2018-09-01 19:38:30 +08:00
|
|
|
dvRange.SetError(DataValidationErrorStyleStop, "error title", "error body")
|
2018-09-13 10:38:01 +08:00
|
|
|
dvRange.SetError(DataValidationErrorStyleWarning, "error title", "error body")
|
|
|
|
dvRange.SetError(DataValidationErrorStyleInformation, "error title", "error body")
|
2019-12-24 01:09:28 +08:00
|
|
|
assert.NoError(t, f.AddDataValidation("Sheet1", dvRange))
|
|
|
|
assert.NoError(t, f.SaveAs(resultFile))
|
2018-09-01 19:38:30 +08:00
|
|
|
|
|
|
|
dvRange = NewDataValidation(true)
|
|
|
|
dvRange.Sqref = "A3:B4"
|
2019-12-24 01:09:28 +08:00
|
|
|
assert.NoError(t, dvRange.SetRange(10, 20, DataValidationTypeWhole, DataValidationOperatorGreaterThan))
|
2018-09-01 19:38:30 +08:00
|
|
|
dvRange.SetInput("input title", "input body")
|
2019-12-24 01:09:28 +08:00
|
|
|
assert.NoError(t, f.AddDataValidation("Sheet1", dvRange))
|
|
|
|
assert.NoError(t, f.SaveAs(resultFile))
|
2018-09-01 19:38:30 +08:00
|
|
|
|
2021-08-26 00:48:18 +08:00
|
|
|
f.NewSheet("Sheet2")
|
|
|
|
assert.NoError(t, f.SetSheetRow("Sheet2", "A2", &[]interface{}{"B2", 1}))
|
|
|
|
assert.NoError(t, f.SetSheetRow("Sheet2", "A3", &[]interface{}{"B3", 3}))
|
|
|
|
dvRange = NewDataValidation(true)
|
|
|
|
dvRange.Sqref = "A1:B1"
|
|
|
|
assert.NoError(t, dvRange.SetRange("INDIRECT($A$2)", "INDIRECT($A$3)", DataValidationTypeWhole, DataValidationOperatorBetween))
|
|
|
|
dvRange.SetError(DataValidationErrorStyleStop, "error title", "error body")
|
|
|
|
assert.NoError(t, f.AddDataValidation("Sheet2", dvRange))
|
|
|
|
|
2018-09-01 19:38:30 +08:00
|
|
|
dvRange = NewDataValidation(true)
|
|
|
|
dvRange.Sqref = "A5:B6"
|
2021-07-31 00:31:51 +08:00
|
|
|
for _, listValid := range [][]string{
|
|
|
|
{"1", "2", "3"},
|
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`},
|
|
|
|
} {
|
|
|
|
dvRange.Formula1 = ""
|
|
|
|
assert.NoError(t, dvRange.SetDropList(listValid),
|
|
|
|
"SetDropList failed for valid input %v", listValid)
|
|
|
|
assert.NotEqual(t, "", dvRange.Formula1,
|
|
|
|
"Formula1 should not be empty for valid input %v", listValid)
|
|
|
|
}
|
|
|
|
assert.Equal(t, `<formula1>"A<,B>,C"",D ,E',F"</formula1>`, dvRange.Formula1)
|
2019-12-24 01:09:28 +08:00
|
|
|
assert.NoError(t, f.AddDataValidation("Sheet1", dvRange))
|
|
|
|
assert.NoError(t, f.SaveAs(resultFile))
|
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
|
|
|
|
|
|
|
dvRange := NewDataValidation(true)
|
2018-09-13 10:38:01 +08:00
|
|
|
dvRange.SetSqref("A7:B8")
|
|
|
|
dvRange.SetSqref("A7:B8")
|
2019-12-24 01:09:28 +08:00
|
|
|
assert.NoError(t, dvRange.SetSqrefDropList("$E$1:$E$3", true))
|
2018-12-27 18:51:44 +08:00
|
|
|
|
2018-09-13 10:38:01 +08:00
|
|
|
err := dvRange.SetSqrefDropList("$E$1:$E$3", false)
|
2018-12-27 18:51:44 +08:00
|
|
|
assert.EqualError(t, err, "cross-sheet sqref cell are not supported")
|
|
|
|
|
2019-12-24 01:09:28 +08:00
|
|
|
assert.NoError(t, f.AddDataValidation("Sheet1", dvRange))
|
2018-09-04 13:40:53 +08:00
|
|
|
|
2018-09-13 10:38:01 +08:00
|
|
|
dvRange = NewDataValidation(true)
|
2019-01-23 22:07:11 +08:00
|
|
|
err = dvRange.SetDropList(make([]string, 258))
|
|
|
|
if dvRange.Formula1 != "" {
|
|
|
|
t.Errorf("data validation error. Formula1 must be empty!")
|
|
|
|
return
|
|
|
|
}
|
2021-07-31 00:31:51 +08:00
|
|
|
assert.EqualError(t, err, ErrDataValidationFormulaLenth.Error())
|
2021-08-26 00:48:18 +08:00
|
|
|
assert.EqualError(t, dvRange.SetRange(nil, 20, DataValidationTypeWhole, DataValidationOperatorBetween), ErrParameterInvalid.Error())
|
|
|
|
assert.EqualError(t, dvRange.SetRange(10, nil, DataValidationTypeWhole, DataValidationOperatorBetween), ErrParameterInvalid.Error())
|
2019-12-24 01:09:28 +08:00
|
|
|
assert.NoError(t, dvRange.SetRange(10, 20, DataValidationTypeWhole, DataValidationOperatorGreaterThan))
|
2019-01-23 22:07:11 +08:00
|
|
|
dvRange.SetSqref("A9:B10")
|
2018-09-13 10:38:01 +08:00
|
|
|
|
2019-12-24 01:09:28 +08:00
|
|
|
assert.NoError(t, f.AddDataValidation("Sheet1", dvRange))
|
2019-12-22 00:02:09 +08:00
|
|
|
|
|
|
|
// Test width invalid data validation formula.
|
2021-07-31 00:31:51 +08:00
|
|
|
prevFormula1 := dvRange.Formula1
|
|
|
|
for _, keys := range [][]string{
|
|
|
|
make([]string, 257),
|
|
|
|
{strings.Repeat("s", 256)},
|
|
|
|
{strings.Repeat("\u4E00", 256)},
|
|
|
|
{strings.Repeat("\U0001F600", 128)},
|
|
|
|
{strings.Repeat("\U0001F600", 127), "s"},
|
|
|
|
} {
|
|
|
|
err = dvRange.SetDropList(keys)
|
|
|
|
assert.Equal(t, prevFormula1, dvRange.Formula1,
|
|
|
|
"Formula1 should be unchanged for invalid input %v", keys)
|
|
|
|
assert.EqualError(t, err, ErrDataValidationFormulaLenth.Error())
|
|
|
|
}
|
|
|
|
assert.NoError(t, f.AddDataValidation("Sheet1", dvRange))
|
|
|
|
assert.NoError(t, dvRange.SetRange(
|
|
|
|
-math.MaxFloat32, math.MaxFloat32,
|
|
|
|
DataValidationTypeWhole, DataValidationOperatorGreaterThan))
|
|
|
|
assert.EqualError(t, dvRange.SetRange(
|
|
|
|
-math.MaxFloat64, math.MaxFloat32,
|
|
|
|
DataValidationTypeWhole, DataValidationOperatorGreaterThan), ErrDataValidationRange.Error())
|
|
|
|
assert.EqualError(t, dvRange.SetRange(
|
|
|
|
math.SmallestNonzeroFloat64, math.MaxFloat64,
|
|
|
|
DataValidationTypeWhole, DataValidationOperatorGreaterThan), ErrDataValidationRange.Error())
|
|
|
|
assert.NoError(t, f.SaveAs(resultFile))
|
2019-12-22 00:02:09 +08:00
|
|
|
|
|
|
|
// Test add data validation on no exists worksheet.
|
|
|
|
f = NewFile()
|
|
|
|
assert.EqualError(t, f.AddDataValidation("SheetN", nil), "sheet SheetN is not exist")
|
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"))
|
|
|
|
|
|
|
|
dvRange := NewDataValidation(true)
|
|
|
|
dvRange.Sqref = "A1:B2"
|
|
|
|
assert.NoError(t, dvRange.SetRange(10, 20, DataValidationTypeWhole, DataValidationOperatorBetween))
|
|
|
|
dvRange.SetInput("input title", "input body")
|
|
|
|
assert.NoError(t, f.AddDataValidation("Sheet1", dvRange))
|
|
|
|
assert.NoError(t, f.DeleteDataValidation("Sheet1", "A1:B2"))
|
2021-08-06 22:44:43 +08:00
|
|
|
|
|
|
|
dvRange.Sqref = "A1"
|
|
|
|
assert.NoError(t, f.AddDataValidation("Sheet1", dvRange))
|
|
|
|
assert.NoError(t, f.DeleteDataValidation("Sheet1", "B1"))
|
|
|
|
assert.NoError(t, f.DeleteDataValidation("Sheet1", "A1"))
|
|
|
|
|
|
|
|
dvRange.Sqref = "C2:C5"
|
|
|
|
assert.NoError(t, f.AddDataValidation("Sheet1", dvRange))
|
|
|
|
assert.NoError(t, f.DeleteDataValidation("Sheet1", "C4"))
|
|
|
|
|
|
|
|
dvRange = NewDataValidation(true)
|
|
|
|
dvRange.Sqref = "D2:D2 D3 D4"
|
|
|
|
assert.NoError(t, dvRange.SetRange(10, 20, DataValidationTypeWhole, DataValidationOperatorBetween))
|
|
|
|
dvRange.SetInput("input title", "input body")
|
|
|
|
assert.NoError(t, f.AddDataValidation("Sheet1", dvRange))
|
|
|
|
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")))
|
|
|
|
|
2021-08-06 22:44:43 +08:00
|
|
|
dvRange.Sqref = "A"
|
|
|
|
assert.NoError(t, f.AddDataValidation("Sheet1", dvRange))
|
|
|
|
assert.EqualError(t, f.DeleteDataValidation("Sheet1", "A1"), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
|
|
|
|
|
|
|
|
assert.EqualError(t, f.DeleteDataValidation("Sheet1", "A1:A"), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
|
|
|
|
ws, ok := f.Sheet.Load("xl/worksheets/sheet1.xml")
|
|
|
|
assert.True(t, ok)
|
|
|
|
ws.(*xlsxWorksheet).DataValidations.DataValidation[0].Sqref = "A1:A"
|
|
|
|
assert.EqualError(t, f.DeleteDataValidation("Sheet1", "A1:B2"), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
|
|
|
|
|
2020-03-13 00:48:16 +08:00
|
|
|
// Test delete data validation on no exists worksheet.
|
|
|
|
assert.EqualError(t, f.DeleteDataValidation("SheetN", "A1:B2"), "sheet SheetN is not exist")
|
|
|
|
}
|