How to iterate over non-empty cells?

Posted by: peter on 29 August 2025, 2:11 am EST

  • Posted 29 August 2025, 2:11 am EST

    Is there a simple way to iterate over the non-empty cells of a Worksheet?

  • Posted 29 August 2025, 6:10 am EST

    Hi,

    To iterate over the cells of a worksheet in SpreadJS, you can use the getUsedRange and getArray methods. The getUsedRange method returns the rectangular range that covers all cells containing data, and getArray allows you to fetch those values as a JavaScript array. You can then check each cell within this range to see whether it is non-empty.

    Please refer to the code snippet below that demonstrates the above said.

    const usedRange = spreadSheet.getUsedRange(GC.Spread.Sheets.UsedRangeType.all);
    const {row, col, rowCount, colCount} = usedRange;
    const valueArray = spreadSheet.getArray(row, col, rowCount, colCount);
    for(let r=0; r<rowCount; r++) {
      for(let c=0; c<colCount; c++) {
        let cellValue = valueArray[r][c];
        if(cellValue) {
          console.log(`Cell[${row+r+1}, ${col+c+1}] = ${cellValue}`);
        }
      }
    }

    You can further refer to the attached sample that uses the above code snippet and fetches the non-empty cells as required (see below).

    Please let us know if you require any further assistance.

    Kind Regards,

    Chirag Gupta

    Attachment: https://jscodemine.mescius.io/share/S2VHzbeF1UOzp_zgIdOpug/?defaultOpen={"OpenedFileName"%3A["%2Findex.html"%2C"%2Fapp.js"%2C"%2Fstyles.css"]%2C"ActiveFile"%3A"%2Fapp.js"}

    References:

    1. getUsedRange: https://developer.mescius.com/spreadjs/api/classes/GC.Spread.Sheets.Worksheet#getusedrange
    2. getArray: https://developer.mescius.com/spreadjs/api/classes/GC.Spread.Sheets.Worksheet#getarray
  • Posted 29 August 2025, 12:05 pm EST

    Thanks, getting the rectangular range containing the data is better than iterating over max rows/cols, but is there something that just returns the non-empty cells?

    For example, if there are 10 non-empty cells in a 1000 x 1000 grid, I just want to iterate over those 10.

  • Posted 31 August 2025, 11:52 pm EST

    Hi,

    Apologies for the delay caused over the weekend.

    It is possible to extract only the non-empty cells from a worksheet in SpreadJS by using the Spreadsheet JSON. Please refer to the code snippet below that illustrates the same:

    const sheetJSON = spreadSheet.toJSON();
    const nonEmptyCellsDT = sheetJSON.data.dataTable;
    

    You can further refer to the attached sample, which uses the above code snippet and extracts only the non-empty cells (see below).

    However, the most optimal approach to extract the non-empty cells from a spreadsheet is to use the getUsedRange and getArray as illustrated in the previous response.

    Please let us know if you require any further assistance.

    Kind Regards,



    Chirag Gupta

    Attachment: https://jscodemine.mescius.io/share/S2VHzbeF1UOzp_zgIdOpug/?defaultOpen={"OpenedFileName"%3A["%2Findex.html"%2C"%2Fapp.js"]%2C"ActiveFile"%3A"%2Fapp.js"}

Need extra support?

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

Learn More

Forum Channels