Using C# .Net Google Sheets API.
I am new to the API, so I may have missed it in the docs - but how do you find out the maximum row and column that contain a value without reading all the data in the sheet?
For example, if a sheet contains multiple values and the "last" cell in the sheet with a value is at C139 (no cells in the rows following have a value and no cells in any column after C have a value), then the maximum row would be 139 and the maximum column would be 2 (zero based) or 3 (one based).
I tried sheet.Properties.GridProperties.RowCount -- but that gives the TOTAL number of rows in the sheet (whether the cells have values or not).
Same goes for sheet.Properties.GridProperties.ColumnCount -- gives the TOTAL number of columns in the sheet (whether the cells have values or not).
Any links or ideas are welcome.
I understand that you want to know the last row of data in your Sheet. In that case, you can use a simple GET with a full range. For example let's assume that your Sheet only has two columns, in that case you can set up the range like A1:B. That range will include the full two columns, but the get will only get as far as the data goes. At this step you already have an array filled with your data range, so you only have to count the array index of the last element in order to know the last row value. If you don't know how many columns your Sheet have, you only have to modify the range in a similar way as before (i.e. A1:Z). Please ask me any doubts about this approach.
Related
I've got a problem with my .UsedRange.Count in my code.
I try to count the used rows in my Worksheet to define a Range from the first Value row of the Sheet to the last. For this I need to get the used Rows in my Worksheet and I try to do it with
this.worksheet.UsedRange.Count
But the Result is to big. My Worksheet just got 140 Rows and .UsedRange.Count is counting something above 100.000
Also
this.worksheet.Rows.Count
won´t work. The result is something near 100.000
Solved the Problem:
However this.worksheet.UsedRange.Count is multiplying the Rows with the Columns (counts all cells that were used)
With this.worksheet.UsedRange.Rows.Count i just get the Row Count.
I'm reading an .xlsx spreadsheet into a C# console app with a view to outputting the content as a formatted xml file (to be picked up by another part of the system further down the line).
The problem with the the .xslx file is that it's a pro-forma input document based on, and replacing, an old paper-based order form we used to provide to customers, and the input fields aren't organised as a series of similar rows (except in the lower part of the document which consists of up to 99 rows of order detail lines). Some of the rows in the header part of the form/sheet are a mixture of label text AND data; same with the columns.
Effectively, what I need to do is to be able to cherry pick data from the initial dozen or so rows in order to poke data into the xml structure; the latter part of the document I can process by iterating over the rows for the order detail lines.
I can't use Interop as this will end up as an Azure function - so I've used ExcelDataReader to convert the spreadsheet to a dataset, then convert that dataset to a new dataset entirely composed of string values. But I haven't been able to successfully point to individual cells as I had expected to be using syntax something like
var cellValue = MyDataSet.Cell[10, 2];
I'd be grateful for any advice as to how I might get the result I need.
A Dataset has Tables and those have Rows which hold ColumnValues
A WorkSheet transforms into a Table (with Columns) and the Cells transform to Rows and column values.
To find the cell value at [10,2] on the first Worksheet do:
var cellValue = MyDataSet.Tables[0].Rows[10][2];
Remember that cellValue will be of type object. Cast accordingly.
I've 2 excel sheets(Sheet1 & Sheet2) in my Excel workbook. I want to copy row data from Sheet2 to Sheet1.
Condition is:
if Sheet2 copied row doesn't exist in Sheet1 then paste it otherwise don't paste the row.
Copied Rows except 1st row in Sheet2:
Range dataWithoutFirstRow = xlAccrualSheet.Range[xlAccrualSheet.UsedRange.Cells[2, 1],
xlAccrualSheet.UsedRange.SpecialCells(XlCellType.xlCellTypeLastCell)];
dataWithoutFirstRow.Copy();
Paste in below used range in Sheet1:
Range DataRange = xlAccrualWorkSheet.Cells[emptycell, 1];
DataRange.PasteSpecial(XlPasteType.xlPasteAllUsingSourceTheme);
Please Tell me How to check already exist rows in Sheet1.
Awaiting for Your Response
Please Tell me How to check already exist rows in Sheet1.
Use Range to read out all data from the workSheet. You are already doing a similar thing accrualSheet. Then, you can use range.Cells(i,j) or range.Rows(r).Value or even range.Rows(r).Cells(i,j) to get the data inside each specific row.
When pasting occurs, loop over all pasted rows, and for each pasted row compare it with rows from workSheet. You may do it directly by reading that on the fly (as mentioned above), or you may read all rows from workSheet and store them in a List and compare incoming rows against that list - it will work much faster that way.
Now, one of the most interesting things is probably what does it mean to "compare rows". Either you will need to compare all cells within a row with another one, or you will need to compare just a specific set of "columns" like "date, time, cause, origin, caseId" etc. But that.. noone knows, you said nothing about that. If there's no info on that, then you probably should compare whole rows and assume that any difference in any cell means that the rows are different.
I've read a lot of where people have missing cell data due to null values but my issue is I have spreadsheets I'm parsing that have exactly 272 rows of data needing to be parsed. When I get the row count I'm only able to retrieve 269. Below is how I'm getting the row count.
// code above gets worksheet...
var rows = from row in worksheet.Descendants<Row>()
where row.RowIndex >= rowIndex
select row;
for (int i = 0; i <= rows.Count(); i++)
{
// processing...
}
The rowIndex above is used because I'm only pulling 272 rows after a few heading rows and it needs to be dynamic as some sheets have multiple heading rows. Basically I search for a particular cell value for a string and then from there get the rowIndex and use that to get the row count.
I've tracked down the two rows not being picked up and do not have values in them, but there are other rows exactly the same that do not have values and the rows are included. It is important as all sheets I need to parse have 272 rows of specific data with blank rows in the same spot. One sheet will work fine and include the rows while another won't.
So I'm trying to determine why some blank rows are included but others are totally ignored like they do not exist.
Any help would be appreciated.
The rows that don't have cells that have values should not be included as a rows in the total count. This makes sense since excel does not store row or cell information where the cell value is null.
The other rows that don't have any values in them but do show up most likely have a space in a cell somewhere. A space is a valid value for a cell and while it looks empty, it will be stored as a cell value and count towards the total number of rows.
oSheet = (Excel._Worksheet)xlWorkBook.ActiveSheet;
oRng = oSheet.get_Range("T10", "T343");
The range oRng contains values of type double. Each cell in Column T shows the max number of the associated row. How can i find out how many 1’s , 2’s 3’s ….. till 10 are in that range. Secondly Eg if there are 20 rows with value =3 , I need to copy column A,B,C from those rows and store them for later use .i need the count of the number of rows for each value from 1 to 10
Here are a few general observations that might be enough to get you going:
Excel.Range has an AutoFilter method that you might be able to employ successively for each value that you're interested in (i.e, 1 through 10). Once you have the individual ranges returned by AutoFilter, you can them query them for the specific information you're interested in. See C# Excel Automation: Retrieving rows after AutoFilter() with SpecialCells() does not seem to work properly for issues associated with this approach.
Alternatively, you might be able to do something like create a simple dictionary that you could then populate as you iterate over column T. For example, the dictionary could be of type Dictionary>.
As you proceed through column T, you encounter a value in each cell. If the cell value hasn't been seen before, you add it as a new key to the Dictionary. For the associated value in the dictionary's key/value pair, you create a new List with the corresponding row number as its first element.
If the cell value has been seen before, you look it up in the dictionary, then add the corresponding row to the List associated with that key.
At the end of the day, your dictionary's keys contains all the values found in column T. The number of rows associated with each value is just the number of elements in the associated List. With the row values in the List, you can then find "A[row value]", "B[row value]" and "C[row value]".