Skip to content

11. Images

Embed images into worksheets.

Supported Formats

FormatExtensionsMIME Type
PNGpngimage/png
JPEGjpeg, jpgimage/jpeg
GIFgifimage/gif
BMPbmpimage/bmp
ICOicoimage/x-icon
TIFFtiff, tifimage/tiff
SVGsvgimage/svg+xml
EMFemfimage/x-emf
EMZemzimage/x-emz
WMFwmfimage/x-wmf
WMZwmzimage/x-wmz

Format strings are case-insensitive (e.g., "PNG", "Svg", "TIFF" all work).

add_image / addImage

Add an image to a sheet at the specified cell position.

Rust:

rust
use sheetkit::image::{ImageConfig, ImageFormat};

let image_data = std::fs::read("logo.png")?;
let config = ImageConfig {
    data: image_data,
    format: ImageFormat::Png,
    from_cell: "B2".to_string(),
    width_px: 200,
    height_px: 100,
};
wb.add_image("Sheet1", &config)?;

TypeScript:

typescript
import { readFileSync } from "fs";

const imageData = readFileSync("logo.png");
wb.addImage("Sheet1", {
    data: imageData,
    format: "png",   // "png" | "jpeg" | "jpg" | "gif" | "bmp" | "ico" | "tiff" | "tif" | "svg" | "emf" | "emz" | "wmf" | "wmz"
    fromCell: "B2",
    widthPx: 200,
    heightPx: 100,
});

ImageConfig

FieldRust TypeTS TypeDescription
dataVec<u8>BufferRaw image bytes
formatImageFormatstringSee supported formats table above
from_cellStringstringAnchor cell (top-left corner)
width_pxu32numberImage width in pixels
height_pxu32numberImage height in pixels

Passing an unsupported format string returns an error with a message indicating the unrecognised format.

delete_picture / deletePicture

Delete a picture anchored at the given cell. Removes the drawing anchor, image data, relationship entry, and content type for the picture. Searches both one-cell and two-cell anchors.

Returns an error if no picture is found at the specified cell.

Parameters:

ParameterRust TypeTS TypeDescription
sheet&strstringSheet name
cell&strstringAnchor cell of the picture (e.g., "B2")

Rust:

rust
wb.delete_picture("Sheet1", "B2")?;

TypeScript:

typescript
wb.deletePicture("Sheet1", "B2");

get_pictures / getPictures

Get all pictures anchored at the given cell. Returns picture data, format, anchor cell reference, and dimensions for each picture found.

Parameters:

ParameterRust TypeTS TypeDescription
sheet&strstringSheet name
cell&strstringAnchor cell to query (e.g., "B2")

Returns: Vec<PictureInfo> (Rust) / JsPictureInfo[] (TypeScript)

Rust:

rust
let pictures = wb.get_pictures("Sheet1", "B2")?;
for pic in &pictures {
    println!("Format: {}, Cell: {}, Size: {}x{}",
        pic.format.extension(), pic.cell, pic.width_px, pic.height_px);
    // pic.data contains the raw image bytes
}

TypeScript:

typescript
const pictures = wb.getPictures("Sheet1", "B2");
for (const pic of pictures) {
    console.log(`Format: ${pic.format}, Cell: ${pic.cell}, Size: ${pic.widthPx}x${pic.heightPx}`);
    // pic.data is a Buffer containing the raw image bytes
}

get_picture_cells / getPictureCells

Get all cells that have pictures anchored to them on the given sheet.

Parameters:

ParameterRust TypeTS TypeDescription
sheet&strstringSheet name

Returns: Vec<String> (Rust) / string[] (TypeScript) -- list of cell references (e.g., ["A1", "B2", "D5"])

Rust:

rust
let cells = wb.get_picture_cells("Sheet1")?;
for cell in &cells {
    println!("Picture at: {}", cell);
}

TypeScript:

typescript
const cells = wb.getPictureCells("Sheet1");
for (const cell of cells) {
    console.log(`Picture at: ${cell}`);
}

PictureInfo / JsPictureInfo

Information about a picture retrieved from a worksheet.

FieldRust TypeTS TypeDescription
dataVec<u8>BufferRaw image bytes
formatImageFormatstringImage format (Rust enum / format extension string)
cellStringstringAnchor cell reference (e.g., "B2")
width_px / widthPxu32numberImage width in pixels
height_px / heightPxu32numberImage height in pixels

Released under the MIT / Apache-2.0 License.