How to Auto-Size AG Grid Columns to Fit Cell Contents with a Max Width Restriction?

I’m working with AG Grid in JavaScript, and I want to implement the following behavior:

When the grid is first loaded, I want the columns to auto-size based on the content, but with a maximum width restriction.

How can I achieve this?

I’m aware of the fitCellContents option, which adjusts the column size according to the content:

autoSizeStrategy: {
    type: 'fitCellContents'
}

However, according to the Ag Grid documentation, using fitCellContents doesn’t allow setting a maximum column width.

Important:

  • I don’t want to use defaultMaxWidth in the column definition since I want users to be able to resize the columns later.