Giter Club home page Giter Club logo

gord's Introduction

gord

Gord is a library to operate MS Word using go-ole library. Thanks go-ole package!

This library works only on Windows.

Install

go get github.com/devlights/gord@latest

Usages

package main

import (
	"fmt"
	"os"
	"time"

	"github.com/devlights/gord"
)

type (
	filePath = string
)

// main is entry point of this app.
func main() {
	if len(os.Args) < 2 {
		_, _ = fmt.Fprintf(os.Stderr, "need file-path")
		os.Exit(1)
	}

	if err := run(os.Args[len(os.Args)-1]); err != nil {
		panic(err)
	}
}

func run(p filePath) error {
	// 0. Initialize Gord
	quitFn := gord.MustInitGord()
	defer quitFn()

	// 1. Create new Gord instance.
	g, release := gord.MustNewGord()

	// must call gord release function when function exited
	// otherwise WORD process was remained.
	defer release()

	// optional settings
	g.MustSetVisible(true)

	// 2. Get Documents instance.
	docs, err := g.Documents()
	if err != nil {
		return err
	}

	// 3. Open document
	doc, docReleaseFn, err := docs.Open(p)
	if err != nil {
		return err
	}

	// call document's release function
	defer docReleaseFn()

	// simulate something processing..
	time.Sleep(5 * time.Second)

	// 4. Close document
	if err := doc.Close(); err != nil {
		return err
	}

	// Document::SetSaved(true) and Document::Close() is automatically called when `defer docReleaseFn()`.
	// Word::Quit() and Word::Release() is automatically called when `defer release()`.

	return nil
}

Also look at the "examples" directory :)

REFERENCES

See also

  • Goxcel
    • Goxcel is a library to operate Excel using go-ole library.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.