exceldb

package module
v0.8.1 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2022 License: MIT Imports: 7 Imported by: 1

README

exceldb

exceldb is a pure Go library importing XLSX files directly into an SQLite database.

package main

import (
	"fmt"
	"github.com/redtoad/exceldb"
)

func main() {

	path := "Book1.xlsx"
	db, err := exceldb.LoadFromExcel(path, exceldb.InMemoryDb,
		exceldb.DateColum("Date", "01/02/06"))
	if err != nil {
		panic(err)
	}
	defer db.Close()

	rows, err := db.Query(`SELECT 
	strftime("%m-%Y", "Date") as 'month-year',
		Employee,
		SUM("Hours worked")
	FROM data
	WHERE "Status" != "non billable"
	GROUP BY strftime("%m-%Y", "Date"), Employee;`)
	if err != nil {
		panic(err)
	}
	defer rows.Close()

	for rows.Next() {
		var monthYear string
		var resource string
		var sum float64
		if err := rows.Scan(&monthYear, &resource, &sum); err != nil {
			panic(err)
		}
		fmt.Printf("%s -> %s -> %f\n", monthYear, resource, sum)
	}

}

TODOs

  • Load mutliple XLSX files
  • Guess column data type from cell format

Changes

0.8.1 (2022-07-07)
  • Fix support for date values.
0.8.0 (2021-08-08)
  • Initial release.

License

This program is published under the terms of the MIT License.

Documentation

Index

Constants

View Source
const (
	FormatText   = 1
	FormatDate   = 2
	FormatNumber = 3
	FormatFloat  = 4
)
View Source
const InMemoryDb = "file::memory:" //?cache=shared"

Variables

View Source
var ErrNoMoreRowsFound = errors.New("no rows to read from")

ErrNoMoreRowsFound will be raised if no (or no more) rows can be read from an Excel file.

Functions

func KeepAsIs

func KeepAsIs(val string) (interface{}, error)

KeepAsIs is a Converter function which will not alter the received string value.

func LoadFromExcel

func LoadFromExcel(path string, dsn string, options ...Column) (*sql.DB, error)

LoadFromExcel will load all rows from the first sheet in the Excel workbook into a newly created SQLite database. You may specify how data in specific columns is interpreted by supplying Column definitons. For those you don't, they will be treated as TEXT.

Types

type Column

type Column struct {
	// Name of the column (usually contained in the first row)
	Name string
	// Column format (e.g. FormatNumber)
	Format int
	// Converter function to convert cell content (string) to native value (Go type)
	Func Converter
}

Column describes all values in a column in an Excel sheet.

func DateColum

func DateColum(name string, dateFormat string) Column

DateColum returns a Column with a Converter function parsing date strings using dateFormat (as used by time.Parse()).

type Converter

type Converter func(val string) (interface{}, error)

Converter is a function to convert a cell value from string to a Go native data type.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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