You can try, Office Scripts, "sheet.getUsedRange().getFormat().autofitColumns()" but it requires Microsoft 365 Business Standard, E3, E5, or Education license. Most enterprises would have this, please contact your IT dept to enable Office Scripts in the Microsoft 365 Admin Center.
Orelse, you can handle column auto-width, using openpyxl, entirely within Databricks before the file gets passed to Power Automate, no Office Scripts needed, no Excel Online dependency.
- Convert Spark DataFrame to Pandas
- Write headers and data to an Excel worksheet
- Calculate max content length per column
- Apply dynamic column widths using openpyxl.utils.get_column_letter
- Save the file to DBFS or preferred location for PowerAutomate to pikup.