Skip to content

Text metrics ​

estimateWrappedLines ​

function

The number of lines text occupies when wrapped at width character units - the width unit a column states, so estimateWrappedLines(cell, sheet.getColumn(2).width ?? 8.43) is the shape of the call.

A hard break opens a line of its own and what follows wraps independently, matching how Excel lays a wrapped cell out. The empty string is one line, not zero: a cell always occupies its row.

An estimate, and only ever that. It counts characters, so it is exact for a monospaced face that wraps mid-word and approximate for every other - a run of Ws wraps sooner on screen than this predicts, a run of is later. Against Excel it reads a shade low, because Excel breaks at word boundaries and its usable width is about 0.64 character units under the stated one: measured at 5 lines where Excel laid out 6, 25 where Excel laid out 26. This exists so that a writer can state a height rather than leave one to the application that opens the file, and being within a line of the truth is what that needs.

ts
function estimateWrappedLines(text: string, width: number): number;

Throws: RangeError if width is not a positive finite number - a column of zero width wraps nothing, and silently answering Infinity or NaN would put that straight into a row height.

Released under the MIT License.