Skip to content
This repository has been archived by the owner on Jun 24, 2024. It is now read-only.

Commit

Permalink
go-ole
Browse files Browse the repository at this point in the history
  • Loading branch information
dt committed Oct 26, 2017
1 parent 3781d4f commit b1dd557
Show file tree
Hide file tree
Showing 18 changed files with 598 additions and 75 deletions.
21 changes: 21 additions & 0 deletions github.com/go-ole/go-ole/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright © 2013-2017 Yasuhiro Matsumoto, <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the “Software”), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
26 changes: 3 additions & 23 deletions github.com/go-ole/go-ole/appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
# - All section names are case-sensitive.
# - Section names should be unique on each level.

version: "1.2.0.{build}-alpha-{branch}"
version: "1.3.0.{build}-alpha-{branch}"

os: Windows Server 2012 R2

branches:
only:
- master
- v1.2
- v1.1
- v1.0

Expand All @@ -22,21 +23,9 @@ environment:
GOPATH: c:\gopath
matrix:
- GOARCH: amd64
GOVERSION: 1.4
GOVERSION: 1.5
GOROOT: c:\go
DOWNLOADPLATFORM: "x64"
- GOARCH: 386
GOVERSION: 1.4
GOROOT: c:\go
DOWNLOADPLATFORM: "x86"

matrix:
fast_finish: true
allow_failures:
- GOARCH: 386
GOVERSION: 1.4
GOROOT: c:\go
DOWNLOADPLATFORM: "x86"

install:
- choco install mingw
Expand All @@ -48,15 +37,6 @@ install:
# - set
- go version
- go env
- c:\gopath\src\github.com\go-ole\go-ole\build\compile-go.bat
- go tool dist install -v cmd/8a
- go tool dist install -v cmd/8c
- go tool dist install -v cmd/8g
- go tool dist install -v cmd/8l
- go tool dist install -v cmd/6a
- go tool dist install -v cmd/6c
- go tool dist install -v cmd/6g
- go tool dist install -v cmd/6l
- go get -u golang.org/x/tools/cmd/cover
- go get -u golang.org/x/tools/cmd/godoc
- go get -u golang.org/x/tools/cmd/stringer
Expand Down
5 changes: 0 additions & 5 deletions github.com/go-ole/go-ole/build/compile-go.bat

This file was deleted.

4 changes: 2 additions & 2 deletions github.com/go-ole/go-ole/com.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,9 +321,9 @@ func DispatchMessage(msg *Msg) (ret int32) {
// GetVariantDate converts COM Variant Time value to Go time.Time.
func GetVariantDate(value float64) (time.Time, error) {
var st syscall.Systemtime
r, _, _ := procVariantTimeToSystemTime.Call(uintptr(unsafe.Pointer(&value)), uintptr(unsafe.Pointer(&st)))
r, _, _ := procVariantTimeToSystemTime.Call(uintptr(value), uintptr(unsafe.Pointer(&st)))
if r != 0 {
return time.Date(int(st.Year), time.Month(st.Month), int(st.Day), int(st.Hour), int(st.Minute), int(st.Second), int(st.Milliseconds/1000), nil), nil
return time.Date(int(st.Year), time.Month(st.Month), int(st.Day), int(st.Hour), int(st.Minute), int(st.Second), int(st.Milliseconds/1000), time.UTC), nil
}
return time.Now(), errors.New("Could not convert to time, passing current time.")
}
162 changes: 162 additions & 0 deletions github.com/go-ole/go-ole/example/libreoffice/libreoffice.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
// +build windows

/*
Demonstrates basic LibreOffce (OpenOffice) automation with OLE using GO-OLE.
Usage: cd [...]\go-ole\example\libreoffice
go run libreoffice.go
References:
http://www.openoffice.org/api/basic/man/tutorial/tutorial.pdf
http://api.libreoffice.org/examples/examples.html#OLE_examples
https://wiki.openoffice.org/wiki/Documentation/BASIC_Guide
Tested environment:
go 1.6.2 (windows/amd64)
LibreOffice 5.1.0.3 (32 bit)
Windows 10 (64 bit)
The MIT License (MIT)
Copyright (c) 2016 Sebastian Schleemilch <https://github.com/itschleemilch>.
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so, subject to the
following conditions:
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
OR OTHER DEALINGS IN THE SOFTWARE.
*/

package main

import (
"fmt"
"log"

ole "github.com/go-ole/go-ole"
"github.com/go-ole/go-ole/oleutil"
)

func checkError(err error, msg string) {
if err != nil {
log.Fatal(msg)
}
}

// LOGetCell returns an handle to a cell within a worksheet
// LibreOffice Basic: GetCell = oSheet.getCellByPosition (nColumn , nRow)
func LOGetCell(worksheet *ole.IDispatch, nColumn int, nRow int) (cell *ole.IDispatch) {
return oleutil.MustCallMethod(worksheet, "getCellByPosition", nColumn, nRow).ToIDispatch()
}

// LOGetCellRangeByName returns a named range (e.g. "A1:B4")
func LOGetCellRangeByName(worksheet *ole.IDispatch, rangeName string) (cells *ole.IDispatch) {
return oleutil.MustCallMethod(worksheet, "getCellRangeByName", rangeName).ToIDispatch()
}

// LOGetCellString returns the displayed value
func LOGetCellString(cell *ole.IDispatch) (value string) {
return oleutil.MustGetProperty(cell, "string").ToString()
}

// LOGetCellValue returns the cell's internal value (not formatted, dummy code, FIXME)
func LOGetCellValue(cell *ole.IDispatch) (value string) {
val := oleutil.MustGetProperty(cell, "value")
fmt.Printf("Cell: %+v\n", val)
return val.ToString()
}

// LOGetCellError returns the error value of a cell (dummy code, FIXME)
func LOGetCellError(cell *ole.IDispatch) (result *ole.VARIANT) {
return oleutil.MustGetProperty(cell, "error")
}

// LOSetCellString sets the text value of a cell
func LOSetCellString(cell *ole.IDispatch, text string) {
oleutil.MustPutProperty(cell, "string", text)
}

// LOSetCellValue sets the numeric value of a cell
func LOSetCellValue(cell *ole.IDispatch, value float64) {
oleutil.MustPutProperty(cell, "value", value)
}

// LOSetCellFormula sets the formula (in englisch language)
func LOSetCellFormula(cell *ole.IDispatch, formula string) {
oleutil.MustPutProperty(cell, "formula", formula)
}

// LOSetCellFormulaLocal sets the formula in the user's language (e.g. German =SUMME instead of =SUM)
func LOSetCellFormulaLocal(cell *ole.IDispatch, formula string) {
oleutil.MustPutProperty(cell, "FormulaLocal", formula)
}

// LONewSpreadsheet creates a new spreadsheet in a new window and returns a document handle.
func LONewSpreadsheet(desktop *ole.IDispatch) (document *ole.IDispatch) {
var args = []string{}
document = oleutil.MustCallMethod(desktop,
"loadComponentFromURL", "private:factory/scalc", // alternative: private:factory/swriter
"_blank", 0, args).ToIDispatch()
return
}

// LOOpenFile opens a file (text, spreadsheet, ...) in a new window and returns a document
// handle. Example: /home/testuser/spreadsheet.ods
func LOOpenFile(desktop *ole.IDispatch, fullpath string) (document *ole.IDispatch) {
var args = []string{}
document = oleutil.MustCallMethod(desktop,
"loadComponentFromURL", "file://"+fullpath,
"_blank", 0, args).ToIDispatch()
return
}

// LOSaveFile saves the current document.
// Only works if a file already exists,
// see https://wiki.openoffice.org/wiki/Saving_a_document
func LOSaveFile(document *ole.IDispatch) {
// use storeAsURL if neccessary with third URL parameter
oleutil.MustCallMethod(document, "store")
}

// LOGetWorksheet returns a worksheet (index starts at 0)
func LOGetWorksheet(document *ole.IDispatch, index int) (worksheet *ole.IDispatch) {
sheets := oleutil.MustGetProperty(document, "Sheets").ToIDispatch()
worksheet = oleutil.MustCallMethod(sheets, "getByIndex", index).ToIDispatch()
return
}

// This example creates a new spreadsheet, reads and modifies cell values and style.
func main() {
ole.CoInitialize(0)
unknown, errCreate := oleutil.CreateObject("com.sun.star.ServiceManager")
checkError(errCreate, "Couldn't create a OLE connection to LibreOffice")
ServiceManager, errSM := unknown.QueryInterface(ole.IID_IDispatch)
checkError(errSM, "Couldn't start a LibreOffice instance")
desktop := oleutil.MustCallMethod(ServiceManager,
"createInstance", "com.sun.star.frame.Desktop").ToIDispatch()

document := LONewSpreadsheet(desktop)
sheet0 := LOGetWorksheet(document, 0)

cell1_1 := LOGetCell(sheet0, 1, 1) // cell B2
cell1_2 := LOGetCell(sheet0, 1, 2) // cell B3
cell1_3 := LOGetCell(sheet0, 1, 3) // cell B4
cell1_4 := LOGetCell(sheet0, 1, 4) // cell B5
LOSetCellString(cell1_1, "Hello World")
LOSetCellValue(cell1_2, 33.45)
LOSetCellFormula(cell1_3, "=B3+5")
b4Value := LOGetCellString(cell1_3)
LOSetCellString(cell1_4, b4Value)
// set background color yellow:
oleutil.MustPutProperty(cell1_1, "cellbackcolor", 0xFFFF00)

fmt.Printf("Press [ENTER] to exit")
fmt.Scanf("%s")
ServiceManager.Release()
ole.CoUninitialize()
}
Loading

0 comments on commit b1dd557

Please sign in to comment.