码迷,mamicode.com
首页 > 其他好文 > 详细

Golang生成xlsx

时间:2020-04-13 22:29:26      阅读:98      评论:0      收藏:0      [点我收藏+]

标签:str   $1   github   sky   port   width   func   tin   from   

// Copyright 2016 - 2020 The excelize Authors. All rights reserved. Use of
// 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
// and read from XLSX files. Support reads and writes XLSX file generated by
// Microsoft Excel? 2007 and later. Support save file without losing original
// charts of XLSX. This library needs Go version 1.10 or later.

package UnitTest

import (
"github.com/360EntSecGroup-Skylar/excelize/v2"
"github.com/stretchr/testify/assert"
"path/filepath"
"strings"
"testing"
)

func TestDataValidation(t *testing.T) {
resultFile := filepath.Join("test", "TestDataValidation.xlsx")

f := excelize.NewFile()

dvRange := excelize.NewDataValidation(true)
dvRange.Sqref = "A1:B2"
assert.NoError(t, dvRange.SetRange(10, 20, excelize.DataValidationTypeWhole, excelize.DataValidationOperatorBetween))
dvRange.SetError(excelize.DataValidationErrorStyleStop, "error title", "error body")
dvRange.SetError(excelize.DataValidationErrorStyleWarning, "error title", "error body")
dvRange.SetError(excelize.DataValidationErrorStyleInformation, "error title", "error body")
assert.NoError(t, f.AddDataValidation("Sheet1", dvRange))
assert.NoError(t, f.SaveAs(resultFile))

dvRange = excelize.NewDataValidation(true)
dvRange.Sqref = "A3:B4"
assert.NoError(t, dvRange.SetRange(10, 20, excelize.DataValidationTypeWhole, excelize.DataValidationOperatorGreaterThan))
dvRange.SetInput("input title", "input body")
assert.NoError(t, f.AddDataValidation("Sheet1", dvRange))
assert.NoError(t, f.SaveAs(resultFile))

dvRange = excelize.NewDataValidation(true)
dvRange.Sqref = "A5:B6"
assert.NoError(t, dvRange.SetDropList([]string{"1", "2", "3"}))
assert.NoError(t, f.AddDataValidation("Sheet1", dvRange))
assert.NoError(t, f.SaveAs(resultFile))

}

func TestDataValidationError(t *testing.T) {
resultFile := filepath.Join("test", "TestDataValidationError.xlsx")

f := excelize.NewFile()
assert.NoError(t, f.SetCellStr("Sheet1", "E1", "E1"))
assert.NoError(t, f.SetCellStr("Sheet1", "E2", "E2"))
assert.NoError(t, f.SetCellStr("Sheet1", "E3", "E3"))

dvRange := excelize.NewDataValidation(true)
dvRange.SetSqref("A7:B8")
dvRange.SetSqref("A7:B8")
assert.NoError(t, dvRange.SetSqrefDropList("$E$1:$E$3", true))

err := dvRange.SetSqrefDropList("$E$1:$E$3", false)
assert.EqualError(t, err, "cross-sheet sqref cell are not supported")

assert.NoError(t, f.AddDataValidation("Sheet1", dvRange))
assert.NoError(t, f.SaveAs(resultFile))

dvRange = excelize.NewDataValidation(true)
err = dvRange.SetDropList(make([]string, 258))
if dvRange.Formula1 != "" {
	t.Errorf("data validation error. Formula1 must be empty!")
	return
}
assert.EqualError(t, err, "data validation must be 0-255 characters")
assert.NoError(t, dvRange.SetRange(10, 20, excelize.DataValidationTypeWhole, excelize.DataValidationOperatorGreaterThan))
dvRange.SetSqref("A9:B10")

assert.NoError(t, f.AddDataValidation("Sheet1", dvRange))
assert.NoError(t, f.SaveAs(resultFile))

// Test width invalid data validation formula.
var dataValidationFormulaStrLen = 257
dvRange.Formula1 = strings.Repeat("s", dataValidationFormulaStrLen+22)
assert.EqualError(t, dvRange.SetRange(10, 20, excelize.DataValidationTypeWhole, excelize.DataValidationOperatorGreaterThan), "data validation must be 0-255 characters")

// Test add data validation on no exists worksheet.
f = excelize.NewFile()
assert.EqualError(t, f.AddDataValidation("SheetN", nil), "sheet SheetN is not exist")

}

Golang生成xlsx

标签:str   $1   github   sky   port   width   func   tin   from   

原文地址:https://www.cnblogs.com/littlehb/p/12694269.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!