Documentation
¶
Rendered for windows/amd64
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ( // ErrForEachBreak is used to break out of a ForEach loop. ErrForEachBreak error = &ForEachBreak{} )
Functions ¶
func Do ¶ added in v0.2.0
Do executes the function with a Background context.
Example ¶
package main
import (
"fmt"
"log"
"github.com/xll-gen/sugar"
)
func main() {
sugar.Do(func(ctx sugar.Context) error {
excel := ctx.Create("Excel.Application")
if err := excel.Err(); err != nil {
log.Println("Failed to create Excel:", err)
return err
}
defer excel.Call("Quit")
excel.Put("Visible", true)
wb := excel.Get("Workbooks").Call("Add")
name, _ := wb.Get("Name").Value()
fmt.Printf("Workbook Name: %v\n", name)
return nil
})
}
Output:
Types ¶
type Chain ¶
type Chain interface {
// Get retrieves a property from the current COM object and returns a NEW Chain
// representing the property value. If the property is a COM object, it will
// be automatically tracked if a Context is present.
Get(prop string, params ...interface{}) Chain
// Call executes a method on the current COM object and returns a NEW Chain
// representing the return value. If the value is a COM object, it will
// be automatically tracked if a Context is present.
Call(method string, params ...interface{}) Chain
// Put sets a property on the current COM object. It returns the same Chain
// instance (or an error-carrying Chain) to allow further operations.
Put(prop string, params ...interface{}) Chain
// ForEach iterates over a COM collection (any object that implements IEnumVARIANT).
// For each item, the callback is executed with a new Chain instance.
//
// To stop iteration:
// - Return nil to continue to the next item.
// - Return ErrForEachBreak (or an error wrapping it) to stop iteration.
// - Return any other error to stop and propagate the error to the parent Chain.
//
// NOTE: The break error is recorded in the Chain and should be checked manually
// by the caller via Err() if they need to distinguish it from other errors.
ForEach(callback func(item Chain) error) Chain
// Fork creates a new independent reference to the current COM object.
// Both the original and the forked Chain will point to the same object
// but are managed as separate entries in the Context's arena.
Fork() Chain
// Store increases the reference count and returns the raw *ole.IDispatch.
// The caller is responsible for calling Release() on the returned object
// if it's not managed by sugar.Context.
Store() (*ole.IDispatch, error)
// Release manually releases the held COM object. Usually, this is handled
// automatically by the sugar.Context, but can be used for early cleanup.
Release() error
// IsDispatch returns true if the last operation's result is a COM object (IDispatch).
IsDispatch() bool
// Value retrieves the underlying Go value of the last operation's result.
// Returns an error if the result is a COM object (use Store() instead).
Value() (interface{}, error)
// Err returns the first error encountered in the chain of operations.
Err() error
}
Chain provides a fluent interface for chaining OLE operations. It handles error propagation, allowing you to call multiple methods and check the error once at the end via Err().
func GetActive ¶
GetActive starts a new chain by attaching to a running COM object.
Example ¶
package main
import (
"fmt"
"github.com/xll-gen/sugar"
)
func main() {
sugar.Do(func(ctx sugar.Context) error {
excel := ctx.GetActive("Excel.Application")
if err := excel.Err(); err != nil {
fmt.Println("GetActive failed as expected.")
} else {
fmt.Println("GetActive succeeded.")
}
return nil
})
}
Output:
type Context ¶ added in v0.2.0
type Context interface {
context.Context
// Track registers a Chain with the Context for automatic release.
Track(ch Chain) Chain
// Create is a wrapper around sugar.Create that automatically tracks the chain.
Create(progID string) Chain
// GetActive is a wrapper around sugar.GetActive that automatically tracks the chain.
GetActive(progID string) Chain
// From is a wrapper around sugar.From that automatically tracks the chain.
From(disp *ole.IDispatch) Chain
// Release releases all tracked chains in LIFO order.
Release() error
// Do executes the function within a nested scope of this context.
Do(fn func(ctx Context) error) error
// Go executes the function in a new goroutine branching from this context.
Go(fn func(ctx Context) error)
}
Context manages the lifecycle of multiple Chains and implements context.Context.
func NewContext ¶ added in v0.2.0
NewContext creates a new Context with the given parent.
type ForEachBreak ¶ added in v0.4.2
type ForEachBreak struct {
Value interface{}
}
ForEachBreak is returned when ForEach iteration is explicitly broken.
func (*ForEachBreak) Error ¶ added in v0.4.2
func (e *ForEachBreak) Error() string
func (*ForEachBreak) Is ¶ added in v0.4.2
func (e *ForEachBreak) Is(target error) bool
type Runner ¶ added in v0.2.0
type Runner struct {
// contains filtered or unexported fields
}
Runner configures the execution environment for COM operations.
Click to show internal directories.
Click to hide internal directories.