spreadsheet

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2018 License: MIT Imports: 9 Imported by: 1

README

Spreadsheet

Spreadsheet is a Go package providing a simple interface for reading spreadsheet files, including XLSX and CSV.

Installation

Please use gopkg.in for stable releases.

go get -u gopkg.in/beta/spreadsheet.v1

Getting started

For full documentation please refer to https://godoc.org/github.com/beta/spreadsheet.

package main

import (
	"fmt"

	"gopkg.in/beta/spreadsheet.v1"
)

func main() {
	// Open sheet.
	ss, err := spreadsheet.Open("test.xlsx")
	if err != nil {
		panic(err)
	}

	s1 := ss.Sheets[0]
	fmt.Printf("Sheet name: %s\n", s1.Name)
	// Traverse sheet.
	for _, row := range s1.Rows {
		for _, cell := range row.Cells {
			switch cell.Type() {
			case spreadsheet.String:
				fmt.Printf("Value: %s\n", cell.String())
			case spreadsheet.Numeric:
				f, _ := cell.Float()
				fmt.Printf("Value: %f\n", f)
				i, _ := cell.Int()
				fmt.Printf("Value: %d\n", i)
				i64, _ := cell.Int64()
				fmt.Printf("Value: %d\n", i64)
			case spreadsheet.Bool:
				b, _ := cell.Bool()
				fmt.Printf("Value: %t\n", b)
			}
		}
	}

	s2 := ss.SheetsByName["Sheet 2"] // Pick sheet by name.
	cell := s2.Cell(2, 3)            // Pick cell by coordinate.
	if cell.Is(spreadsheet.String) { // Check cell type.
		fmt.Printf("Value: %s\n", cell.String())
	}
}

License

MIT

Documentation

Overview

Package spreadsheet provides simple a interface to read spreadsheet files including XLSX and CSV.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cell

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

Cell holds a single piece of data in a spreadsheet.

func (*Cell) Bool

func (c *Cell) Bool() (bool, error)

Bool returns the cell data as a bool. If the cell data is not a bool

func (*Cell) Float

func (c *Cell) Float() (float64, error)

Float returns the cell data as a float. If the cell data is not a numeric value, an error is returned.

func (*Cell) Int

func (c *Cell) Int() (int, error)

Int returns the cell data as an int. If the cell data is not a numeric value, an error is returned.

func (*Cell) Int64

func (c *Cell) Int64() (int64, error)

Int64 returns the cell data as an int64. If the cell data is not a numeric value, an error is returned.

func (*Cell) Is

func (c *Cell) Is(t CellDataType) bool

Is returns true if data type of cell c is the same with the given type t.

func (*Cell) String

func (c *Cell) String() string

String returns the cell data as a string.

func (*Cell) Type

func (c *Cell) Type() CellDataType

Type returns the data type of cell c.

type CellDataType

type CellDataType uint8

CellDataType represents the primitive data type in spreadsheet cells.

const (
	String CellDataType = iota
	Numeric
	Bool
)

CellDataType constants.

func (CellDataType) Name

func (t CellDataType) Name() string

Name returns name of cell data type t.

type Row

type Row struct {
	Cells []*Cell
}

Row is a single row of data in a spreadsheet, containing multiple cells.

type Sheet

type Sheet struct {
	Name string
	Rows []*Row
}

Sheet is a single page in a spreadsheet.

func (*Sheet) Cell

func (s *Sheet) Cell(i int, j int) *Cell

Cell returns the j-th cell in the i-th row of sheet s.

type Spreadsheet

type Spreadsheet struct {
	Sheets       []*Sheet // For CSV there's only 1 sheet, named "Sheet 1".
	SheetsByName map[string]*Sheet
}

Spreadsheet is a high-level interface for spreadsheet files including XLSX, XLS and CSV. A Spreadsheet contains one or more sheets of data.

func Open

func Open(filePath string) (*Spreadsheet, error)

Open opens a spreadsheet file. filePath is the path to the spreadsheet file to be opened. Supported file types are XLSX and CSV (as described in RFC 4180). A Spreadsheet pointer will be returned if successful. Otherwise, a nil pointer will be returned with an error indicating what is wrong.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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