示例#1
0
文件: excel.go 项目: gitdlam/excel
//get Property as interface.
func GetProperty(idisp *ole.IDispatch, args ...string) (ret interface{}, err error) {
	defer Except("GetProperty", &err)
	argnum := len(args)
	if argnum == 0 {
		ret = VARIANT{oleutil.MustGetProperty(idisp, "Value")}.Value()
	} else {
		maxi := argnum - 1
		for i := 0; i < maxi && err == nil; i++ {
			idisp = oleutil.MustGetProperty(idisp, args[i]).ToIDispatch()
			defer DoFuncs(idisp.Release)
		}
		//get multi-Property
		argv := args[maxi]
		if strings.IndexAny(argv, ",") != -1 {
			sl := []string{}
			for _, key := range strings.Split(argv, ",") {
				sl = append(sl, key+":"+String(VARIANT{oleutil.MustGetProperty(idisp, key)}.Value()))
			}
			ret = strings.Join(sl, ", ")
		} else {
			ret = VARIANT{oleutil.MustGetProperty(idisp, argv)}.Value()
		}
	}
	return
}
示例#2
0
文件: excel.go 项目: gitdlam/excel
//put Property.
func PutProperty(idisp *ole.IDispatch, args ...interface{}) (err error) {
	defer Except("PutProperty", &err)
	argnum := len(args)
	if argnum == 1 {
		oleutil.MustPutProperty(idisp, "Value", args[0])
	} else if argnum > 1 {
		maxi := argnum - 2
		for i := 0; i < maxi && err == nil; i++ {
			idisp = oleutil.MustGetProperty(idisp, args[i].(string)).ToIDispatch()
			defer DoFuncs(idisp.Release)
		}
		//put multi-Property
		if argv, ok := args[argnum-1].(map[string]interface{}); ok {
			idisp = oleutil.MustGetProperty(idisp, args[maxi].(string)).ToIDispatch()
			defer DoFuncs(idisp.Release)
			for key, val := range argv {
				oleutil.MustPutProperty(idisp, key, val)
			}
		} else {
			oleutil.MustPutProperty(idisp, args[maxi].(string), args[argnum-1])
		}
	} else {
		err = errors.New("args is empty")
	}
	return
}
示例#3
0
文件: excel.go 项目: dlintw/go-ole
func writeExample(excel, workbooks *ole.IDispatch, filepath string) {

	// ref: https://msdn.microsoft.com/zh-tw/library/office/ff198017.aspx
	// http://stackoverflow.com/questions/12159513/what-is-the-correct-xlfileformat-enumeration-for-excel-97-2003
	const xlExcel8 = 56
	workbook := oleutil.MustCallMethod(workbooks, "Add", nil).ToIDispatch()
	defer workbook.Release()
	worksheet := oleutil.MustGetProperty(workbook, "Worksheets", 1).ToIDispatch()
	defer worksheet.Release()
	cell := oleutil.MustGetProperty(worksheet, "Cells", 1, 1).ToIDispatch()
	oleutil.PutProperty(cell, "Value", 12345)
	cell.Release()
	activeWorkBook := oleutil.MustGetProperty(excel, "ActiveWorkBook").ToIDispatch()
	defer activeWorkBook.Release()

	os.Remove(filepath)
	// ref: https://msdn.microsoft.com/zh-tw/library/microsoft.office.tools.excel.workbook.saveas.aspx
	oleutil.MustCallMethod(activeWorkBook, "SaveAs", filepath, xlExcel8, nil, nil).ToIDispatch()

	//time.Sleep(2 * time.Second)

	// let excel could close without asking
	// oleutil.PutProperty(workbook, "Saved", true)
	// oleutil.CallMethod(workbook, "Close", false)
}
示例#4
0
文件: excel.go 项目: dlintw/go-ole
func readExample(fileName string, excel, workbooks *ole.IDispatch) {
	workbook, err := oleutil.CallMethod(workbooks, "Open", fileName)

	if err != nil {
		log.Fatalln(err)
	}
	defer workbook.ToIDispatch().Release()

	sheets := oleutil.MustGetProperty(excel, "Sheets").ToIDispatch()
	sheetCount := (int)(oleutil.MustGetProperty(sheets, "Count").Val)
	fmt.Println("sheet count=", sheetCount)
	sheets.Release()

	worksheet := oleutil.MustGetProperty(workbook.ToIDispatch(), "Worksheets", 1).ToIDispatch()
	defer worksheet.Release()
	for row := 1; row <= 2; row++ {
		for col := 1; col <= 5; col++ {
			cell := oleutil.MustGetProperty(worksheet, "Cells", row, col).ToIDispatch()
			val, err := oleutil.GetProperty(cell, "Value")
			if err != nil {
				break
			}
			fmt.Printf("(%d,%d)=%+v toString=%s\n", col, row, val.Value(), val.ToString())
			cell.Release()
		}
	}
}
示例#5
0
文件: excel.go 项目: gitdlam/excel
func (mso *MSO) Pick(workx string, id interface{}) (ret *ole.IDispatch, err error) {
	defer Except("Pick", &err)
	if id_int, ok := id.(int); ok {
		ret = oleutil.MustGetProperty(mso.IdExcel, workx, id_int).ToIDispatch()
	} else if id_str, ok := id.(string); ok {
		ret = oleutil.MustGetProperty(mso.IdExcel, workx, id_str).ToIDispatch()
	} else {
		err = errors.New("sheet id incorrect")
	}
	return
}
示例#6
0
文件: excel.go 项目: gitdlam/excel
func (mso *MSO) WorkBooks() (wbs WorkBooks) {
	num := mso.CountWorkBooks()
	for i := 1; i <= num; i++ {
		wbs = append(wbs, WorkBook{oleutil.MustGetProperty(mso.IdExcel, "WorkBooks", i).ToIDispatch(), mso})
	}
	return
}
示例#7
0
文件: excel.go 项目: gitdlam/excel
func (mso *MSO) Sheets() (sheets []Sheet) {
	num := mso.CountSheets()
	for i := 1; i <= num; i++ {
		sheet := Sheet{oleutil.MustGetProperty(mso.IdExcel, "WorkSheets", i).ToIDispatch()}
		sheets = append(sheets, sheet)
	}
	return
}
示例#8
0
func main() {
	ole.CoInitialize(0)
	unknown, err := oleutil.CreateObject("WMPlayer.OCX")
	if err != nil {
		log.Fatal(err)
	}
	wmp := unknown.MustQueryInterface(ole.IID_IDispatch)
	collection := oleutil.MustGetProperty(wmp, "MediaCollection").ToIDispatch()
	list := oleutil.MustCallMethod(collection, "getAll").ToIDispatch()
	count := int(oleutil.MustGetProperty(list, "count").Val)
	for i := 0; i < count; i++ {
		item := oleutil.MustGetProperty(list, "item", i).ToIDispatch()
		name := oleutil.MustGetProperty(item, "name").ToString()
		sourceURL := oleutil.MustGetProperty(item, "sourceURL").ToString()
		fmt.Println(name, sourceURL)
	}
}
示例#9
0
文件: excel.go 项目: gitdlam/excel
func Initialize(opt ...Option) (mso *MSO) {
	ole.CoInitialize(0)
	app, _ := oleutil.CreateObject("Excel.Application")
	excel, _ := app.QueryInterface(ole.IID_IDispatch)
	wbs := oleutil.MustGetProperty(excel, "WorkBooks").ToIDispatch()
	ver, _ := strconv.ParseFloat(oleutil.MustGetProperty(excel, "Version").ToString(), 64)

	if len(opt) == 0 {
		opt = []Option{{"Visible": true, "DisplayAlerts": true, "ScreenUpdating": true}}
	}
	mso = &MSO{Option: opt[0], IuApp: app, IdExcel: excel, IdWorkBooks: wbs, Version: ver}
	mso.SetOption(1)

	//XlFileFormat Enumeration: http://msdn.microsoft.com/en-us/library/office/ff198017%28v=office.15%29.aspx
	mso.FILEFORMAT = map[string]int{"txt": -4158, "csv": 6, "html": 44}
	return
}
示例#10
0
文件: excel.go 项目: gitdlam/excel
func (sheet Sheet) Name(args ...string) (name string) {
	defer Except("", nil)
	if len(args) == 0 {
		name = oleutil.MustGetProperty(sheet.Idisp, "Name").ToString()
	} else {
		name = args[0]
		oleutil.MustPutProperty(sheet.Idisp, "Name", name)
	}
	return
}
示例#11
0
文件: excel.go 项目: gitdlam/excel
func (mso *MSO) AddSheet(wb *ole.IDispatch, args ...string) (Sheet, error) {
	sheets := oleutil.MustGetProperty(wb, "Sheets").ToIDispatch()
	defer sheets.Release()
	_sheet, err := oleutil.CallMethod(sheets, "Add")
	sheet := Sheet{_sheet.ToIDispatch()}
	if args != nil {
		sheet.Name(args...)
	}
	sheet.Select()
	return sheet, err
}
示例#12
0
文件: excel.go 项目: ChongFeng/beats
func main() {
	ole.CoInitialize(0)
	unknown, _ := oleutil.CreateObject("Excel.Application")
	excel, _ := unknown.QueryInterface(ole.IID_IDispatch)
	oleutil.PutProperty(excel, "Visible", true)
	workbooks := oleutil.MustGetProperty(excel, "Workbooks").ToIDispatch()
	workbook := oleutil.MustCallMethod(workbooks, "Add", nil).ToIDispatch()
	worksheet := oleutil.MustGetProperty(workbook, "Worksheets", 1).ToIDispatch()
	cell := oleutil.MustGetProperty(worksheet, "Cells", 1, 1).ToIDispatch()
	oleutil.PutProperty(cell, "Value", 12345)

	time.Sleep(2000000000)

	oleutil.PutProperty(workbook, "Saved", true)
	oleutil.CallMethod(workbook, "Close", false)
	oleutil.CallMethod(excel, "Quit")
	excel.Release()

	ole.CoUninitialize()
}
示例#13
0
func main() {
	ole.CoInitialize(0)
	unknown, _ := oleutil.CreateObject("Agent.Control.1")
	agent, _ := unknown.QueryInterface(ole.IID_IDispatch)
	oleutil.PutProperty(agent, "Connected", true)
	characters := oleutil.MustGetProperty(agent, "Characters").ToIDispatch()
	oleutil.CallMethod(characters, "Load", "Merlin", "c:\\windows\\msagent\\chars\\Merlin.acs")
	character := oleutil.MustCallMethod(characters, "Character", "Merlin").ToIDispatch()
	oleutil.CallMethod(character, "Show")
	oleutil.CallMethod(character, "Speak", "こんにちわ世界")

	time.Sleep(4000000000)
}
示例#14
0
文件: ie.go 项目: ChongFeng/beats
func main() {
	ole.CoInitialize(0)
	unknown, _ := oleutil.CreateObject("InternetExplorer.Application")
	ie, _ := unknown.QueryInterface(ole.IID_IDispatch)
	oleutil.CallMethod(ie, "Navigate", "http://www.google.com")
	oleutil.PutProperty(ie, "Visible", true)
	for {
		if oleutil.MustGetProperty(ie, "Busy").Val == 0 {
			break
		}
	}

	time.Sleep(1e9)

	document := oleutil.MustGetProperty(ie, "document").ToIDispatch()
	window := oleutil.MustGetProperty(document, "parentWindow").ToIDispatch()
	// set 'golang' to text box.
	oleutil.MustCallMethod(window, "eval", "document.getElementsByName('q')[0].value = 'golang'")
	// click btnG.
	btnG := oleutil.MustCallMethod(window, "eval", "document.getElementsByName('btnG')[0]").ToIDispatch()
	oleutil.MustCallMethod(btnG, "click")
}
示例#15
0
func main() {
	cwd, _ := os.Getwd()
	artworkjpg := filepath.Join(cwd, "artwork.jpg")

	gc := gntp.NewClient()
	gc.AppName = "nowplaying"
	gc.Register([]gntp.Notification{{"default", "", true}})

	ole.CoInitialize(0)
	unk, err := oleutil.CreateObject("iTunes.Application")
	if err != nil {
		log.Fatal(err)
	}
	unk.AddRef()
	dsp := unk.MustQueryInterface(ole.IID_IDispatch)

	prev := ""
	for {
		func() {
			defer func() {
				recover()
			}()
			track := oleutil.MustGetProperty(dsp, "CurrentTrack").ToIDispatch()
			if track != nil {
				name := oleutil.MustGetProperty(track, "Name").ToString()
				artist := oleutil.MustGetProperty(track, "artist").ToString()
				album := oleutil.MustGetProperty(track, "Album").ToString()
				curr := fmt.Sprintf("%s/%s\n%s", name, artist, album)
				if curr != prev {
					prev = curr
					artwork := oleutil.MustGetProperty(track, "Artwork").ToIDispatch()
					item := oleutil.MustGetProperty(artwork, "Item", 1).ToIDispatch()
					icon := ""
					if item != nil {
						_, err = oleutil.CallMethod(item, "SaveArtworkToFile", artworkjpg)
						if err != nil {
							log.Print(err)
						} else {
							icon = artworkjpg
						}
					}
					log.Print(curr)
					err = gc.Notify(&gntp.Message{
						Event: "default",
						Title: "iTunes",
						Text:  curr,
						Icon:  icon,
					})
					if err != nil {
						log.Print(err)
					}
				}
			}
		}()
		time.Sleep(3 * time.Second)
	}
}
示例#16
0
文件: excel.go 项目: dlintw/go-ole
func main() {
	log.SetFlags(log.Flags() | log.Lshortfile)
	ole.CoInitialize(0)
	unknown, _ := oleutil.CreateObject("Excel.Application")
	excel, _ := unknown.QueryInterface(ole.IID_IDispatch)
	oleutil.PutProperty(excel, "Visible", true)

	workbooks := oleutil.MustGetProperty(excel, "Workbooks").ToIDispatch()
	cwd, _ := os.Getwd()
	writeExample(excel, workbooks, cwd+"\\write.xls")
	readExample(cwd+"\\excel97-2003.xls", excel, workbooks)
	showMethodsAndProperties(workbooks)
	workbooks.Release()
	// oleutil.CallMethod(excel, "Quit")
	excel.Release()
	ole.CoUninitialize()
}
示例#17
0
func main() {
	ole.CoInitialize(0)
	unknown, _ := oleutil.CreateObject("Outlook.Application")
	outlook, _ := unknown.QueryInterface(ole.IID_IDispatch)
	ns := oleutil.MustCallMethod(outlook, "GetNamespace", "MAPI").ToIDispatch()
	folder := oleutil.MustCallMethod(ns, "GetDefaultFolder", 10).ToIDispatch()
	contacts := oleutil.MustCallMethod(folder, "Items").ToIDispatch()
	count := oleutil.MustGetProperty(contacts, "Count").Value().(int32)
	for i := 1; i <= int(count); i++ {
		item, err := oleutil.GetProperty(contacts, "Item", i)
		if err == nil && item.VT == ole.VT_DISPATCH {
			if value, err := oleutil.GetProperty(item.ToIDispatch(), "FullName"); err == nil {
				fmt.Println(value.Value())
			}
		}
	}
	oleutil.MustCallMethod(outlook, "Quit")
}
示例#18
0
func main() {
	ole.CoInitialize(0)
	unknown, _ := oleutil.CreateObject("Microsoft.XMLHTTP")
	xmlhttp, _ := unknown.QueryInterface(ole.IID_IDispatch)
	_, err := oleutil.CallMethod(xmlhttp, "open", "GET", "http://rss.slashdot.org/Slashdot/slashdot", false)
	if err != nil {
		panic(err.Error())
	}
	_, err = oleutil.CallMethod(xmlhttp, "send", nil)
	if err != nil {
		panic(err.Error())
	}
	state := -1
	for state != 4 {
		state = int(oleutil.MustGetProperty(xmlhttp, "readyState").Val)
		time.Sleep(10000000)
	}
	responseXml := oleutil.MustGetProperty(xmlhttp, "responseXml").ToIDispatch()
	items := oleutil.MustCallMethod(responseXml, "selectNodes", "/rss/channel/item").ToIDispatch()
	length := int(oleutil.MustGetProperty(items, "length").Val)

	for n := 0; n < length; n++ {
		item := oleutil.MustGetProperty(items, "item", n).ToIDispatch()

		title := oleutil.MustCallMethod(item, "selectSingleNode", "title").ToIDispatch()
		fmt.Println(oleutil.MustGetProperty(title, "text").ToString())

		link := oleutil.MustCallMethod(item, "selectSingleNode", "link").ToIDispatch()
		fmt.Println("  " + oleutil.MustGetProperty(link, "text").ToString())

		title.Release()
		link.Release()
		item.Release()
	}
	items.Release()
	xmlhttp.Release()
}
示例#19
0
文件: excel.go 项目: gitdlam/excel
//Must get cell pointer.
func (sheet Sheet) MustCell(r int, c int) (cell Cell) {
	cell = Cell{oleutil.MustGetProperty(sheet.Idisp, "Cells", r, c).ToIDispatch()}
	return
}
示例#20
0
文件: excel.go 项目: gitdlam/excel
func (mso *MSO) CountWorkBooks() int {
	return (int)(oleutil.MustGetProperty(mso.IdWorkBooks, "Count").Val)
}
示例#21
0
// 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()
}
示例#22
0
文件: excel.go 项目: gitdlam/excel
func (mso *MSO) CountSheets() int {
	sheets := oleutil.MustGetProperty(mso.IdExcel, "Sheets").ToIDispatch()
	return (int)(oleutil.MustGetProperty(sheets, "Count").Val)
}
示例#23
0
文件: excel.go 项目: gitdlam/excel
func (wb WorkBook) Name() string {
	defer Except("", nil)
	return oleutil.MustGetProperty(wb.Idisp, "Name").ToString()
}
示例#24
0
// 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
}
示例#25
0
// LOGetCellString returns the displayed value
func LOGetCellString(cell *ole.IDispatch) (value string) {
	return oleutil.MustGetProperty(cell, "string").ToString()
}
示例#26
0
文件: excel.go 项目: gitdlam/excel
//get range pointer.
func (sheet Sheet) Range(rang string) Range {
	return Range{oleutil.MustGetProperty(sheet.Idisp, "Range", rang).ToIDispatch()}
}
示例#27
0
// LOGetCellError returns the error value of a cell (dummy code, FIXME)
func LOGetCellError(cell *ole.IDispatch) (result *ole.VARIANT) {
	return oleutil.MustGetProperty(cell, "error")
}