Articles in this section
Category / Section

How to adjust the width of columns in a JavaScript Spreadsheet based on their content

1 min read

This article explains how to adjust the width of columns in a JavaScript spreadsheet based on their content. Use the autoFit method to achieve this requirement. Construct the active sheet’s used column range by invoking the getColumnHeaderText function and providing it to the autoFit method as an argument.

[HTML]

 <div id="spreadsheet"></div>

[TS]

let spreadsheet: Spreadsheet = new Spreadsheet({
 sheets: [
   {
     name: 'Car Sales Report',
     ranges: [{ dataSource: (dataSource as any).defaultData }]
   }
 ],

created: (): void => {
    // Applies cell and number formatting to specified range of the active sheet
      spreadsheet.cellFormat(
     { fontWeight: 'bold', textAlign: 'center', verticalAlign: 'middle' },
     'A1:F1'
     );
   // Get the current sheet's used range.
   let usedRange: UsedRangeModel = spreadsheet.getActiveSheet().usedRange;
   // Get the used column range using the getColumnHeaderText method.
   let columnRange: string =
     getColumnHeaderText(1) +
     ':' +
     getColumnHeaderText(usedRange.colIndex + 1);

  // To auto fit the used column width based on the content width.
     spreadsheet.autoFit(columnRange);
 }
});

spreadsheet.appendTo('#spreadsheet');

Refer to the working sample for additional details and implementation:: Sample

Screenshot:

autofit.png

API Reference:

https://ej2.syncfusion.com/documentation/api/spreadsheet#autofit

Conclusion

We hope you enjoyed learning about how to adjust the width of columns in a JavaScript Spreadsheet based on their content.

You can refer to our JavaScript Spreadsheet page to learn about its other groundbreaking feature representations. You can also explore our JavaScript Spreadsheet Documentation to understand how to manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, BoldDesk Support, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied