I have seen many posts about finding the last row of a given column for Google Sheets API v4 in C#, but I can't seem to find anything about finding the last column of a given row. I didn't find any questions about this specifically - but if I'm mistaken please direct me to the right place.
In my sheet, I have headers for each column. Over time, I anticipate I will need to add or remove columns as needed - it would be great to not have to update my code every time this happens.
I'm at the beginning stages of writing my code that gathers my data from Google Sheets - but here is what I have so far. I know that I will need to change the way my variable "range" is written, just don't know what.
static void ReadEntries()
{
var range = $"{sheet}!A1:ET";
var request = service.Spreadsheets.Values.Get(SpreadsheetId, range);
var response = request.Execute();
var values = response.Values;
if(values != null && values.Count>0)
{
foreach (var row in values)
{
System.Diagnostics.Debug.WriteLine("{0} | {1} | {2}", row[0], row[1], row[2]);
}
}
else
{
System.Diagnostics.Debug.WriteLine("No data found.");
}
}
EDIT: SOLVED
I used the pseudo code provided by Nazi A for this. I was having issues with the if(row[col]) piece with casting and other system exceptions. It turns out foreach allows for us to not have to check if that row[col] is in range. Below is my final code in case anyone needs it in the future. I plan to let column "ET" declared in var range = $"{sheet}!A1:ET; be big enough to accommodate any future columns being added to my spreadsheet. Thanks for your help!
static void ReadEntries()
{
var range = $"{sheet}!A1:ET";
var request = service.Spreadsheets.Values.Get(SpreadsheetId, range);
var response = request.Execute();
var values = response.Values;
int max = 0;
int currMax;
if (values != null && values.Count>0)
{
foreach(var row in values)
{
currMax = 0;
foreach(var col in row)
{
currMax++;
}
if (max < currMax)
{
max = currMax;
}
}
}
else
{
System.Diagnostics.Debug.WriteLine("No data found.");
}
System.Diagnostics.Debug.WriteLine(max);
}
So basically, you need to have a nested loop to traverse all rows and columns in values.
I have tested this psuedo code and worked but since I have no any means to run a C# code, this is all I can give to you. This is a pseudo code that should be readable to you.
var max = 0;
foreach(var row in values){
var currMax = 0;
foreach(var col in row){
if(row[col]){ // as long as data exists, currMax will increment
currMax++;
continue;
}
break; // stop loop if last cell being checked is empty
}
if(max < currMax){ // assign the largest currMax to max
max = currMax;
}
}
So in this psuedo code, max will contain the value of the largest column of all rows in the range. this code above should replace your foreach call
If you have any questions, feel free to clarify below.
Related
I'm trying to check whether a worksheet has any filters (whether applied or not), if so, I'd like to figure out which cells or row have a filter function.
I've tried:
var filter = ((dynamic) range.AutoFilter(rowStart, j);
and this one to see if there are any in the whole worksheet
var range = workbookWorksheet.UsedRange;
var sheet= application.ActiveWindow.ActiveSheet;
Range filteredRange = range.SpecialCells(XlCellType.xlCellTypeVisible, XlSpecialCellsValue.xlTextValues);
var sdsds = filteredRange.AutoFilter();
Trying to do this without Plus or another library other than Interop. Not sure if this is possible but any help will be greatly appreciated.
You can use the the AutoFilter property to check if there are filters in the Worksheet.
In order to get the addresses, you need to iterate over the filters.
Use the On property of the filter to check if it's on/off.
// check if there are filters in the worksheet.
if (workbookWorksheet.AutoFilter == null ||
workbookWorksheet.AutoFilter.Filters.Count == 0)
{
// no filters
return;
}
// go over the filters and get the addresses
for (var i = 1; i < ws.AutoFilter.Filters.Count; i++)
{
var filter = ws.AutoFilter.Filters[i];
Console.WriteLine(filter.On); // print/check if the filter is on.
Range range = ws.AutoFilter.Range[1, i];
Console.WriteLine(range.Address); // address of the filter cell
}
Update:
Not sure it's the most efficient way, but it does the job.
This functions takes range as the parameter and check if it has a filter and returns true if the filter is on.
Note: If you prefer to work with absolute address, change the parameter to string address.
private bool HasFilterOn(Range range)
{
// check if there are filters in the worksheet.
if (workbookWorksheet.AutoFilter == null ||
workbookWorksheet.AutoFilter.Filters.Count == 0)
{
// no filters
return false;
}
// go over the filters and get the addresses
for (var i = 1; i < workbookWorksheet.AutoFilter.Filters.Count; i++)
{
var filter = workbookWorksheet.AutoFilter.Filters[i];
if (workbookWorksheet.AutoFilter.Range[1, i].Address == range.Address)
return filter.On;
}
return false;
}
One of +60 columns I pass to Excel is a hyperlink
private object GetApplicationUrl(int id)
{
var environmentUri = _configurationManager.Default.AppSettings["EnvironmentUri"];
return $"=HYPERLINK(\"{environmentUri}://TradingObject={id}/\", \"{id}\");";
}
this resolves external protocol and opens particular deal in another application. This works, but initially, the hyperlink formula is unevaluated, forcing the users to evaluate it first. I use object array:
protected void SetRange(object[,] range, int rows, int columns, ExcelCell start = null)
{
start = start ?? GetCurrentCell();
ExcelAsyncUtil.QueueAsMacro(
() =>
{
var data = new ExcelReference(start.Row, start.Row + (rows - 1), start.Column, start.Column + (columns - 1));
data.SetValue(range);
});
}
How can I force the excel to evaluate it?
I tried several things, but they did not work. For example:
return XlCall.Excel(
XlCall.xlcCalculateNow,
$"HYPERLINK(\"{environmentUri}://TradingObject={id}/\", \"{id}\")");
maybe I am using an incorrect flag or missing something.
I found out several ways to fix it, but not all of them worked as I expected. In short, I came up with calling the cell formula value instead.
private void SetFormulaColumn(List<int> ids, ExcelCell cell)
{
var wSheet = ((Application)ExcelDnaUtil.Application).ActiveWorkbook.ActiveSheet;
for (var i = 0; i < ids.Count; i++)
{
wSheet.Cells(cell.Row + i, cell.Column).Formula = GetApplicationUrl(ids[i]);
}
}
It is probably slightly less performant but works well enough to be considered as a valid solution.
what I'm trying to do: I have a large datatable, and I'm going through a list of strings where some of them are in the datatable and some aren't. I need to make a list of those that are, and count those that aren't.
This is my code part:
DataRow[] foundRows;
foundRows = DTgesamt.Select("SAP_NR like '%"+SAP+"%'");
if (AreAllCellsEmpty(foundRows[0]) == false && !(foundRows[0]==null))
{
list.Add(SAP);
}
else
{
notfound++;
}
public static bool AreAllCellsEmpty(DataRow row)
{
if (row == null) throw new ArgumentNullException("row");
for (int i = row.Table.Columns.Count - 1; i >= 0; i--)
{
if (!row.IsNull(i))
{
return false;
}
}
return true;
}
DTgesamt ist a large DataTable. "SAP" is a string that is in the first column of the DataTable, but not all of them are included. I want to count the unfound ones with the int "notfound".
The problem is, the Select returns an empty DataRow {System.Data.DataRow[0]} when it finds nothing.
I'm getting the errormessage Index out of array area.
The two statements in the if-clause are what I read on the internet but they don't work. With only the 2nd statement it just adds all numbers to the list, with the first it still gives this error.
Thanks for any help :)
check count of items in foundRows array to avoid IndexOutOfRange exception
foundRows = DTgesamt.Select("SAP_NR like '%"+SAP+"%'");
if (foundRows.Length > 0 && AreAllCellsEmpty(foundRows[0])==false)
list.Add(SAP);
else
notfound++;
The found cells cannot be empty. Your select statement would be wrong. So what you actually need is:
if (DTgesamt.Select("SAP_NR like '%"+SAP+"%'").Any())
{
list.Add(SAP);
}
else
{
notfound++;
}
You probably don't even need the counter, when you can calculate the missed records based on how many SAP numbers you had and how many results you got in list.
If you have an original list or array of SAP numbers, you could shorten your whole loop to:
var numbersInTable = originalNumbers.Where(sap => DTgesamt.Select("SAP_NR like '%"+sap+"%'").Any()).ToList();
var notFound = originalNumbers.Count - numbersInTable.Count;
I need some help with a for each statement, basically what happens is when a user edits a value within a cell, my foreach will apply this to all cells within the datagrid and change the value of them all, i need my foreach statement to work by iterating through the datagrid but only change the selected row that has been edited
try
{
//loop through each of the rows in the dgv
foreach (DataGridViewRow row in dgvDetials.SelectedRows)
{
int intQtyInsp = 0;
//if quantity inspected is empty:
if (row.Cells[2].Value.ToString() == "")
{
//quantity inspected is 0. Prevents null value errors:
intQtyInsp = 0;
}
intQtyInsp =
Int32.Parse(dgvDetials.CurrentRow.Cells[2].Value.ToString());
if (intQtyInsp < 0) // checks the cell for a negative value
{
intQtyInsp = 0; // if cells is negative submits value as Zero
}
else
{
//sets quantity inspected to value entered
intQtyInsp = Int32.Parse(row.Cells[2].Value.ToString());
}
if (intQtyInsp == 0) //if quantity inspected is 0. Ignore row.
{
}
else //else gather details and insert row as production.
{
area = dtArea2.Rows[0]["area_code"].ToString();
inspDate = dtpInspectionDate.Value.ToString("MM/dd/yyyy");
inspShift = cbShift.Text;
partNo = row.Cells[0].Value.ToString();
// dieCode = row.Cells[0].Value.ToString();
dieCode = "";
machine = "";
qtyInsp = intQtyInsp;
qtyInspRecorded = Int32.Parse(row.Cells[5].Value.ToString());
comment = "";
//machine = row.Cells[3].Value.ToString();
if (qtyInspRecorded == 0)
{
SQLMethods.insertProduction(area,
inspDate,
inspShift,
partNo,
dieCode,
qtyInsp,
comment,
machine);
}
else
{
SQLMethods.updateProduction(area,
inspDate,
inspShift,
partNo,
dieCode,
(qtyInspRecorded + qtyInsp),
comment,
machine);
}
}
}
retrieveData(); //reset values
}
catch (Exception ex)
{
MessageBox.Show(
"Error instering production values. Processed with error: "
+ ex.Message);
}
First of all, I would simplify the code here a little by splitting it into several methods that may be called from the For-loop. That would make it easier to read, and thereby easier to help you too. Just to provide an example, the following:
if (intQtyInsp < 0) // checks the cell for a negative value
{
intQtyInsp = 0; // if cells is negative submits value as Zero
}
else
{
//sets quantity inspected to value entered
intQtyInsp = Int32.Parse(row.Cells[2].Value.ToString());
}
could be replaced with something like:
int intQtyInsp = SetQuantityInspected();
Then that method could contain the if-structure. Repeat this for other parts of the code in the loop too. Trust me, this will make your life easier.
Also, it seems as if the result of this section is never used; the value of intQtyInsp is overwritten right afterwards!:
if (row.Cells[2].Value.ToString() == "")
{
//quantity inspected is 0. Prevents null value errors:
intQtyInsp = 0;
}
As for your question: I'm not sure how you would get the id of the row that is currently being edited. (If possible (?), it might be getter to loop through the table / data source behind the datagrid?).
In any case, what you need to do is something like the following inside your loop:
if(IsCurrentlyEditedRow(row)){
...
// (all the stuff currently in the body of your loop goes here)
...
}
Now you can implement the method IsCurrentlyEditedRow() to return True or False depending on whether or not the id of the current row is the the same as that of the one you are editing.
Sorry if this is not a very specific and detailed answer, hope it is of some use anyway.
I am currently using one button for inserting/updating content within a table. It then takes the uploaded CSV and inserts or updates it into a data table depending on whether the row exists or not.
Here is the code fired after the button's OnClick:
if (ExcelDDL.SelectedValue == "Time Points" && fileName == "TimePoints.csv")
{
var GetTPoints = (SEPTA_DS.TimePointsTBLDataTable)tpta.GetDataByCategory(CategoryDDL.SelectedItem.ToString());
//Loop through each row and insert into database
int i = 0;
foreach (DataRow row in TempRouteDataTable.Rows)
{
//Gather column headers
var category = Convert.ToString(CategoryDDL.SelectedItem);
var agency = Convert.ToString(row["Agency"]);
if (agency == null || agency == "")
{
//If row is empty skip it entirely
goto skipped;
}
var route = Convert.ToString(row["Route"]);
var GetRShortName = (SEPTA_DS.RoutesTBLDataTable)rta.GetDataByRouteID(route);
var newRoute = "";
if (GetRShortName.Rows.Count > 0)
{
newRoute = Convert.ToString(GetRShortName.Rows[0]["route_short_name"]);
}
var direction = Convert.ToString(row["Direction"]);
var serviceKey = Convert.ToString(row["Service Key"]);
var language = Convert.ToString(row["Language"]);
var stopID = Convert.ToString(row["Stop ID"]);
var stopName = Convert.ToString(row["Stop Name"]);
if (stopName.Contains("accessible"))
{
string[] noHTML = stopName.Split('>');
int insertH = Convert.ToInt32(hta.InsertHandicapRow(newRoute,noHTML[2]));
}
var sequence = Convert.ToString(row["Sequence"]);
var origID = -1;
if (GetTPoints.Rows.Count > 0)
{
origID = Convert.ToInt32(GetTPoints.Rows[i]["TPointsID"]);
var GetID = (SEPTA_DS.TimePointsTBLDataTable)tpta.GetDataByID(origID);
if (GetID.Rows.Count < 1)
{
origID = -1;
}
}
if (origID == -1)
{
int insertData = Convert.ToInt32(tpta.InsertTimePoints(category, agency, newRoute, direction, serviceKey, language, stopID, stopName, sequence));
}
else
{
int updateData = Convert.ToInt32(tpta.UpdateTimePoints(category, agency, newRoute, direction, serviceKey, language, stopID, stopName, sequence, origID));
}
skipped:
i++;
}
}
You can see how I check whether to insert or update around the bottom. I am using this method across other sections of this program and it works just fine. But in this case it is distorting my datatable immensely and I can't figure out why.
This is the bottom part of my table after inserting [no items currently within the database]:
This is the table after reuploading the CSV with data already existing within the table:
I am also getting this error when updating There is no row at position 2230.
What is going wrong in the code to cause this huge shift? I am just checking to see if the ID exists and if it does update rather than insert.
Also the reason i am using goto is because there are blank rows in the document that need to be skipped.
Is your TPointsID column, a auto-generated number? If so, since you are skipping the empty row, some referential integrity problem might be occuring,because of empty data in the skipped rows in the database.
From the error : There is no row at position 2230 , it is also understood that, because of the skipping you might be trying to access some non existent row in the datatable.
Also, if possible consider using the ADO.NET DataAdapter which has got the CRUD operation capability. You can find more about it at : http://support.microsoft.com/kb/308507