How and Where to Use Enumerators with Golang via C++
An enumerator is an object that provides the ability to traverse a container or a collection. Enumerators can be used to read the data in the collection, but they cannot be used to modify the underlying collection, whereas IEnumerable is an interface that defines one method GetEnumerator which returns an IEnumerator interface. This, in turn, allows read‑only access to a collection.
Aspose.Cells APIs provide a bunch of enumerators; however, this article mainly discusses the three types listed below.
- Cells Enumerator
- Rows Enumerator
- Columns Enumerator
How to use Enumerators
Cells Enumerator
There are various ways to access the Cells Enumerator, and one can use any of these methods based on the application requirements. Here are the methods that return the cells enumerator.
All of the above‑mentioned methods return an enumerator that allows traversing the collection of cells which have been initialized.
The following code example demonstrates the implementation of the IEnumerator interface for a Cells collection.
Rows Enumerator
The Rows Enumerator can be accessed while using the RowCollection.GetEnumerator method. The following code example demonstrates the implementation of the IEnumerator interface for RowCollection.
Columns Enumerator
Columns can be accessed while using the ColumnCollection.Get method. The following code example demonstrates the implementation of the Get method for ColumnCollection.
Where to use Enumerators
In order to discuss the advantages of using enumerators, let’s take a real‑time example.
Scenario
An application requirement is to traverse all cells in a given Worksheet to read their values. There could be several ways to implement this goal. A few are demonstrated below.
Using Display Range
Using MaxDataRow & MaxDataColumn
As you can observe, both of the above‑mentioned approaches use more or less similar logic, that is: loop over all cells in the collection to read the cell values. This could be problematic for a number of reasons as discussed below.
- APIs such as GetMaxRow(), GetMaxDataRow(), GetMaxColumn(), GetMaxDataColumn() & GetMaxDisplayRange() require extra time to gather the corresponding statistics. In case the data matrix (rows × columns) is large, using these APIs could impose a performance penalty.
- In most cases, not all cells in a given range are instantiated. In such situations, checking every cell in the matrix is not as efficient as checking only the initialized cells.
- Accessing a cell in a loop as
Cells row, columnwill cause all cell objects in a range to be instantiated, which may eventually cause anOutOfMemoryException.
Conclusion
Based on the above‑mentioned facts, the following are the possible scenarios where enumerators should be used.
- Read‑only access of the cell collection is required, that is: the requirement is to only inspect the cells.
- A large number of cells are to be traversed.
- Only initialized cells/rows/columns are to be traversed.