csvbook

package module
v0.0.0-...-7fe0fac Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2024 License: MIT Imports: 10 Imported by: 0

README

csvbook

csvbook is a Go library that provides a simple interface for managing and manipulating tabular data in CSV format. It supports reading, writing, and modifying CSV data, both in-memory and through zip archives. It also provides a way to handle metadata for both individual sheets and the entire file.

Installation

To install csvbook, use the following Go command:

go get github.com/ic-it/go-csvbook

Usage

Creating a New Sheet

Create a new sheet using the NewSheet function:

import "github.com/ic-it/go-csvbook"

sheet := csvbook.NewSheet()
Writing and Reading Data
Writing Data
  • Write a Line
err := sheet.WriteLine([]any{"Column1", "Column2"})
  • Write a Cell
err := sheet.WriteCell("Value", lineNum, colNum)
Reading Data
  • Read a Line
line, err := sheet.ReadLine(lineNum)
  • Read a Cell
value, err := sheet.ReadCell(lineNum, colNum)
Managing Metadata
  • Set Metadata
sheet.SetMeta("key", "value")
  • Get Metadata
value := sheet.GetMeta("key")
Working with Files

You can manage multiple sheets and save them to a zip file or read from one.

  • Create a New File
file, err := csvbook.NewFile()
  • Add a Sheet
sheet := file.CreateSheet("Sheet1")
  • Write to a File
err := file.WriteZipFile("data.zip")
  • Read from a File
err := file.ReadZipFile("data.zip")
  • Set File Metadata
file.SetMeta("key", "value")
  • Get File Metadata
value := file.GetMeta("key")

Examples

Here is a simple example demonstrating how to use the csvbook library:

package main

import "github.com/ic-it/go-csvbook"

func main() {
	f, err := csvbook.NewFile()

	if err != nil {
		panic(err)
	}

	sheet := f.CreateSheet("sheet1")

	sheet.WriteLine([]any{"a", "b", "c"})
	sheet.WriteLine([]any{"1", "2", "3"})
	sheet.WriteLine([]any{"4", "5", "6"})
	sheet.WriteLine([]any{"7", "8", "9", 123})

	sheet = f.CreateSheet("sheet2")

	sheet.WriteLine([]any{"a", "b", "c"})
	sheet.WriteLine([]any{"1", "2", "3"})
	sheet.WriteLine([]any{"4", "5", "6"})
	sheet.WriteLine([]any{"7", "8", "9", 123})

	sheet.WriteCell("111", 100, 15)

	err = f.WriteZipFile("test.zip")
	if err != nil {
		panic(err)
	}

	f, _ = csvbook.NewFile()
	err = f.ReadZipFile("test.zip")

	if err != nil {
		panic(err)
	}

	sheet = f.GetSheet("sheet1")

	for i := range sheet.LineNum() {
		line, _ := sheet.ReadLine(i)
		for _, cell := range line {
			print(cell.(string))
			print(" ")
		}
		println()
	}
}

License

csvbook is licensed under the MIT License. See the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidLineNum  = errors.New("invalid line number")
	ErrorInvalidColNum = errors.New("invalid cell number")
	ErrInvalidMetadata = errors.New("invalid metadata")
)

Functions

This section is empty.

Types

type File

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

func NewFile

func NewFile(
	meta ...map[string]string,
) (*File, error)

func (*File) CreateSheet

func (f *File) CreateSheet(name string) *Sheet

func (*File) DeleteSheet

func (f *File) DeleteSheet(name string)

func (*File) GetMeta

func (f *File) GetMeta(key string) any

func (*File) GetSheet

func (f *File) GetSheet(name string) *Sheet

func (*File) Read

func (f *File) Read(reader io.ReaderAt, size int64) error

func (*File) ReadZipFile

func (f *File) ReadZipFile(filename string) error

func (*File) SetMeta

func (f *File) SetMeta(key string, value any)

func (*File) Write

func (f *File) Write(writer io.Writer) error

func (*File) WriteZipFile

func (f *File) WriteZipFile(filename string) error

type Sheet

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

func NewSheet

func NewSheet() *Sheet

func (*Sheet) GetMeta

func (s *Sheet) GetMeta(key string) any

func (*Sheet) LineNum

func (s *Sheet) LineNum() int

Get the number of lines in the sheet.

func (*Sheet) ReadCell

func (s *Sheet) ReadCell(lineNum, colNum int) (any, error)

Read Cell

func (*Sheet) ReadLine

func (s *Sheet) ReadLine(lineNum int) ([]any, error)

Get Line Copy

func (*Sheet) SetMeta

func (s *Sheet) SetMeta(key string, value any)

func (*Sheet) WriteCell

func (s *Sheet) WriteCell(value any, lineNum, colNum int) error

Write Cell

func (*Sheet) WriteLine

func (s *Sheet) WriteLine(line []any, lineNum ...int) error

If lineNum is not provided, append line to the end of the sheet.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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