Forum > FPSpreadsheet

Cell Order


If I read the docs correctly, it appears the quickest way to get data from cells is to use a for in loop against the TsWorkSheet.Cells.  Something like:
for Cell in Sheet.Cells do

My question is, is there a guarantee ordering?  For example will this return cells by row by col?  In other words, row 0 cols 0 thru n in order then row 1, cells 0 thru n and so on.

Or, is there no guarantee of ordering?  It depends on the spreadsheet?

The pointers to the cell records are stored in an AVLTree which heavily depends on sorting. The cells are arranged from left to right and then from top to bottom, and this is the order in which a worksheet is traversed when you call "for cell in Worksheet.Cells".

As a descendant of TAVLTree, the TsCells class has a property OnCompare which you can replace to arrange the cells by column:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program project1; uses  fpSpreadsheet, fpsTypes, fpsClasses, xlsBIFF8, fpsOpenDocument;var  workbook: TsWorkbook;  worksheet: TsWorksheet;  cell: PCell; function CompareColRow(Item1, Item2: Pointer): Integer;begin  Result := Int64(PsRowCol(Item1)^.Col) - Int64(PsRowCol(Item2)^.Col);  if Result = 0 then    Result := Int64(PsRowCol(Item1)^.Row) - Int64(PsRowCol(Item2)^.Row);end; begin  workbook := TsWorkbook.Create;  try    worksheet := workbook.AddWorksheet('Sheet 1');    worksheet.Cells.OnCompare := @CompareColRow;    worksheet.WriteText(5, 3, 'D6');    worksheet.WriteText(0, 0, 'A1');    worksheet.WriteText(0, 1, 'B1');    worksheet.WriteText(1, 0, 'A2');    worksheet.WriteText(0, 2, 'C1');    worksheet.WriteText(1, 1, 'B2');     for cell in worksheet.Cells do      WriteLn('row ', cell^.Row, ', col ', cell^.Col, ' ---> ', worksheet.ReadAsText(cell));     workbook.WriteToFile('test.ods', sfOpenDocument, true);    workbook.WriteToFile('test.xls', sfExcel8, true);  finally    workbook.Free;  end;   WriteLn;  Write('Press ENTER to close');  ReadLn;end.
I cannot guarantee, however, that the default arrangement of cells by row implicitly isn't assumed somewhere internally. If you find an inconsistency, please report.

Thank you.  Just knowing it is row by row and in column order us helpful when traversing.  I don't need to change the sort at this time.


[0] Message Index

Go to full version