LoginSignup
4
3

More than 5 years have passed since last update.

Go言語で Excel へのスクショ貼りを自動化しよう!

Last updated at Posted at 2018-09-01

Go で Excel ファイルを作成するのに使えるライブラリとしては

  • go-ole/go-ole
    • Excel自身をOLE経由で動かすので互換性の懸念はない。xlsx だけでなく、xls でもいける。だけど 遅い。要Excel。結構めんどうくさい(ミスるとExcelプロセスがゾンビになって残る)
  • tealeg/xlsx
    • 定番。読み取り・新規作成はバッチリだが、既存のxlsxファイルを加工する際に画像などが飛んでしまうと言われている。また、メモリ消費量が多い
  • loadoff/excl
    • xlsx と違って、既存Excelファイルへの加工に心配がない。メモリ消費量が抑えられている。開発者日本人。Qiita に紹介記事あり

などが知られているが、画像などが貼れるものということで、今回、tealeg/xlsxのトップ画面 で「You should probably also checkout」とか言って紹介されていた 360EntSecGroup-Skylar/excelizeを使ってみた。

shot.go
package main

import (
    "fmt"
    "os"
    "path/filepath"

    _ "image/gif"
    _ "image/jpeg"
    _ "image/png"

    "github.com/360EntSecGroup-Skylar/excelize"
)

func mains() error {
    xlsx := excelize.NewFile()

    for _, arg := range os.Args[1:] {
        name := filepath.Base(arg)
        xlsx.NewSheet(name)
        if err := xlsx.AddPicture(name, "A1", arg, ""); err != nil {
            return err
        }
    }
    xlsx.DeleteSheet("Sheet1")

    return xlsx.SaveAs("./Book1.xlsx")
}

func main() {
    if err := mains(); err != nil {
        fmt.Fprintln(os.Stderr, err)
        os.Exit(1)
    }
}

go run shot.go ./2018-08-19_01.15.57.pngとかすると、2018-08-19_01.15.57.png というシートに 2018-08-19_01.15.57.png が貼り付けられた Book1.xlsx というファイルが生成される。

なお、以下の import が抜けると、画像が貼り付けが行われない上に、それがエラーとして通知もされないので注意。

    _ "image/gif"
    _ "image/jpeg"
    _ "image/png"

以上

4
3
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
4
3