Row height autofit text

Posted by: earl on 30 September 2021, 9:45 am EST

    • Post Options:
    • Link

    Posted 30 September 2021, 9:45 am EST - Updated 3 October 2022, 9:36 am EST

    Good day,

    We are currently evaluating SpreadJS so apologies if we have missed this in the documentation. We have text that often spans merged cells and is set to wrap. Is there a method to auto set the row height for each paragraph.

    Please see image, correct showing what we would like happen automatically (row height adjusted to display all text) vs what is currently defaults to, i.e. only showing single line, and thus rest of text is cut off.

  • Posted 1 October 2021, 3:22 am EST

    Hi,

    For this, you may use the autoFitRow method and if you want to autoFit just after the user enters multiple lines of text you may use this method inside the CellChanged. Please refer to the following code snippet and attached sample that demonstrates the same.

    
      spread.bind(
        GC.Spread.Sheets.Events.CellChanged,
        (e, { sheet, row, col, propertyName, ...rest }) => {
          if (propertyName === 'value' && sheet.getCell(row, col).wordWrap()) {
            sheet.autoFitRow(row);
          }
        }
      );
    
    

    sample: https://stackblitz.com/edit/js-o9wmbv?file=index.js

    API:

    autoFitRow: https://www.grapecity.com/spreadjs/docs/v14/online/SpreadJS~GC.Spread.Sheets.Worksheet~autoFitRow.html

    Regards

    Avinash

  • Posted 5 October 2021, 10:30 am EST

    Hi Avinash,

    Thank you for this, we want to autofit everything that has been “Wrap text” on opening the document, is there a implementation for such?

    Thank you so much

  • Posted 6 October 2021, 2:08 am EST

    Hi,

    For this you need to override the fromJson method and once the JSON is loaded you need auto fit all the sheets. Please refer to the following code snippet and let me know if you face any issues.

    
    
    function autoFitRowAll(spread) {
    spread.suspendPaint()
      const hasWordWrap = (sheet, rowIndex) => {
        for (let col = 0; col < sheet.getColumnCount(); col++) {
          if (sheet.getCell(rowIndex, col).wordWrap()) return true;
        }
        return false;
      };
      spread.sheets.forEach((sheet) => {
        for (let row = 0; row < sheet.getRowCount(); row++) {
          if (hasWordWrap(sheet, row)) {
            sheet.autoFitRow(row);
          }
        }
      });
    spread.resumePaint();
    }
    
    let old = GC.Spread.Sheets.Workbook.prototype.fromJSON;
    GC.Spread.Sheets.Workbook.prototype.fromJSON = function () {
      old.apply(this, arguments);
      autoFitRowAll(designer.getWorkbook());
    };
    
    
    

    Regards

    Avinash

  • Posted 11 March 2025, 6:50 am EST

    Hi can this property work in the following way

    without looping over the rows can it be automatically get applied on the cells which have text wrap applied in the underlying excel sheet.

    As the above approach would require the system to loop over all the rows and some of the sheets can have lakhs of rows

    Please help if any alternative is there

  • Posted 12 March 2025, 5:32 am EST

    Hi,

    It seems like you have asked the same question in the case Text Wrapping: https://developer.mescius.com/forums/spreadjs/text-wrapping. The question is already answered there, therefore, we recommend you continue the thread there only to have a clear track of the conversation without any confusion on any end.

    Kind Regards,

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels