This repository contains the core types and conversion logic for the xll-gen project. It serves two main purposes:
- Go Protocol Types: Provides generated FlatBuffers Go code for the
xll-genprotocol. - C++ Library: Provides a static library for converting between Excel types (
XLOPER12,FP12) and FlatBuffers, along with memory management utilities.
The go/protocol directory contains the generated Go code from the protocol.fbs FlatBuffers schema (located at go/protocol/protocol.fbs). This package is used to serialize and deserialize messages exchanged between the Excel add-in and the backend service.
import "github.com/xll-gen/types/go/protocol"The package provides structs and builders for all protocol messages (e.g., Range, Scalar, Grid, Command).
The C++ library provides the heavy lifting for marshalling data between Excel's native formats and FlatBuffers. It also handles memory management for Excel data structures.
You can include this library in your CMake project using FetchContent.
include(FetchContent)
FetchContent_Declare(
xll-gen-types
GIT_REPOSITORY https://github.com/xll-gen/types.git
GIT_TAG main # Replace with specific tag or commit
)
FetchContent_MakeAvailable(xll-gen-types)
target_link_libraries(your_target PRIVATE xll-gen-types)Dependencies:
- FlatBuffers: This project depends on Google FlatBuffers. The CMake configuration handles this dependency.
This library is Windows-only. The C++ code targets Windows x86 / x86-64 with the Excel SDK and is built with MSVC 2019+ or MinGW (the windows-mingw CMake preset). All headers #include <windows.h> directly; there is no Linux/macOS compatibility layer (the former win_compat.h shim has been removed).
Header: include/types/converters.h
These functions convert between Excel XLOPER12/FP12 structures and FlatBuffers objects.
Excel to FlatBuffers:
flatbuffers::Offset<protocol::Range> ConvertRange(LPXLOPER12 op, flatbuffers::FlatBufferBuilder& builder, const std::string& format = "")- Converts an
XLOPER12(which can be a single value, array, or reference) into aprotocol::Rangemessage.
- Converts an
flatbuffers::Offset<protocol::Scalar> ConvertScalar(const XLOPER12& cell, flatbuffers::FlatBufferBuilder& builder)- Converts a single
XLOPER12cell value to aprotocol::Scalar.
- Converts a single
flatbuffers::Offset<protocol::Grid> ConvertGrid(LPXLOPER12 op, flatbuffers::FlatBufferBuilder& builder)- Converts an
XLOPER12array/multi to aprotocol::Grid(2D array of scalars).
- Converts an
flatbuffers::Offset<protocol::NumGrid> ConvertNumGrid(FP12* fp, flatbuffers::FlatBufferBuilder& builder)- Converts an
FP12(floating point array) to aprotocol::NumGrid.
- Converts an
flatbuffers::Offset<protocol::Any> ConvertAny(LPXLOPER12 op, flatbuffers::FlatBufferBuilder& builder)- Generic conversion that detects the type of
XLOPER12and converts it to the appropriateprotocol::Anyunion type.
- Generic conversion that detects the type of
FlatBuffers to Excel:
LPXLOPER12 AnyToXLOPER12(const protocol::Any* any)- Converts a
protocol::Anymessage back to anXLOPER12. The returned pointer is managed by the library's memory pool or is an Excel-managed string.
- Converts a
LPXLOPER12 RangeToXLOPER12(const protocol::Range* range)- Converts a
protocol::RangetoXLOPER12.
- Converts a
LPXLOPER12 GridToXLOPER12(const protocol::Grid* grid)- Converts a
protocol::GridtoXLOPER12.
- Converts a
FP12* NumGridToFP12(const protocol::NumGrid* grid)- Converts a
protocol::NumGridtoFP12.
- Converts a
Header: include/types/mem.h
Manages memory for XLOPER12 and FP12 structures, ensuring thread safety and proper cleanup.
LPXLOPER12 NewXLOPER12()- Allocates a new
XLOPER12from the thread-safe object pool.
- Allocates a new
void ReleaseXLOPER12(LPXLOPER12 p)- Returns an
XLOPER12to the pool. Use this for internal cleanup of intermediate values.
- Returns an
LPXLOPER12 NewExcelString(const std::wstring& str)- Creates an
XLOPER12string (Pascal-style) that is managed by the DLL. It setsxlbitDLLFree, so Excel will callxlAutoFree12when done.
- Creates an
FP12* NewFP12(int rows, int cols)- Allocates a new
FP12structure.
- Allocates a new
void __stdcall xlAutoFree12(LPXLOPER12 p)- The standard callback invoked by Excel to free memory allocated by the XLL (specifically for
xlbitDLLFreetypes).
- The standard callback invoked by Excel to free memory allocated by the XLL (specifically for
Header: include/types/PascalString.h
Utilities for handling Excel's length-prefixed (Pascal) strings.
std::vector<char> CStringToPascalString(const std::string& c_str)- Converts C++
std::stringto a byte-length-prefixed string.
- Converts C++
std::string PascalStringToCString(const unsigned short* pascal_str)- Converts a byte-length-prefixed string to
std::string.
- Converts a byte-length-prefixed string to
std::vector<wchar_t> WStringToPascalString(const std::wstring& w_str)- Converts
std::wstringto Excel 12's wide character Pascal string (firstwchar_tis length).
- Converts
std::wstring PascalString12ToWString(const wchar_t* pascal_str)- Converts Excel 12 Pascal string to
std::wstring.
- Converts Excel 12 Pascal string to
std::wstring PascalToWString(const wchar_t* pascal_str)- Alias for
PascalString12ToWString.
- Alias for
wchar_t* WStringToNewPascalString(const std::wstring& w_str)- Creates a new Pascal string on the heap.
Header: include/types/utility.h
LPXLOPER12 TempStr12(const wchar_t* txt)- Creates a temporary
XLOPER12string (mostly for calling Excel functions).
- Creates a temporary
LPXLOPER12 TempInt12(int val)- Creates a temporary
XLOPER12integer.
- Creates a temporary
std::wstring StringToWString(const std::string& str)std::string WideToUtf8(const std::wstring& wstr)std::wstring ConvertToWString(const char* str)std::string ConvertExcelString(const wchar_t* wstr)bool IsSingleCell(LPXLOPER12 pxRef)- Checks if a reference
XLOPER12points to a single cell.
- Checks if a reference
std::wstring GetXllDir()- Returns the directory where the XLL is located.
void SetDebugFlag(bool enabled)- Enables or disables debug logging.
bool GetDebugFlag()- Returns the current debug logging state.
void DebugLog(const char* fmt, ...)- Logs a formatted message to the Windows debug output (e.g. the Visual Studio Output window / DebugView, via
OutputDebugStringA) if the debug flag is enabled.
- Logs a formatted message to the Windows debug output (e.g. the Visual Studio Output window / DebugView, via
Header: include/types/ObjectPool.h
template <typename T, size_t ShardCount = 16> class ObjectPool- A thread-safe, sharded object pool used internally for
XLOPER12allocation to reduce heap contention.
- A thread-safe, sharded object pool used internally for
Header: include/types/xlcall.h
This library includes the standard Excel C API definitions (SDK Version 15.0), defining types like XLOPER12, FP12, XLREF12, etc., and the Excel12 / Excel12v callbacks.
This project uses Task to automate development tasks.
- CMake 3.24+
- Go 1.18+
- C++ Compiler (C++17 support)
clang-format(optional, for formatting)- Task
- Build:
task build(configures and builds). - Test:
task test(runs unit tests). - Format:
task format(formats C++ and Go files). - Generate:
task generate(regenerates Go and C++ code). - Clean:
task clean(removes build directory).
If you don't have task installed, you can run the underlying CMake commands directly (see Taskfile.yml for details).