Extracting visible sheet in Revit using C# - c#

Is there a way to only select Sheet Lists in a Schedule?
Like in this picture, but instead of those fields, I want only the existent Sheet Lists in the project.
Creating a Sheet List, but I want all the Sheet Lists in the fields.

I actually found code that helped me out with this:
public IEnumerable<ViewSheet> GetAllSheets (Document doc)
{
IEnumerable<ViewSheet> sheets = new FilteredElementCollector(doc)
.OfClass(typeof(ViewSheet))
.OfCategory(BuiltInCategory.OST_Sheets)
.Cast<ViewSheet>()
;
return sheets;
}

Related

I have to select a specific row in an Excel file

I'm working on an Excel file that I have to convert to an XML file.
Every time I cycle the rows to check if they are empty I use this code:
var ComuniRow = firstRowUsed.RowUsed();
while (!ComuniRow.Cell(coComuneId).IsEmpty())
{
// do stuff;
}
This time tho I need to select a specific row, not the starting one but I have no Idea about how to do that.
Any solutions?
Thanks :)
You can group the rows.
public void UngroupFromAll(){
IXLWorksheet ws = new XLWorkbook().AddWorksheet("Sheet1");
ws.Rows(1, 2).Group();
ws.Rows(1, 2).Ungroup(true);
}

How to read the state of a checkbox in an excel file with EPPlus ( C# )

The title already explains my problem pretty well. I have an excel file which contains checkboxes and I would like to read their state (checked or not) using the EPPlus library.
I am not sure if this is even supported. So far I have found no documentation or examples for that specific problem using EPPlus.
If you add a Cell link then pulling the value is straight forward. I don't believe that the Drawing Object contains the value.
using System.Linq;
using OfficeOpenXml;
using OfficeOpenXml.Drawing;
namespace EPPlus {
public void Run() {
var excelFile = new System.IO.FileInfo(System.IO.Path.Combine(BaseDirectory, "Excel", "Checkbox.xlsx"));
using (ExcelPackage excel = new ExcelPackage(excelFile))
{
ExcelWorksheet sheet = excel.Workbook.Worksheets.SingleOrDefault(a => a.Name == "Sheet1");
ExcelDrawing checkbox2 = sheet.Drawings.SingleOrDefault(a => a.Name == "Check Box 2");
var value = sheet.Cells["G5"].Value.ToString();
}
}
}
}
For existing excel, just designate a cell somewhere and link it to the checkbox. Insert the true/false value directly to that cell (NOT TO THE CHECKBOX). The checkbox will automatically reflect the value of the cell in the checkbox.
You can put all designated cells in a certain column, then hide that column. :)

How do I insert Excel cells without creating a corrupt file?

I'm using the OpenXML SDK to update the contents of an Excel spreadsheet. When inserting cells into an Excel row they must be inserted in the correct order or the file will not open properly in Excel. I'm using the following code to find the first cell that will be after the cell I am inserting. This code comes almost directly from the OpenXML SDK documentation
public static Cell GetFirstFollowingCell(Row row, string newCellReference)
{
Cell refCell = null;
foreach (Cell cell in row.Elements<Cell>())
{
if (string.Compare(cell.CellReference.Value, newCellReference, true) > 0)
{
refCell = cell;
break;
}
}
return refCell;
}
When I edit files with this code and then open them in Excel, Excel reports that the file is corrupted. Excel is able to repair the file, but most of the data is removed from the workbook. Why does this result in file corruption?
Side note: I tried two different .NET Excel libraries before turning to the painfully low-level OpenXML SDK. NPOI created spreadsheets with corruption and EPPlus threw an exception whenever I tried to save. I was using the most recent version of each.
The code you are using is seriously flawed. This is very unfortunate, seeing as it comes from the documentation. It may work acceptably for spreadsheets that only use the first 26 columns but will fail miserably when confronted with "wider" spreadsheets. The first 26 columns are named alphabetically, A-Z. Columns 27-52 are named AA-AZ. Column 53-78 are named BA-BZ. (You should notice the pattern.)
Cell "AA1" should come after all cells with a single character column name (i.e. "A1" - "Z1"). Let's examine the current code comparing cell "AA1" with cell "B1".
string.Compare("B1", "AA1", true) returns the value 1
The code interprets this to mean that "AA1" should be placed before cell "B1".
The calling code will insert "AA1" before "B1" in the XML.
At this point the cells will be out of order and the Excel file is corrupted. Clearly, string.Compare by itself is not a sufficient test to determine the proper order of cells in a row. A more sophisticated comparison is required.
public static bool IsNewCellAfterCurrentCell(string currentCellReference, string newCellReference)
{
var columnNameRegex = new Regex("[A-Za-z]+");
var currentCellColumn = columnNameRegex.Match(currentCellReference).Value;
var newCellColumn = columnNameRegex.Match(newCellReference).Value;
var currentCellColumnLength = currentCellColumn.Length;
var newCellColumnLength = newCellColumn.Length;
if (currentCellColumnLength == newCellColumnLength)
{
var comparisonValue = string.Compare(currentCellColumn, newCellColumn, StringComparison.OrdinalIgnoreCase);
return comparisonValue > 0;
}
return currentCellColumnLength < newCellColumnLength;
}
If you wanted to place a new cell in column "BC" and you were comparing to cell "D5" you would use IsCellAfterColumn("D5", "BC5"). Substituting the new comparison function into the original code and simplifying with LINQ:
public static Cell GetFirstFollowingCell(Row row, string newCellReference)
{
var rowCells = row.Elements<Cell>();
return rowCells.FirstOrDefault(c => IsNewCellAfterCurrentCell(c.CellReference.Value, newCellReference));
}

ClosedXML iterate Worksheets programmatically.

In my Workbook, I have 4 worksheets with different tab names.
Say if they are named as follows: First, Second, Third, Fourth.
I could not find online how to iterate through each of the worksheet with say for for loop. As I am iterating, I would also like to capture the text on the Worksheet's tab (First, Second, etc.).
You can either grab the worksheets by name or id such as:
int index = 1; // note indexes are 1 based in ClosedXML
var worksheet = workbook.Worksheet(index);
string name = "First";
var worksheet = workbook.Worksheet(name);
Note you'll only want to do the above in instances where you know the sheet name and max id (example)
or you can iterate through the collection of worksheets in a workbook as such:
foreach (IXLWorksheet worksheet in workbook.Worksheets)
{
Console.WriteLine(worksheet.Name); // outputs the current worksheet name.
// do the thing you want to do on each individual worksheet.
}
You can find this information in visual studio by hitting F12 on your workbook object, you'll see all of the public methods/variables you're given access too. IXLWorksheet and IXLWorksheets is what you're looking for.

How can I get the Range of filtered rows using Excel Interop?

I'm using Excel Interop assemblies for my project,
if I want to use auto filter with then thats possible using
sheet.UsedRange.AutoFilter(1,SheetNames[1],Microsoft.Office.Interop.Excel.XlAutoFilterOperator.xlAnd,oMissing,false)
but how can I get the filtered rows ??
can anyone have idea??
Once you filtered the range, you can access the cells that pass the filter criteria by making use of the Range.SpecialCells method, passing in a valued of 'Excel.XlCellType.xlCellTypeVisible' in order to get the visible cells.
Based on your example code, above, accessing the visible cells should look something like this:
Excel.Range visibleCells = sheet.UsedRange.SpecialCells(
Excel.XlCellType.xlCellTypeVisible,
Type.Missing)
From there you can either access each cell in the visible range, via the 'Range.Cells' collection, or access each row, by first accessing the areas via the 'Range.Areas' collection and then iterating each row within the 'Rows' collection for each area. For example:
foreach (Excel.Range area in visibleCells.Areas)
{
foreach (Excel.Range row in area.Rows)
{
// Process each un-filtered, visible row here.
}
}
Hope this helps!
Mike
I used as mentioned below, similar to what Mike told,
foreach (Excel.Range area in visibleCells.Areas)
{
foreach(Excel.Range row in area.Rows)
{
int index = row.Row; // now index is the present Row index within the range
string test = Mysheet.Cells[index,4].Values //// Mysheet is my present working sheet. After this test will contain the values pointing to the values.cells[index,4]
}
}

Categories

Resources