UmyaSpreadsheet.FileFormatOptions (umya_spreadsheet_ex v0.7.0)

View Source

Advanced file format options for Excel files.

This module provides additional control over file format options that are available in the underlying Rust library but were not previously exposed in the Elixir wrapper.

Options include:

  • Compression level control for XLSX files
  • Enhanced encryption options
  • Converting a spreadsheet directly to a binary without writing to disk

Summary

Functions

Gets the default compression level used for XLSX files.

Gets the encryption algorithm used for a password-protected spreadsheet.

Checks if a spreadsheet has encryption enabled.

Converts a spreadsheet to binary XLSX format without writing to disk.

Writes a spreadsheet to disk with a specified compression level.

Functions

get_compression_level(spreadsheet)

@spec get_compression_level(UmyaSpreadsheet.Spreadsheet.t()) ::
  integer() | {:error, atom()}

Gets the default compression level used for XLSX files.

This function returns the current default compression level that will be used when writing XLSX files with the standard write function.

Parameters

  • spreadsheet - The spreadsheet struct

Returns

  • Integer from 0 to 9 representing the compression level (0 = no compression, 9 = maximum compression)

Examples

iex> {:ok, spreadsheet} = UmyaSpreadsheet.new()
iex> UmyaSpreadsheet.FileFormatOptions.get_compression_level(spreadsheet)
6

get_encryption_algorithm(spreadsheet)

@spec get_encryption_algorithm(UmyaSpreadsheet.Spreadsheet.t()) ::
  String.t() | nil | {:error, atom()}

Gets the encryption algorithm used for a password-protected spreadsheet.

This function returns the encryption algorithm used to protect the spreadsheet, or nil if the spreadsheet is not encrypted.

Parameters

  • spreadsheet - The spreadsheet struct

Returns

  • A string representing the encryption algorithm (e.g., "AES256") if the spreadsheet is encrypted
  • nil if the spreadsheet is not encrypted
  • {:error, reason} if the operation failed

Examples

iex> {:ok, spreadsheet} = UmyaSpreadsheet.new()
iex> UmyaSpreadsheet.FileFormatOptions.get_encryption_algorithm(spreadsheet)
nil

iex> {:ok, spreadsheet} = UmyaSpreadsheet.read("encrypted.xlsx", "password")
iex> UmyaSpreadsheet.FileFormatOptions.get_encryption_algorithm(spreadsheet)
"AES256"

is_encrypted(spreadsheet)

@spec is_encrypted(UmyaSpreadsheet.Spreadsheet.t()) :: boolean() | {:error, atom()}

Checks if a spreadsheet has encryption enabled.

This function checks if the spreadsheet has any workbook protection or encryption settings enabled.

Parameters

  • spreadsheet - The spreadsheet struct

Returns

  • Boolean indicating if encryption is enabled

Examples

iex> {:ok, spreadsheet} = UmyaSpreadsheet.new()
iex> UmyaSpreadsheet.FileFormatOptions.is_encrypted(spreadsheet)
false

iex> {:ok, spreadsheet} = UmyaSpreadsheet.read_xlsx_with_password("encrypted.xlsx", "secret123")
iex> UmyaSpreadsheet.FileFormatOptions.is_encrypted(spreadsheet)
true

to_binary_xlsx(spreadsheet)

@spec to_binary_xlsx(UmyaSpreadsheet.Spreadsheet.t()) :: binary() | {:error, atom()}

Converts a spreadsheet to binary XLSX format without writing to disk.

This function is useful when you need to:

  • Send an Excel file in an HTTP response
  • Store Excel files in a database
  • Process Excel files in memory without touching the filesystem

Parameters

  • spreadsheet - The spreadsheet struct

Returns

  • Binary data representing the Excel file

Examples

iex> {:ok, spreadsheet} = UmyaSpreadsheet.new()
iex> UmyaSpreadsheet.set_cell_value(spreadsheet, "Sheet1", "A1", "Hello")
iex> binary = UmyaSpreadsheet.FileFormatOptions.to_binary_xlsx(spreadsheet)
iex> is_binary(binary)
true
iex> byte_size(binary) > 0
true

write_with_compression(spreadsheet, path, compression_level)

@spec write_with_compression(
  UmyaSpreadsheet.Spreadsheet.t(),
  String.t(),
  non_neg_integer()
) ::
  :ok | {:error, atom()}

Writes a spreadsheet to disk with a specified compression level.

Compression levels range from 0 (no compression) to 9 (maximum compression). Higher compression levels result in smaller files but take longer to create.

Parameters

  • spreadsheet - The spreadsheet struct
  • path - Path where the Excel file will be saved
  • compression_level - Integer from 0 to 9 (0 = no compression, 9 = maximum compression)

Examples

iex> {:ok, spreadsheet} = UmyaSpreadsheet.new()
iex> UmyaSpreadsheet.FileFormatOptions.write_with_compression(spreadsheet, "high_compression.xlsx", 9)
:ok

iex> {:ok, spreadsheet} = UmyaSpreadsheet.new()
iex> UmyaSpreadsheet.FileFormatOptions.write_with_compression(spreadsheet, "no_compression.xlsx", 0)
:ok

write_with_encryption_options(spreadsheet, path, password, algorithm, salt_value \\ nil, spin_count \\ nil)

@spec write_with_encryption_options(
  UmyaSpreadsheet.Spreadsheet.t(),
  String.t(),
  String.t(),
  String.t(),
  String.t() | nil,
  non_neg_integer() | nil
) :: :ok | {:error, atom()}

Writes a spreadsheet to disk with enhanced encryption options.

This function provides more control over the encryption process than the standard write_with_password function, allowing you to specify encryption algorithm, salt values, and spin counts for enhanced security.

Parameters

  • spreadsheet - The spreadsheet struct
  • path - Path where the encrypted Excel file will be saved
  • password - Password to protect the file with
  • algorithm - Encryption algorithm (e.g., "AES128", "AES256", "default")
  • salt_value - Optional salt value for password derivation (if nil, a random salt is used)
  • spin_count - Optional spin count for key derivation (if nil, Excel default is used)

Examples

iex> {:ok, spreadsheet} = UmyaSpreadsheet.new()
iex> UmyaSpreadsheet.FileFormatOptions.write_with_encryption_options(
...>   spreadsheet,
...>   "highly_secure.xlsx",
...>   "secret123",
...>   "AES256"
...> )
:ok

iex> {:ok, spreadsheet} = UmyaSpreadsheet.new()
iex> UmyaSpreadsheet.FileFormatOptions.write_with_encryption_options(
...>   spreadsheet,
...>   "custom_encryption.xlsx",
...>   "very_secret",
...>   "AES256",
...>   "customSalt",
...>   100000
...> )
:ok