unioffice

repository·master·Indexed 26 days ago

https://github.com/unidoc/unioffice

A high-performance Go library for creating, reading, and editing Office Open XML documents, including .docx, .xlsx, and .pptx files. It provides high-level APIs for Word formatting and PDF conversion, Excel cell validation and formula evaluation, and PowerPoint template creation, while allowing direct manipulation of raw XML-based types via the X() method.

Tokens
1.7K
Snippets
8
Records
18
Agent score
39%

What's inside unioffice

  1. Overview of unioffice capabilities

    master

    unioffice is a high-performance Go library for creating and editing Office Open XML documents. Supported formats include:

    Word (.docx)

    • Read, Write, and Edit
    • Formatting, Images, and Tables
    • Word to PDF conversion

    Excel (.xlsx)

    • Read, Write, and Edit
    • Cell formatting (including conditional formatting)
    • Cell validation (drop-down comboboxes, rules, etc.)
    • Formatted value retrieval (e.g., dates/numbers as displayed in Excel)
    • Formula Evaluation (100+ functions supported)
    • Embedded Images and all chart types

    PowerPoint (.pptx)

    • Creation from templates
    • Textboxes and shapes
  2. Access raw OOXML types via X() method

    master

    If the high-level API does not support a specific OOXML feature, you can manipulate the raw XML-based types directly. These types are accessible from wrapper types using the X() method. For example, to set a document background color manually by editing the CT_Background element:

    doc := document.New()
    doc.X().Background = wordprocessingml.NewCT_Background()
    doc.X().Background.ColorAttr = &wordprocessingml.ST_HexColor{}
    doc.X().Background.ColorAttr.ST_HexColorRGB = color.RGB(50, 50, 50).AsRGBString()
  3. Get relative filenames for Office Open XML relationships

    master

    The RelativeFilename function calculates a filename that is relative to a specific source file referenced within a relationships file. This is useful when navigating the internal directory structure of Office Open XML documents (like .docx or .xlsx).

    Parameters:

    • dt: The DocType of the document.
    • relToTyp: The type of the file to which the new path should be relative.
    • typ: The type of the target file.
    • index: An index used when multiple files of the same type exist (e.g., multiple worksheets or drawings).
    func RelativeFilename (dt DocType ,relToTyp ,typ string ,index int )string
  4. Convert uint16 and uint32 to pointers

    master

    These helper functions return a pointer to a copy of the provided unsigned integer value. This is useful when an API requires a pointer to a primitive type (e.g., for optional fields in XML).

    func Uint32 (v uint32 )*uint32 {
    _ddc :=v 
    return &_ddc }
    
    func Uint16 (v uint16 )*uint16 {
    _ddg :=v 
    return &_ddg }
  5. Check if a string requires space preservation

    master
    The NeedsSpacePreserve function determines if a string contains leading or trailing whitespace (including tabs, newlines, and specific Unicode space characters like … or  ). This is used to decide whether to add the xml:space="preserve" attribute to an XML element.
  6. Convert primitive values to pointers

    master

    The unioffice package provides several utility functions to convert primitive Go types into pointers. This is useful when working with APIs that require pointer types for optional fields or specific XML marshaling requirements.

    Supported conversions:

    • String(v string) *string
    • Float64(v float64) *float64
    • Float32(v float32) *float32
    • Int32(v int32) *int32
    • Int8(v int8) *int8
    • Uint8(v uint8) *uint8
  7. Register a constructor for xsd:any elements

    master

    Use RegisterConstructor to register a custom constructor function for unmarshaling xsd:any elements. This allows the library to handle custom or unknown XML elements by mapping a namespace and name to a specific function.

    Parameters:

    • ns: The XML namespace.
    • name: The local name of the element.
    • fn: The constructor function to be used.
    func RegisterConstructor (ns ,name string ,fn interface{})
  8. Pointer helper functions for primitive types

    master

    The package provides helper functions to return a pointer to a value for common primitive types. This is useful when working with API fields that require pointer types (e.g., for optionality in XML).

    • Uint64(v uint64) *uint64
    • Bool(v bool) *bool
    • Int64(v int64) *int64