module Xlsxrb::Ooxml::Utils
OOXML-specific utility methods and constants.
Constants
- BUILTIN_DATE_FMT_IDS
-
Built-in numFmtIds that represent date/time formats.
- BUILTIN_NUM_FMT_CODES
-
Built-in number format codes defined by SpreadsheetML.
- DEFAULT_DATETIME_FORMAT
-
Default date-time format code used by
Writerfor Time cells. - DEFAULT_DATE_FORMAT
-
Default date format code used by
Writerfor Date cells. - EPOCH_1900
-
Excel 1900 date system epoch.
- EPOCH_1900_JD
- EPOCH_1904
-
Excel 1904 date system epoch.
- EPOCH_1904_JD
- MAX_DATE
-
Maximum date supported by Excel date systems.
- MAX_SERIAL_1900
-
Maximum serial values supported by Excel date systems (corresponding to 9999-12-31).
- MAX_SERIAL_1904
Public Class Methods
(untyped value) → untyped
Source
# File lib/xlsxrb/ooxml/utils.rb, line 182 def booleanize(value) case value when true then "1" when false then "0" else value end end
Converts a boolean to “1” or “0” for OOXML schema compliance. Returns non-boolean values as-is.
(String | Symbol attr_name, ?capitalize_first: bool) → String
Source
# File lib/xlsxrb/ooxml/utils.rb, line 192 def camelize(attr_name, capitalize_first: false) str = attr_name.to_s return "" if str.empty? parts = str.split("_") if parts.empty? "" elsif parts.size == 1 capitalize_first ? parts[0].sub(/\A\w/, &:upcase) : parts[0].sub(/\A\w/, &:downcase) else first = capitalize_first ? parts[0].capitalize : parts[0].sub(/\A\w/, &:downcase) rest = parts[1..].map(&:capitalize).join "#{first}#{rest}" end end
Converts an attribute name to camelCase (or PascalCase) for OOXML attributes.
(Date date, ?date1904: bool) → untyped
Source
# File lib/xlsxrb/ooxml/utils.rb, line 94 def date_to_serial(date, date1904: false) if date1904 raise ArgumentError, "Date is before the 1904 date system epoch (1904-01-01)" if date < EPOCH_1904 raise ArgumentError, "Date exceeds the maximum supported date (9999-12-31)" if date > MAX_DATE date.jd - EPOCH_1904_JD else raise ArgumentError, "Date is before the 1900 date system epoch (1899-12-31)" if date < EPOCH_1900 raise ArgumentError, "Date exceeds the maximum supported date (9999-12-31)" if date > MAX_DATE serial = date.jd - EPOCH_1900_JD # Lotus 1-2-3 bug: serial 60 = Feb 29, 1900 (doesn't exist). # Dates on or after Mar 1, 1900 (raw serial >= 60) need +1. serial += 1 if serial >= 60 serial end end
Converts a Date to an Excel serial number (1900 or 1904 system).
(Time | DateTime time, ?date1904: bool) → Float
Source
# File lib/xlsxrb/ooxml/utils.rb, line 132 def datetime_to_serial(time, date1904: false) date = time.to_date day_serial = date_to_serial(date, date1904: date1904) # Fractional part: seconds since midnight / seconds per day seconds_since_midnight = (time.hour * 3600) + (time.min * 60) + time.sec day_serial + (seconds_since_midnight.to_f / 86_400) end
Converts a Time or DateTime to a fractional Excel serial number (1900 or 1904 system).
(untyped password, ?algorithm: untyped, ?salt: untyped, ?spin_count: untyped) → untyped
Source
# File lib/xlsxrb/ooxml/utils.rb, line 155 def hash_password(password, algorithm: "SHA-512", salt: nil, spin_count: 100_000) raise ArgumentError, "password must be a String" unless password.is_a?(String) raise ArgumentError, "spin_count must be a positive Integer" unless spin_count.is_a?(Integer) && spin_count.positive? salt_bytes = (salt || SecureRandom.random_bytes(16)).b # OOXML sheet/workbook protection hashing uses UTF-16LE password bytes. password_bytes = password.encode("UTF-16LE").b digest_name = algorithm.tr("-", "") hash = OpenSSL::Digest.digest(digest_name, salt_bytes + password_bytes) spin_count.times do |i| iteration_bytes = [i].pack("V") # little-endian uint32 hash = OpenSSL::Digest.digest(digest_name, hash + iteration_bytes) end { algorithm_name: algorithm, hash_value: [hash].pack("m0"), salt_value: [salt_bytes].pack("m0"), spin_count: spin_count } end
Hashes a plain-text password for use with sheet/workbook protection. Returns { algorithm_name:, hash_value:, salt_value:, spin_count: }. Algorithm per ECMA-376 Part 4 §2.4.2.24.
(Numeric serial, ?date1904: bool) → Date
Source
# File lib/xlsxrb/ooxml/utils.rb, line 114 def serial_to_date(serial, date1904: false) max_serial = date1904 ? MAX_SERIAL_1904 : MAX_SERIAL_1900 raise ArgumentError, "Excel serial date cannot be NaN" if serial.respond_to?(:nan?) && serial.nan? raise ArgumentError, "Excel serial date must be non-negative" if serial.negative? raise ArgumentError, "Excel serial date exceeds maximum supported date (9999-12-31)" if serial >= max_serial + 1 days = serial.div(1) if date1904 EPOCH_1904 + days else # Adjust for Lotus 1-2-3 bug. days -= 1 if days > 60 EPOCH_1900 + days end end
Converts an Excel serial number (1900 or 1904 system) to a Date.
(Numeric serial, ?date1904: bool) → Time
Source
# File lib/xlsxrb/ooxml/utils.rb, line 143 def serial_to_datetime(serial, date1904: false) int_part, frac = serial.divmod(1) date = serial_to_date(int_part, date1904: date1904) total_seconds = (frac * 86_400).round hours, remaining = total_seconds.divmod(3600) minutes, seconds = remaining.divmod(60) Time.utc(date.year, date.month, date.day, hours, minutes, seconds) end
Converts a fractional Excel serial number to a Time (1900 or 1904 system, UTC). Excel serial numbers carry no timezone metadata; returned Time objects are in UTC.
(String text) → Integer
Source
# File lib/xlsxrb/ooxml/utils.rb, line 79 def utf16_length(text) text.length + text.b.count(SUPPLEMENTARY_LEAD_BYTES) end
The length of UTF-8 text in UTF-16 code units, the unit Excel counts its text limits in (ADR 0011): one per character, and one more for each character outside the Basic Multilingual Plane, such as an emoji.
(untyped serial, ?date1904: bool) → bool
Source
# File lib/xlsxrb/ooxml/utils.rb, line 85 def valid_excel_date?(serial, date1904: false) return false unless serial.is_a?(Numeric) max_serial = date1904 ? MAX_SERIAL_1904 : MAX_SERIAL_1900 serial >= 0 && serial < max_serial + 1 end
Checks whether a serial number is a valid Excel date within the supported range.