Skip to content

[BUG] Table cell margin produces NaN XML attributes when margin is not a number or array #1440

@NAM-MAN

Description

@NAM-MAN

Description

When cellOpts.margin evaluates to a truthy non-number, non-array value (e.g., an object or a string), the cell margin XML attributes are generated as marL="NaN" marR="NaN" marT="NaN" marB="NaN", which triggers the PowerPoint repair dialog.

Root Cause

In gen-objects.ts (compiled: pptxgen.cjs.js around L5322), the margin handling converts numbers to arrays but does not guard against non-array, non-number values:

let cellMargin = cellOpts.margin === 0 || cellOpts.margin ? cellOpts.margin : DEF_CELL_MARGIN_IN;
if (!Array.isArray(cellMargin) && typeof cellMargin === 'number')
    cellMargin = [cellMargin, cellMargin, cellMargin, cellMargin];
// If cellMargin is truthy but not a number or array, it falls through
// Then cellMargin[0] is undefined → inch2Emu(undefined) → Math.round(914400 * undefined) → NaN

valToPts() has a Number(pt) || 0 guard that catches NaN, but inch2Emu() does not — it directly computes Math.round(EMU * inches) which produces NaN.

Steps to Reproduce

  1. Create a table where cell margin resolves to a non-number, non-array truthy value
  2. Generate the .pptx
  3. Open in Microsoft PowerPoint → repair dialog appears

Expected Behavior

Invalid margin values should fall back to DEF_CELL_MARGIN_IN instead of producing NaN.

Suggested Fix

let cellMargin = cellOpts.margin === 0 || cellOpts.margin ? cellOpts.margin : DEF_CELL_MARGIN_IN;
if (!Array.isArray(cellMargin) && typeof cellMargin === 'number')
    cellMargin = [cellMargin, cellMargin, cellMargin, cellMargin];
if (!Array.isArray(cellMargin))
    cellMargin = DEF_CELL_MARGIN_IN;
cellMargin = cellMargin.map(v => (typeof v === 'number' && !isNaN(v)) ? v : 0);

Environment

  • PptxGenJS: 4.0.1
  • PowerPoint for Mac (Microsoft 365)
  • Node.js 22.x

Related

Possibly related to #1137 (margin unit inconsistency).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions