xs

package module
v1.0.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 29, 2020 License: MIT Imports: 8 Imported by: 0

README

xs

Travis Status for KarlTango/xs codecov for KarlTango/xs Go Report Card GolangCI Release

简体中文

Installation

go get github.com/KarlTango/xs

Requirements

  • github.com/tealeg/xlsx

Index

  1. Examples
    1. Unmarshal
    2. Marshal

Examples

Unmarshal

If you wan to get message from a excel file like this:

Excel file (Excel could be found in example folder).

You could define struct with xs tag to map struct key excel column name

type User struct {
	Username string `xs:"Username"`
	Age      int    `xs:"Age"`
	Sex      string `xs:"Sex"`
}

and then you can use UnmarshalFromFile

var users = make([]User, 0)
filePath := "example/test.xlsx"
err := UnmarshalFromFile(filePath, &users)

or use Unmarshal

var users = make([]User, 0)
filePath := "example/test.xlsx"
body, _ := ioutil.ReadFile(filePath)
err = Unmarshal(body, &users)
Marshal

The other way round, if you want to write a excel from go struct slice, you can use Marshal

Mutiple sheet
type User struct {
	Username string `xs:"Username"`
	Age      int    `xs:"Age"`
	Sex      string `xs:"Sex"`
}

type User2 struct {
	Username string  `xs:"Username"`
	Salary   float64 `xs:"Salary"`
}

users := []User{{"Karl", 25, "Male"}, {"Ann", 18, "Female"}}
users2 := []User2{{"Karl", 8000.00}, {"Ann", 9999.50}}

//marshal
body, err := Marshal(users, users2)
if err != nil {
    //TODO: process err
}

//write file
err = ioutil.WriteFile("user.xlsx", body, 06666)
if err != nil {
    //TODO: process err
}

Documentation

Index

Constants

View Source
const (
	XS_TAG                = "xs"
	NULL_TAG              = ""
	IGNORE_TAG            = "-"
	OMITEMPTY             = "omitempty"
	TAG_SEPERATER         = ","
	TAG_FORMATE_SEPERATER = ":"
	HYPERLINK_FORMATE     = "hyperlink"
)

Variables

This section is empty.

Functions

func GetTags

func GetTags(s interface{}) map[string]bool

parse tags. return map[string]bool, which key is tag and value is if the tag is necessary

func Marshal

func Marshal(ss ...interface{}) ([]byte, error)

Marshal read the data from the slice of struct and write it into xlsx file. Every slice of struct will be written in a sheet

func Unmarshal

func Unmarshal(body []byte, ss ...interface{}) error

Unmarshal parses the xlsx data and stores the result in the value pointed to by v. If v is nil or not a pointer, Unmarshal returns an InvalidUnmarshalError.

func UnmarshalFromFile

func UnmarshalFromFile(filePath string, ss ...interface{}) error

UnmarshalFromFile read the xlsx file and parses the data and stores the result in the value pointed to by v. If v is nil or not a pointer, Unmarshal returns an InvalidUnmarshalError.

Types

type EmptySheetError

type EmptySheetError struct {
}

the sheet need to contains more than one row

func (*EmptySheetError) Error

func (e *EmptySheetError) Error() string

type InvalidMarshalError

type InvalidMarshalError struct {
	Type reflect.Type
}

the parameter s is in a invalid type

func (*InvalidMarshalError) Error

func (e *InvalidMarshalError) Error() string

type InvalidUnmarshalError

type InvalidUnmarshalError struct {
	Type reflect.Type
}

the parameter s is in a invalid type

func (*InvalidUnmarshalError) Error

func (e *InvalidUnmarshalError) Error() string

type LackColError

type LackColError struct {
	Row    int
	Header string
}

In some rows, the value of some required fields is null

func (*LackColError) Error

func (e *LackColError) Error() string

type LackHeaderError

type LackHeaderError struct {
	Header string
}

lack of required header

func (*LackHeaderError) Error

func (e *LackHeaderError) Error() string

type OpenFileError

type OpenFileError struct {
	FilePath string
	Err      error
}

xlsx file can't be opened without error

func (*OpenFileError) Error

func (e *OpenFileError) Error() string

type SheetAndSLiceMismatched

type SheetAndSLiceMismatched struct {
	SheetNum int
	SliceNum int
}

func (*SheetAndSLiceMismatched) Error

func (e *SheetAndSLiceMismatched) Error() string

type TagInfo

type TagInfo struct {
	Index       int
	Format      string
	IsHyperlink bool
	Omitempty   bool
}

store the information of tag

type TagInfoMap

type TagInfoMap struct {
	Headers []string
	M       map[string]TagInfo
}

store the information of all tags. Headers is a array of tag M map the tag and tag's TagInfo

func GetTagInfo

func GetTagInfo(s interface{}) TagInfoMap

get the information of all tags

type TypeMismatchedError

type TypeMismatchedError struct {
	Data         string
	RequiredType reflect.Type
	GivenType    reflect.Type
}

the type of data mismatched the required type

func (*TypeMismatchedError) Error

func (e *TypeMismatchedError) Error() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL