github.com/remoteit/go-ole@v1.2.7/_example/libreoffice/libreoffice.go (about)

     1  // +build windows
     2  
     3  /*
     4  	Demonstrates basic LibreOffce (OpenOffice) automation with OLE using GO-OLE.
     5  	Usage: 	cd [...]\go-ole\example\libreoffice
     6  			go run libreoffice.go
     7  	References:
     8  			http://www.openoffice.org/api/basic/man/tutorial/tutorial.pdf
     9  			http://api.libreoffice.org/examples/examples.html#OLE_examples
    10  			https://wiki.openoffice.org/wiki/Documentation/BASIC_Guide
    11  
    12  	Tested environment:
    13  			go 1.6.2 (windows/amd64)
    14  			LibreOffice 5.1.0.3 (32 bit)
    15  			Windows 10 (64 bit)
    16  
    17  	The MIT License (MIT)
    18  	Copyright (c) 2016 Sebastian Schleemilch <https://github.com/itschleemilch>.
    19  
    20  	Permission is hereby granted, free of charge, to any person obtaining a copy of
    21  	this software and associated documentation files (the "Software"), to deal in
    22  	the Software without restriction, including without limitation the rights to use,
    23  	copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
    24  	and to permit persons to whom the Software is furnished to do so, subject to the
    25  	following conditions:
    26  
    27  	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
    28  	INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
    29  	PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
    30  	LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
    31  	TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
    32  	OR OTHER DEALINGS IN THE SOFTWARE.
    33  */
    34  
    35  package main
    36  
    37  import (
    38  	"fmt"
    39  	"log"
    40  
    41  	ole "github.com/remoteit/go-ole"
    42  	"github.com/remoteit/go-ole/oleutil"
    43  )
    44  
    45  func checkError(err error, msg string) {
    46  	if err != nil {
    47  		log.Fatal(msg)
    48  	}
    49  }
    50  
    51  // LOGetCell returns an handle to a cell within a worksheet
    52  // LibreOffice Basic: GetCell = oSheet.getCellByPosition (nColumn , nRow)
    53  func LOGetCell(worksheet *ole.IDispatch, nColumn int, nRow int) (cell *ole.IDispatch) {
    54  	return oleutil.MustCallMethod(worksheet, "getCellByPosition", nColumn, nRow).ToIDispatch()
    55  }
    56  
    57  // LOGetCellRangeByName returns a named range (e.g. "A1:B4")
    58  func LOGetCellRangeByName(worksheet *ole.IDispatch, rangeName string) (cells *ole.IDispatch) {
    59  	return oleutil.MustCallMethod(worksheet, "getCellRangeByName", rangeName).ToIDispatch()
    60  }
    61  
    62  // LOGetCellString returns the displayed value
    63  func LOGetCellString(cell *ole.IDispatch) (value string) {
    64  	return oleutil.MustGetProperty(cell, "string").ToString()
    65  }
    66  
    67  // LOGetCellValue returns the cell's internal value (not formatted, dummy code, FIXME)
    68  func LOGetCellValue(cell *ole.IDispatch) (value string) {
    69  	val := oleutil.MustGetProperty(cell, "value")
    70  	fmt.Printf("Cell: %+v\n", val)
    71  	return val.ToString()
    72  }
    73  
    74  // LOGetCellError returns the error value of a cell (dummy code, FIXME)
    75  func LOGetCellError(cell *ole.IDispatch) (result *ole.VARIANT) {
    76  	return oleutil.MustGetProperty(cell, "error")
    77  }
    78  
    79  // LOSetCellString sets the text value of a cell
    80  func LOSetCellString(cell *ole.IDispatch, text string) {
    81  	oleutil.MustPutProperty(cell, "string", text)
    82  }
    83  
    84  // LOSetCellValue sets the numeric value of a cell
    85  func LOSetCellValue(cell *ole.IDispatch, value float64) {
    86  	oleutil.MustPutProperty(cell, "value", value)
    87  }
    88  
    89  // LOSetCellFormula sets the formula (in englisch language)
    90  func LOSetCellFormula(cell *ole.IDispatch, formula string) {
    91  	oleutil.MustPutProperty(cell, "formula", formula)
    92  }
    93  
    94  // LOSetCellFormulaLocal sets the formula in the user's language (e.g. German =SUMME instead of =SUM)
    95  func LOSetCellFormulaLocal(cell *ole.IDispatch, formula string) {
    96  	oleutil.MustPutProperty(cell, "FormulaLocal", formula)
    97  }
    98  
    99  // LONewSpreadsheet creates a new spreadsheet in a new window and returns a document handle.
   100  func LONewSpreadsheet(desktop *ole.IDispatch) (document *ole.IDispatch) {
   101  	var args = []string{}
   102  	document = oleutil.MustCallMethod(desktop,
   103  		"loadComponentFromURL", "private:factory/scalc", // alternative: private:factory/swriter
   104  		"_blank", 0, args).ToIDispatch()
   105  	return
   106  }
   107  
   108  // LOOpenFile opens a file (text, spreadsheet, ...) in a new window and returns a document
   109  // handle. Example: /home/testuser/spreadsheet.ods
   110  func LOOpenFile(desktop *ole.IDispatch, fullpath string) (document *ole.IDispatch) {
   111  	var args = []string{}
   112  	document = oleutil.MustCallMethod(desktop,
   113  		"loadComponentFromURL", "file://"+fullpath,
   114  		"_blank", 0, args).ToIDispatch()
   115  	return
   116  }
   117  
   118  // LOSaveFile saves the current document.
   119  // Only works if a file already exists,
   120  // see https://wiki.openoffice.org/wiki/Saving_a_document
   121  func LOSaveFile(document *ole.IDispatch) {
   122  	// use storeAsURL if neccessary with third URL parameter
   123  	oleutil.MustCallMethod(document, "store")
   124  }
   125  
   126  // LOGetWorksheet returns a worksheet (index starts at 0)
   127  func LOGetWorksheet(document *ole.IDispatch, index int) (worksheet *ole.IDispatch) {
   128  	sheets := oleutil.MustGetProperty(document, "Sheets").ToIDispatch()
   129  	worksheet = oleutil.MustCallMethod(sheets, "getByIndex", index).ToIDispatch()
   130  	return
   131  }
   132  
   133  // This example creates a new spreadsheet, reads and modifies cell values and style.
   134  func main() {
   135  	ole.CoInitialize(0)
   136  	unknown, errCreate := oleutil.CreateObject("com.sun.star.ServiceManager")
   137  	checkError(errCreate, "Couldn't create a OLE connection to LibreOffice")
   138  	ServiceManager, errSM := unknown.QueryInterface(ole.IID_IDispatch)
   139  	checkError(errSM, "Couldn't start a LibreOffice instance")
   140  	desktop := oleutil.MustCallMethod(ServiceManager,
   141  		"createInstance", "com.sun.star.frame.Desktop").ToIDispatch()
   142  
   143  	document := LONewSpreadsheet(desktop)
   144  	sheet0 := LOGetWorksheet(document, 0)
   145  
   146  	cell1_1 := LOGetCell(sheet0, 1, 1) // cell B2
   147  	cell1_2 := LOGetCell(sheet0, 1, 2) // cell B3
   148  	cell1_3 := LOGetCell(sheet0, 1, 3) // cell B4
   149  	cell1_4 := LOGetCell(sheet0, 1, 4) // cell B5
   150  	LOSetCellString(cell1_1, "Hello World")
   151  	LOSetCellValue(cell1_2, 33.45)
   152  	LOSetCellFormula(cell1_3, "=B3+5")
   153  	b4Value := LOGetCellString(cell1_3)
   154  	LOSetCellString(cell1_4, b4Value)
   155  	// set background color yellow:
   156  	oleutil.MustPutProperty(cell1_1, "cellbackcolor", 0xFFFF00)
   157  
   158  	fmt.Printf("Press [ENTER] to exit")
   159  	fmt.Scanf("%s")
   160  	ServiceManager.Release()
   161  	ole.CoUninitialize()
   162  }