xlsy

package module
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2024 License: Apache-2.0 Imports: 15 Imported by: 1

README

xsly - XLS marshaler

GoReportCard GoDoc

This library is compatible with Go 1.17+

Please refer to CHANGELOG.md if you encounter breaking changes.

Motivation

This project was create to marshal arbitrary go struct/slice into XSL format.

Introduction

This project uses excelize project to convert complex go type into XLS.

You can use the following struct Tag to customize output

  • Name: header or sheet name
  • Style: CSS like style that are translated to excelize.Style: i.e: cell.style:{color:red;font-style:bold}
  • Style use the following destination prefix: cell|header|column: i.e. header.style:{color:red;font-style:bold}
  • Style: StyleRef: style reference
  • Ignore: ignore struct field
  • Blank: blank row or column
  • Position: optional field column position
  • Embed: by default relation are moved to separate sheet, embed flatten relation in the same sheet
  • Direction: vertical uses rows as column
  • OffsetX: initial row offset
  • OffsetY: initial column offset

The following style are currently supported

  • color
  • background-color
  • background-gradient
  • font-style
  • font-family
  • vertical-align
  • text-align
  • text-wrap
  • text-indent
  • width
  • width-max
  • height
  • format

Usage


package mypkg

import (
	"bytes"
	"context"
	"github.com/viant/afs"
	"github.com/viant/afs/file"
	"github.com/viant/xlsy"
	"log"
	"time"
)

func ExampleNewMarshaller() {

	now := time.Now().Truncate(time.Second)
	type Record struct {
		ID      int        `xls:"name=Id"`
		Name    string     `xls:"name=Name"`
		Active  bool       `xls:"name=Active,style={color:blue}"`
		Started *time.Time `xls:"name=Started,style={width:200px;format:date}"`
	}
	var records = []*Record{
		{
			ID:      1,
			Name:    "Item 1",
			Active:  true,
			Started: &now,
		},
		{
			ID:      2,
			Name:    "Item 2",
			Active:  false,
			Started: &now,
		},
		{
			ID:      3,
			Name:    "Item 3",
			Active:  true,
			Started: &now,
		},
	}

	marshaller := xlsy.NewMarshaller(xlsy.WithDefaultStyle("font-style:bold"))
	data, err := marshaller.Marshal(records)
	if err != nil {
		log.Fatal(err)
	}
	fs := afs.New()
	err = fs.Upload(context.Background(), "test.xlsx", file.DefaultFileOsMode, bytes.NewReader(data))
	if err != nil {
		log.Fatal(err)
	}
}


License

The source code is made available under the terms of the Apache License, Version 2, as stated in the file LICENSE.

Individual files may be made available under their own specific license, all compatible with Apache License, Version 2. Please see individual files for details.

Contributing to xsly

xsly is an open source project and contributors are welcome!

See TODO list

License

The source code is made available under the terms of the Apache License, Version 2, as stated in the file LICENSE.

Individual files may be made available under their own specific license, all compatible with Apache License, Version 2. Please see individual files for details.

Credits and Acknowledgements

Library Author: Adrian Witas

Documentation

Index

Examples

Constants

View Source
const (

	//DirectionHorizontal = "horizontal"
	//DirectionVertical vertial direction
	DirectionVertical = "vertical"
)
View Source
const TagName = "xls"

Variables

This section is empty.

Functions

func ParseStyle

func ParseStyle(definition string, style *Style) error

Types

type Column

type Column struct {
	Position int
	Name     string
	Tag      *Tag
	Field    *xunsafe.Field

	Table *Table
	// contains filtered or unexported fields
}

ColumnAddress represents acolumn

func (*Column) CellStyleID

func (c *Column) CellStyleID(stylizer *Stylizer) *int

CellStyleID returns a style ID

func (*Column) ColumnStyleID

func (c *Column) ColumnStyleID(stylizer *Stylizer) *int

ColumnStyleID returns column style ID or nil

func (*Column) HeaderStyleID

func (c *Column) HeaderStyleID(stylizer *Stylizer) *int

HeaderStyleID returns header style ID or nil

func (*Column) Width

func (c *Column) Width(stylizer *Stylizer) *Length

Width returns width

type Columns

type Columns []*Column

Columns represents columns

type Cursor

type Cursor int

func (Cursor) RawString

func (c Cursor) RawString() string

func (Cursor) String

func (c Cursor) String() string

type Group

type Group struct {
	Columns Columns
}

type Length

type Length struct {
	Size float64
	Unit string
}

func (*Length) Value

func (l *Length) Value() float64

type Marshaller

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

Marshaller represents xls marshalle

func NewMarshaller

func NewMarshaller(opts ...Option) *Marshaller

NewMarshaller create a marshaler with option

Example
package main

import (
	"bytes"
	"context"
	"github.com/viant/afs"
	"github.com/viant/afs/file"
	"github.com/viant/xlsy"
	"log"
	"time"
)

func main() {

	now := time.Now().Truncate(time.Second)
	type Record struct {
		ID      int        `xls:"name=ID"`
		Name    string     `xls:"name=Name"`
		Active  bool       `xls:"name=Active,style={color:blue}"`
		Started *time.Time `xls:"name=Started,style={width:200px;format:date}"`
	}
	var records = []*Record{
		{
			ID:      1,
			Name:    "Item 1",
			Active:  true,
			Started: &now,
		},
		{
			ID:      2,
			Name:    "Item 2",
			Active:  false,
			Started: &now,
		},
		{
			ID:      3,
			Name:    "Item 3",
			Active:  true,
			Started: &now,
		},
	}

	marshaller := xlsy.NewMarshaller(xlsy.WithDefaultStyle("font-style:bold"))
	data, err := marshaller.Marshal(records)
	if err != nil {
		log.Fatal(err)
	}
	fs := afs.New()
	err = fs.Upload(context.Background(), "test.xlsx", file.DefaultFileOsMode, bytes.NewReader(data))
	if err != nil {
		log.Fatal(err)
	}

}

func (*Marshaller) Marshal

func (m *Marshaller) Marshal(any interface{}) ([]byte, error)

Marshal marshall arbitrary type to xls

type Option

type Option func(m *session) error

Option represents marshaller session

func WithDefaultHeaderStyle

func WithDefaultHeaderStyle(definition string) Option

func WithDefaultStyle

func WithDefaultStyle(definition string) Option

func WithInverted

func WithInverted() Option

WithInverted return vertical option

func WithNamedStyles

func WithNamedStyles(pairs ...string) Option

WithNamedStyles accept name/style definition pairs

func WithTag

func WithTag(tag *Tag) Option

WithTag return tag session

type Row

type Row struct {
	Values   Values
	Snapshot Cursor
	StyleID  int
}

type Rows

type Rows []*Row

type Style

type Style struct {
	Destination string //default cell,
	ID          string
	Definition  string
	Header      *extStyle
	Cell        *extStyle
	Column      *extStyle
}

Style represents a style

func (*Style) Init

func (s *Style) Init() error

Init initializes style

type StyleTag

type StyleTag struct {
	Destination string
	Style       string
	Ref         string
}

func (*StyleTag) Definition

func (s *StyleTag) Definition() *Style

type Stylizer

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

Stylizer represents stylizer

func (*Stylizer) Register

func (s *Stylizer) Register(style *Style) (err error)

Register register a style

func (*Stylizer) Style

func (s *Stylizer) Style(style string) *Style

Style returns register style or nil

type Table

type Table struct {
	Parent   *Column
	Stylizer *Stylizer
	*Tag
	Columns     Columns
	Header      *Row
	Rows        Rows
	Type        reflect.Type
	IsStruct    bool
	Cardinality int
	// contains filtered or unexported fields
}

Table represents a table

func NewTable

func NewTable(rType reflect.Type, tableTag *Tag, aSession *session, parent *Column) (*Table, error)

NewTable creates a table

func (*Table) IsStandalone

func (t *Table) IsStandalone() bool

func (*Table) OffsetX

func (t *Table) OffsetX() int

OffsetX returns table x dim

func (*Table) OffsetY

func (t *Table) OffsetY() int

OffsetY returns table y dim

func (*Table) SheetName

func (t *Table) SheetName() string

SheetName returns table cheed name

func (*Table) UseRow

func (t *Table) UseRow(b bool) bool

type Tag

type Tag struct {
	*format.Tag
	WorkSheet    string
	HeaderStyle  *StyleTag
	CellStyle    *StyleTag
	ColumnStyle  *StyleTag
	SheetPos     int
	Blank        bool
	Position     *int
	Inverted     *bool //inverted orientation
	First        bool
	Row          int
	Column       int
	ColumnOffset int
	RowOffset    int
}

func NewTag added in v0.2.1

func NewTag() *Tag

func (*Tag) Invert

func (t *Tag) Invert() bool

type Values

type Values []*value

Jump to

Keyboard shortcuts

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