LoginSignup
3
1

More than 5 years have passed since last update.

Goでods(Open Office Spreadsheet)ファイルを読み込む

Last updated at Posted at 2018-11-16

急にGoがやりたくなったのですが、odsを読めるライブラリに使い方が書いてなくってtestから読み解かなければならなかったので、使い方のメモを残します。

ライブラリの導入

go get github.com/knieriem/odf

import

odfでなくて、その中のodsを使う。

import (
    "github.com/knieriem/odf/ods"
)

open&read

f, err := ods.Open("test.ods")
if err != nil {
    panic(err)
}
defer f.Close()

var doc ods.Doc
if err := f.ParseContent(&doc); err != nil {
    panic(err)
}

sheet

Docの[]Tableがsheet。Nameで名前が拾えるので必要なsheetを拾う。
1つしかsheetが無いんだったらdoc.Table[0]でもいい。

for _, sheet := range doc.Table {
    fmt.Print(sheet.Name)
}

データ

Strings()で表全部のデータが[][]stringで取得できる。intのフィールドも全部stringで返ってくるので、strconv.Atoi等で適当に変換する。

cells := sheet.Strings()
3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1