GetRows() { AssertValid(); // Get the visible range. If the table is filtered, the range may // contain multiple areas. Range oVisibleTableRange; if (!ExcelTableUtil.TryGetVisibleTableRange(m_oTable, out oVisibleTableRange)) { yield break; } // Loop through the areas, and split each area into subranges if the // area contains too many rows. ExcelTableRow oExcelTableRow = new ExcelTableRow(this); foreach (Range oSubrange in ExcelRangeSplitter.SplitRange(oVisibleTableRange)) { m_oCurrentSubrange = oSubrange; m_aoCurrentSubrangeValues = ExcelUtil.GetRangeValues( m_oCurrentSubrange); Int32 iRows = m_oCurrentSubrange.Rows.Count; for (m_iCurrentRowOneBased = 1; m_iCurrentRowOneBased <= iRows; m_iCurrentRowOneBased++) { // Note that the same ExcelTableRow object is always returned, // and the object doesn't know anything about the current row. // The current row information is maintained by this class, not // by ExcelTableRow, and ExcelTableRow forwards all its method // calls to this class. yield return(oExcelTableRow); } } m_oCurrentSubrange = null; m_aoCurrentSubrangeValues = null; m_iCurrentRowOneBased = Int32.MinValue; }
FillIDColumn ( ListObject oTable ) { Debug.Assert(oTable != null); AssertValid(); // Read the range that contains visible data. If the table is // filtered, the range may contain multiple areas. Range oVisibleRange; ListColumn oIDColumn; if ( !ExcelTableUtil.TryGetVisibleTableRange(oTable, out oVisibleRange) || ExcelTableUtil.VisibleTableRangeIsEmpty(oTable) || !ExcelTableUtil.TryGetOrAddTableColumn(oTable, CommonTableColumnNames.ID, ExcelTableUtil.AutoColumnWidth, null, out oIDColumn) ) { return; } Int32 iIDColumnIndex = oIDColumn.Index; Range oDataBodyRange = oTable.DataBodyRange; Debug.Assert(oDataBodyRange != null); Debug.Assert(oTable.Parent is Worksheet); Worksheet oWorksheet = (Worksheet)oTable.Parent; foreach (Range oArea in oVisibleRange.Areas) { // Get the rows within the ID column that should be filled in. Int32 iAreaStartRowOneBased = oArea.Row; Int32 iRowsInArea = oArea.Rows.Count; Int32 iTableStartRowOneBased = oTable.Range.Row; Range oIDRange = oWorksheet.get_Range( (Range)oDataBodyRange.Cells[ iAreaStartRowOneBased - iTableStartRowOneBased, iIDColumnIndex], (Range)oDataBodyRange.Cells[ iAreaStartRowOneBased - iTableStartRowOneBased + iRowsInArea - 1, iIDColumnIndex] ); // Use the Excel row numbers as the unique IDs. Create a // one-column array, then fill it in with the row numbers. Int32 iRows = oIDRange.Rows.Count; Object [,] aoValues = ExcelUtil.GetSingleColumn2DArray(iRows); for (Int32 i = 1; i <= iRows; i++) { aoValues[i, 1] = iAreaStartRowOneBased + i - 1; } oIDRange.Value2 = aoValues; #if false // Note: Don't use the following clever code to fill in the row // numbers. On large worksheets, the calculations take forever. oIDRange.Value2 = "=ROW()"; oIDRange.Value2 = oIDRange.Value2; #endif } }