xcoldec

package module
v1.3.2 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2023 License: MIT Imports: 9 Imported by: 0

README

xcoldec

xcoldec implements decoding of xlsx column per row to golang structure.

Installation

go get github/minhquang4334/xcoldec

Usage

package main

import (
	"fmt"
	"log"

	"github.com/minhquang4334/xcoldec"

	"github.com/xuri/excelize/v2"
)

type EmbeddedStruct struct {
	ColumnK string `col:"K"`
	ColumnL int    `col:"L"`
}

type SubStruct struct {
	ColJ         int    `col:"J"`
	ColK         string `col:"K"`
}

type SupportedType struct {
  Str      string    `col:"A,omitempty"`
  Int      int       `col:"B"` // should not be empty
  Uint     uint      `col:"C"`
  Boolean  bool      `col:"D"`
  Float32  float32   `col:"E"`
  Float64  float64   `col:"F"`
  StrSlice []string  `col:"G"`
  IntSlice []int     `col:"H"`
  Time     time.Time `col:"I"`
  EmbeddedStruct
  SubStruct SubStruct
}


func main() {
	f, err := excelize.OpenFile("sample.xlsx")
	if err != nil {
		fmt.Println(err)
		return
	}
	defer func() {
		if err := f.Close(); err != nil {
			fmt.Println(err)
		}
	}()

	rows, err := f.GetRows("Sheet1")
	if err != nil {
		fmt.Println(err)
		return
	}
	for _, row := range rows[1:] {
		hoge := SupportedType{}
		dec := xcoldec.NewDecoder(row)
		if err := dec.Decode(&hoge); err != nil {
			log.Fatal(err)
		}

		fmt.Printf("%+v\n", hoge)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidPointerOfStruct = errors.New("v must be a pointer of struct")
)

Functions

This section is empty.

Types

type DecodeError added in v1.3.2

type DecodeError struct {
	// contains filtered or unexported fields
}

func (*DecodeError) Error added in v1.3.2

func (e *DecodeError) Error() string

type Decoder

type Decoder struct {
	// contains filtered or unexported fields
}

Decoder decodes values from row.

func NewDecoder

func NewDecoder(row []string) *Decoder

NewDecoder return a new decoder that read from row.

func (*Decoder) Decode

func (d *Decoder) Decode(v interface{}) error

Decode decodes given row (string slice) to struct of column.

Supported Go data types are: - string - int, float family - Boolean - the type implements encoding.TextUnmarshaler - slices

  • the element type must be Decode() support type
  • element will split by "," from given string

- time.Time - embedded struct - sub struct

Jump to

Keyboard shortcuts

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