-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Implement support for Excel EMBED function and object file embedding #2189
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
- Add AddEmbeddedObject function to embed files as objects in cells - Add SetCellEmbedFormula convenience function for EMBED formulas - Add content types and relationship support for OLE objects - Include comprehensive tests for both functions - Support Package and other object types for embedding Co-authored-by: xuri <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR implements Excel EMBED function support and object file embedding functionality to enable users to embed files as objects in Excel cells using the EMBED("Package","")
formula.
Key changes:
- Added
AddEmbeddedObject()
function to embed binary files as OLE objects with automatic EMBED formula generation - Added
SetCellEmbedFormula()
convenience function for setting EMBED formulas directly - Added necessary constants and XML structure support for OLE object relationships
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.
File | Description |
---|---|
templates.go | Adds content type and relationship constants for OLE objects |
picture.go | Implements core embedding functionality with OLE object management |
cell.go | Adds convenience function for setting EMBED formulas |
picture_test.go | Comprehensive test coverage for both new functions |
} | ||
|
||
// Create OLE object XML content | ||
oleObjectXML := fmt.Sprintf(`<oleObject progId="Package" dvAspect="DVASPECT_ICON" link="false" oleUpdate="OLEUPDATE_ONCALL" autoLoad="false" shapeId="1025" r:id="rId%d"/>`, rID) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The hardcoded shapeId="1025" could cause conflicts if multiple OLE objects are added to the same worksheet. Consider generating unique shape IDs or making this configurable.
Copilot uses AI. Check for mistakes.
} | ||
|
||
// Create OLE object XML content | ||
oleObjectXML := fmt.Sprintf(`<oleObject progId="Package" dvAspect="DVASPECT_ICON" link="false" oleUpdate="OLEUPDATE_ONCALL" autoLoad="false" shapeId="1025" r:id="rId%d"/>`, rID) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The progId is hardcoded as "Package" but should use the ObjectType from the options parameter to allow for different object types.
oleObjectXML := fmt.Sprintf(`<oleObject progId="Package" dvAspect="DVASPECT_ICON" link="false" oleUpdate="OLEUPDATE_ONCALL" autoLoad="false" shapeId="1025" r:id="rId%d"/>`, rID) | |
oleObjectXML := fmt.Sprintf(`<oleObject progId="%s" dvAspect="DVASPECT_ICON" link="false" oleUpdate="OLEUPDATE_ONCALL" autoLoad="false" shapeId="1025" r:id="rId%d"/>`, opts.ObjectType, rID) |
Copilot uses AI. Check for mistakes.
// addEmbeddedObject adds embedded object file to the package and returns the path. | ||
func (f *File) addEmbeddedObject(file []byte, filename string) string { | ||
count := f.countEmbeddedObjects() | ||
objPath := "embeddings/oleObject" + strconv.Itoa(count+1) + ".bin" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The filename parameter is not used in the function implementation. Either remove it or use it for better object identification in the generated path.
objPath := "embeddings/oleObject" + strconv.Itoa(count+1) + ".bin" | |
// Sanitize filename to remove invalid characters and extensions | |
baseName := strings.TrimSuffix(filepath.Base(filename), filepath.Ext(filename)) | |
baseName = strings.ReplaceAll(baseName, " ", "_") // Replace spaces with underscores | |
objPath := "embeddings/" + baseName + "_" + strconv.Itoa(count+1) + ".bin" |
Copilot uses AI. Check for mistakes.
objectType = "Package" | ||
} | ||
formula := fmt.Sprintf("EMBED(\"%s\",\"\")", objectType) | ||
return f.SetCellFormula(sheet, cell, formula) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing input validation for sheet and cell parameters. The function should validate these inputs before proceeding, similar to other cell functions in the codebase.
Copilot uses AI. Check for mistakes.
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)
EMBED("ObjectType","")
formulaxl/embeddings/oleObject*.bin
within the Excel package2.
SetCellEmbedFormula(sheet, cell, objectType)
=EMBED("ObjectType","")
syntaxUsage Examples
Embedding a file as an object:
Setting EMBED formula only:
Technical Implementation
ContentTypeOLEObject
andSourceRelationshipOLEObject
constants.bin
filesFile Structure Created
The implementation creates fully Excel-compatible files:
xl/embeddings/oleObject*.bin
xl/worksheets/_rels/sheet*.xml.rels
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.