excelize icon indicating copy to clipboard operation
excelize copied to clipboard

Implement support for Excel EMBED function and object file embedding

Open Copilot opened this issue 8 months ago • 0 comments

This PR adds comprehensive support for embedding object files in Excel cells using the EMBED function, addressing the user request to support type object file import in Excel.

Problem

Users needed the ability to embed files as objects in Excel cells and use Excel's EMBED("Package","") formula functionality, which was not previously supported by the excelize library.

Solution

Added two main functions to enable object embedding:

1. AddEmbeddedObject(sheet, cell, filename, fileData, options)

  • Embeds file content as an object in a specified cell
  • Automatically sets the EMBED("ObjectType","") formula
  • Stores binary data in xl/embeddings/oleObject*.bin within the Excel package
  • Creates proper OLE object relationships and XML structure
  • Supports customizable object types and options

2. SetCellEmbedFormula(sheet, cell, objectType)

  • Convenience function for setting EMBED formulas directly
  • Generates proper =EMBED("ObjectType","") syntax
  • Defaults to "Package" object type if not specified

Usage Examples

Embedding a file as an object:

// Read file content
fileData, err := os.ReadFile("document.pdf")

// Embed as object with formula
err = f.AddEmbeddedObject("Sheet1", "A1", "document.pdf", fileData, 
    &excelize.EmbeddedObjectOptions{
        ObjectType: "Package",
        AltText:    "Embedded PDF Document",
    })

Setting EMBED formula only:

err = f.SetCellEmbedFormula("Sheet1", "B1", "Package")
// Results in: =EMBED("Package","")

Technical Implementation

  • Added ContentTypeOLEObject and SourceRelationshipOLEObject constants
  • Implemented proper content type registration for embedded .bin files
  • Added OLE object XML generation in worksheet structure
  • Created relationship management for embedded files
  • Added comprehensive test coverage for both functions

File Structure Created

The implementation creates fully Excel-compatible files:

  • Embedded data stored in xl/embeddings/oleObject*.bin
  • Proper relationships in xl/worksheets/_rels/sheet*.xml.rels
  • OLE object definitions in worksheet XML
  • Content type registration for binary files

The generated Excel files are fully compatible with Microsoft Excel, where users can access embedded objects through Excel's standard object functionality.

Fixes #2186.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot avatar Jul 31 '25 06:07 Copilot