I need help in determining the real range of my spreadsheet as I don't know the last row number as the data as it gets imported from an external source. I am creating a small program to find if a certain columns have a "null" value and send the details to Slack. Once I reach an empty row, I need to terminate the code. Attaching a small screenshot. enter image description here
Now, I have tried creating a range variable but the script runs till the end and sends the message on Slack even though there is no data in the last rows. I need it to stop once it reaches the end of the data, like 29th row in the screenshot. Below is my code -
public static void ReadEntries()
{
var range = $ "{Sheet}!A3:AW28";
SpreadsheetsResource.ValuesResource.GetRequest request =
_service.Spreadsheets.Values.Get(SpreadsheetId, range);
var response = request.Execute();
IList < IList < object >> values = response.Values;
if (values != null && values.Count > 0) {
foreach(var row in values) {
var n = 0;
if (row[n].ToString().Trim() == null) {
break;
}
//var rowCount = worksheet.Dimension.End.Row;
//Console.WriteLine("{0} | {1} | {2} | {3}", row[45], row[46], row[47], row[48]);
//if (row[n] == "-")
for (int i = 0; i < 49; i++)
//else if (row[n].ToString().Trim()== "null" && n<49)
{
var client1 = new RestClient("<slack webhook>");
client1.Timeout = -1;
var request1 = new RestRequest(Method.POST);
var body = "<message_payload>";
request1.AddParameter("application/json", body, ParameterType.RequestBody);
//IRestResponse response1 = client1.Execute(request1);
if (row[i].ToString().Trim() == "null") {
Console.WriteLine("OK" + i);
} else if (row[i].ToString().Trim() == "") {
Console.WriteLine("Its Over" + i);
}
//Thread.Sleep(1000);
//n++;
}
}
} else {
Console.WriteLine("No data found.");
}
Now, I have tried creating a range variable but the script runs till the end and sends the message on Slack even though there is no data in the last rows. I need it to stop once it reaches the end of the data, like 29th row in the screenshot. I have tried searching for an answer and found a line that can find the last row and probably the real range of my spreadsheet, but it does not work for some reason -
var rowCount = worksheet.Dimension.End.Row;
I have also tried to identify a blank column by converting the response cell to string like this -
if (row[n].ToString().Trim() == null)
{
break;
}
But I don't think it is working correctly. I am really sorry if I did not explain it properly, english is not my first language but I will try to share more details if needed.
if (string.IsNullOrEmpty(row[n]?.ToString().Trim()))
{
break;
or
string.IsNullOrWhiteSpace
Try like that
Is this what you seek: (not sure)
function myfunk() {
const sh = SpreadsheetApp.getActiveSheet();
const rg = `${sh.getName()}!${sh.getDataRange().getA1Notation()}`;
Logger.log(rg);
return rg;
}
Related
I'm writing a program that uses a fingerprint reader. I have stored the fingerprint data in an array [arr]. Unfortunately, only the first value is read i.e [0]. So only one finger is detected and the rest are ignored but if I place a specific number in the array e.g 2. It works fine for that value alone:
Here's my code:
for (int x = 0; x < (arr.Length - 1); x++)
{
byte[] fpbyte = GetStringToBytes(arr[x]);
Stream stream = new MemoryStream(fpbyte);
Data.Templates[x] = new DPFP.Template(stream);
}
foreach (DPFP.Template template in Data.Templates)
{
// Get template from storage.
if (template != null)
{
// Compare feature set with particular template.
ver.Verify(FeatureSet, template, ref res);
Data.IsFeatureSetMatched = res.Verified;
Data.FalseAcceptRate = res.FARAchieved;
if (res.Verified)
MessageBox.Show("Yes");
break; // success
}
}
if (!res.Verified)
Status = DPFP.Gui.EventHandlerStatus.Failure;
MessageBox.Show("No");
Data.Update();
You unconditionally break from your loop, whether verified or not.
Your code should read :
if (res.Verified) {
MessageBox.Show("Yes");
break; // success
}
This is a good example why good coding practice suggests always having the brackets, even for a one line conditional effect, as the error would have been much more obvious.
Similarly you should have written
if (!res.Verified) {
Status = DPFP.Gui.EventHandlerStatus.Failure;
MessageBox.Show("No");
}
at the end of your snippet.
Thanks to Dragonthoughts, I made the following changes and the code works just fine:
for (int x = 0; x < (arr.Length - 1); x++)
{
byte[] fpbyte = GetStringToBytes(arr[x]);
using (Stream stream = new MemoryStream(fpbyte))
{
Data.Templates[x] = new DPFP.Template(stream);
// Get template from storage.
if (Data.Templates[x] != null)
{
// Compare feature set with particular template.
ver.Verify(FeatureSet, Data.Templates[x], ref res);
Data.IsFeatureSetMatched = res.Verified;
Data.FalseAcceptRate = res.FARAchieved;
if (res.Verified)
{
status.Text = "Verified";
break; // success
}
}
}
}
if (!res.Verified)
{
Status = DPFP.Gui.EventHandlerStatus.Failure;
status.Text = "Unverified";
}
Data.Update();
I have a text file that is divided up into many sections, each about 10 or so lines long. I'm reading in the file using File.ReadAllLines into an array, one line per element of the array, and I'm then I'm trying to parse each section of the file to bring back just some of the data. I'm storing the results in a list, and hoping to export the list to csv ultimately.
My for loop is giving me trouble, as it loops through the right amount of times, but only pulls the data from the first section of the text file each time rather than pulling the data from the first section and then moving on and pulling the data from the next section. I'm sure I'm doing something wrong either in my for loop or for each loop. Any clues to help me solve this would be much appreciated! Thanks
David
My code so far:
namespace ParseAndExport
{
class Program
{
static readonly string sourcefile = #"Path";
static void Main(string[] args)
{
string[] readInLines = File.ReadAllLines(sourcefile);
int counter = 0;
int holderCPStart = counter + 3;//Changed Paths will be an different number of lines each time, but will always start 3 lines after the startDiv
/*Need to find the start of the section and the end of the section and parse the bit in between.
* Also need to identify the blank line that occurs in each section as it is essentially a divider too.*/
int startDiv = Array.FindIndex(readInLines, counter, hyphens72);
int blankLine = Array.FindIndex(readInLines, startDiv, emptyElement);
int endDiv = Array.FindIndex(readInLines, counter + 1, hyphens72);
List<string> results = new List<string>();
//Test to see if FindIndexes work. Results should be 0, 7, 9 for 1st section of sourcefile
/*Console.WriteLine(startDiv);
Console.WriteLine(blankLine);
Console.WriteLine(endDiv);*/
//Check how long the file is so that for testing we know how long the while loop should run for
//Console.WriteLine(readInLines.Length);
//sourcefile has 5255 lines (elements) in the array
for (int i = 0; i <= readInLines.Length; i++)
{
if (i == startDiv)
{
results = (readInLines[i + 1].Split('|').Select(p => p.Trim()).ToList());
string holderCP = string.Join(Environment.NewLine, readInLines, holderCPStart, (blankLine - holderCPStart - 1)).Trim();
results.Add(holderCP);
string comment = string.Join(" ", readInLines, blankLine + 1, (endDiv - (blankLine + 1)));//in case the comment is more than one line long
results.Add(comment);
i = i + 1;
}
else
{
i = i + 1;
}
foreach (string result in results)
{
Console.WriteLine(result);
}
//csvcontent.AppendLine("Revision Number, Author, Date, Time, Count of Lines, Changed Paths, Comments");
/* foreach (string result in results)
{
for (int x = 0; x <= results.Count(); x++)
{
StringBuilder csvcontent = new StringBuilder();
csvcontent.AppendLine(results[x] + "," + results[x + 1] + "," + results[x + 2] + "," + results[x + 3] + "," + results[x + 4] + "," + results[x + 5]);
x = x + 6;
string csvpath = #"addressforcsvfile";
File.AppendAllText(csvpath, csvcontent.ToString());
}
}*/
}
Console.ReadKey();
}
private static bool hyphens72(String h)
{
if (h == "------------------------------------------------------------------------")
{
return true;
}
else
{
return false;
}
}
private static bool emptyElement(String ee)
{
if (ee == "")
{
return true;
}
else
{
return false;
}
}
}
}
It looks like you are trying to grab all of the lines in a file that are not "------" and put them into a list of strings.
You can try this:
var lineswithoutdashes = readInLines.Where(x => x != hyphens72).Select(x => x).ToList();
Now you can take this list and do the split with a '|' to extract the fields you wanted
The logic seems wrong. There are issues with the code in itself also. I am unsure what precisely you're trying to do. Anyway, a few hints that I hope will help:
The if (i == startDiv) checks to see if I equals startDiv. I assume the logic that happens when this condition is met, is what you refer to as "pulls the data from the first section". That's correct, given you only run this code when I equals startDiv.
You increase the counter I inside the for loop, which in itself also increases the counter i.
If the issue in 2. wouldn't exists then I'd suggest to not do the same operation "i = i + 1" in both the true and false conditions of the if (i == startDiv).
Given I assume this file might actually be massive, it's probably a good idea to not store it in memory, but just read the file line by line and process line by line. There's currently no obvious reason why you'd want to consume this amount of memory, unless it's because of the convenience of this API "File.ReadAllLines(sourcefile)". I wouldn't be too scared to read the file like this:
Try (BufferedReader br = new BufferedReader(new FileReader (file))) {
String line;
while ((line = br.readLine()) != null) {
// process the line.
}
}
You can skip the lines until you've passed where the line equals hyphens72.
Then for each line, you process the line with the code you provided in the true case of (i == startDiv), or at least, from what you described, this is what I assume you are trying to do.
int startDiv will return the line number that contains hyphens72.
So your current for loop will only copy to results for the single line that matches the calculated line number.
I guess you want to search the postion of startDiv in the current line?
const string hyphens72;
// loop over lines
for (var lineNumber = 0; lineNumber <= readInLines.Length; lineNumber++) {
string currentLine = readInLines[lineNumber];
int startDiv = currentLine.IndexOf(hyphens72);
// loop over characters in line
for (var charIndex = 0; charIndex < currentLine.Length; charIndex++) {
if (charIndex == startDiv) {
var currentCharacter = currentLine[charIndex];
// write to result ...
}
else {
continue; // skip this character
}
}
}
There are a several things which could be improved.
I would use ReadLines over File.ReadAllLines( because ReadAllLines reads all the lines at ones. ReadLines will stream it.
With the line results = (readInLines[i + 1].Split('|').Select(p => p.Trim()).ToList()); you're overwriting the previous results list. You'd better use results.AddRange() to add new results.
for (int i = 0; i <= readInLines.Length; i++) means when the length = 10 it will do 11 iterations. (1 too many) (remove the =)
Array.FindIndex(readInLines, counter, hyphens72); will do a scan. On large files it will take ages to completely read them and search in it. Try to touch a single line only ones.
I cannot test what you are doing, but here's a hint:
IEnumerable<string> readInLines = File.ReadLines(sourcefile);
bool started = false;
List<string> results = new List<string>();
foreach(var line in readInLines)
{
// skip empty lines
if(emptyElement(line))
continue;
// when dashes are found, flip a boolean to activate the reading mode.
if(hyphens72(line))
{
// flip state.. (start/end)
started != started;
}
if(started)
{
// I don't know what you are doing here precisely, do what you gotta do. ;-)
results.AddRange((line.Split('|').Select(p => p.Trim()).ToList()));
string holderCP = string.Join(Environment.NewLine, readInLines, holderCPStart, (blankLine - holderCPStart - 1)).Trim();
results.Add(holderCP);
string comment = string.Join(" ", readInLines, blankLine + 1, (endDiv - (blankLine + 1)));//in case the comment is more than one line long
results.Add(comment);
}
}
foreach (string result in results)
{
Console.WriteLine(result);
}
You might want to start with a class like this. I don't know whether each section begins with a row of hyphens, or if it's just in between. This should handle either scenario.
What this is going to do is take your giant list of strings (the lines in the file) and break it into chunks - each chunk is a set of lines (10 or so lines, according to your OP.)
The reason is that it's unnecessarily complicated to try to read the file, looking for the hyphens, and process the contents of the file at the same time. Instead, one class takes the input and breaks it into chunks. That's all it does.
Another class might read the file and pass its contents to this class to break them up. Then the output is the individual chunks of text.
Another class can then process those individual sections of 10 or so lines without having to worry about hyphens or what separates on chunk from another.
Now that each of these classes is doing its own thing, it's easier to write unit tests for each of them separately. You can test that your "processing" class receives an array of 10 or so lines and does whatever it's supposed to do with them.
public class TextSectionsParser
{
private readonly string _delimiter;
public TextSectionsParser(string delimiter)
{
_delimiter = delimiter;
}
public IEnumerable<IEnumerable<string>> ParseSections(IEnumerable<string> lines)
{
var result = new List<List<string>>();
var currentList = new List<string>();
foreach (var line in lines)
{
if (line == _delimiter)
{
if(currentList.Any())
result.Add(currentList);
currentList = new List<string>();
}
else
{
currentList.Add(line);
}
}
if (currentList.Any() && !result.Contains(currentList))
{
result.Add(currentList);
}
return result;
}
}
I am trying to parse a column (File is composed of only 1 column filled with double numbers.) in a .csv file but C# throws me error when it encounters an empty cell.
{"Input string was not in a correct format."}
I want program to continue with the next cell when this happens. Is there a way?
Note: I tried
if(array[i] != null)
but this does not seem to work.
I use this block to read from .csv:
var column = new List<string>();
using (var rd = new StreamReader(#"pathofthecsvfile"))
{
while (!rd.EndOfStream)
{
var splits = rd.ReadLine().Split(';');
column.Add(splits[0]);
}
}
string[] arr = column.ToArray();
double[] array = new double[arr.Length];
//problem is in this block
for (int i = 0; i < array.Length; i++)
{
if (arr[i] != null)
{
array[i] = Convert.ToDouble(arr[i]);
}
}
I would check it when inserting:
if split[0] != null && split[0] != "" {
column.Add(splits[0]);
}
You are not validating the data you are loading into the array to see if it is in a valid format - Convert.ToDouble will fail for a number of reasons other than just null value and empty string.
If you are happy just to skip to next column, use a TryParse instead in the for loop:
double.TryParse(arr[i], out array[i]);
I've recently finished a small project that I have been working on, except there is a small problem that I'm trying to figure out.
This project utilizes C# web services using a SOAP client along with both the Request and Response Classes. This project has been designed to save inputted data. What it does is that it reserves a seat with a row in the cinema, and when I book that seat and try to select that seat again to find another available seat in the row (within the form), it displays them both as "0". However, when I book another seat in a different row, it successfully saves that data in the cache but it won't display any other seat and row within the "else if" statement of the code.
Here is the Webmethod for the Webservice
public string Name { get; set; } //gets and sets the Name from the ReserveSeatResponse
public int Row { get; set; } //gets and sets the Row from the ReserveSeatResponse
public int Seat { get; set; } //gets and sets the Seat from the ReserveSeatResponse
private const int maxRows = 13; //sets the max amount of Rows in the Array tried setting the rows to 12, but since the array starts at 0, I had to set the Rows to 13)
private const int maxSeats = 17; //sets the max amount of Seats in the Array (tried setting the seats at 16, but since the array starts at 0, I had to set it to 17)
private bool[,]reservedSeats = new bool[13, 17]; //same description above, but this sets a boolean in the reserved seats, to check if its taken or no)
private bool[,]reservedRows = new bool[13, 17]; //same description for the max row/seats.
[WebMethod]
public GetSeatResponse Booking (GetSeatRequest req)
{
GetSeatResponse resp = new GetSeatResponse();
// resp.Seat = req.SeatNumber;
// resp.Row = req.RowNumber;
object abc = HttpContext.Current.Cache["CinemaReservation"];
//if the cache does not exist
if (abc == null)
{
//creates a blank multidimensional array
reservedSeats = new bool[maxRows,maxSeats];
}
//if the cache exists
else
{
//using the cache object as an array
try
{
reservedSeats = (bool[,])abc;
}
catch
{
}
}
//if the seat is vacant
if (reservedSeats[req.RowNumber, req.SeatNumber] != true)
{
resp.Name = req.Name;
resp.Seat = req.SeatNumber;
resp.Row = req.RowNumber;
reservedSeats[req.RowNumber, req.SeatNumber] = true;
HttpContext.Current.Cache["CinemaReservation"] = reservedSeats;
return resp;
}
//if the seat is taken
else if (reservedSeats[req.RowNumber, req.SeatNumber] != true)
{
bool breakTest = false;
string Name = req.Name;
int row = req.RowNumber;
int seat = req.SeatNumber;
for (int i = row; i < reservedSeats.GetLength(0); i++)
{
for (int j = seat; j < reservedSeats.GetLength(1); j++)
{
if (reservedSeats[i, j] !=true )
{
resp.Name = Name;
resp.Row = i+1;
resp.Seat = j+1;
reservedSeats[i, j] = true;
breakTest = true;
break;
}
}
seat = 0;
if (breakTest == true)
{
break;
}
}
//getNextSeat(req, resp, reservedSeats);
HttpContext.Current.Cache["CinemaReservation"] = reservedSeats;
}
return resp;
And here is the part where I am stuck on in the method that calls from the Webservice
if (resp.erroresp != true)
{
if ((resp.Row > 0) && (resp.Seat > 0))
{
MessageBox.Show(String.Format("Hi there {0}!!, you have reserved a Seat at Row: {1} in Seat Number : {2}", resp.Name, resp.Row, resp.Seat));
}
else if (resp.erroresp != true)
{
if ((resp.Row < 1) && (resp.Seat < 1))
{
MessageBox.Show(String.Format("Sorry, your selected Seat has been taken, however there is an available seat at row {0} in seat {1}", resp.Row, resp.Seat));
}
}
}
I see a few problems with your code.
Serverside:
reservedRows is never used. Whats its purpose?
You define constants i.e. maxRows but you don't use them. Write
private bool[,]reservedSeats = new bool[maxRows, maxSeats]
In both checks (Seat taken or not) you use the same condition - that wont work. Just write:
if (!reservedSeats[req.RowNumber, req.SeatNumber]) {
// Seat is free
} else {
// Seat is taken
}
If you want to differentiate whether the returned seat is an alternative, you should include a boolean flag i your response or even better an enumeration like this:
public enum ResultEnum {
Ok, // the requested seat is free
Full, // no seat is available
Alternative // you got another seat
}
Clientside:
Your checks are somewhat redundant (you check two times for resp.erroresp != true) and not clear at all.
When using the approach with an enum you can check like this:
switch (resp.Result) {
case ResultEnum.Ok:
break;
case ResultEnum.Full:
break;
case ResultEnum.Alternative:
break;
}
I spot 2 errors in ur code!
Firstly, I couldn't see the initialization for your 2-dimensional array after:
`if (abc == null)
{
//creates a blank multidimensional array
reservedSeats = new bool[maxRows,maxSeats];
}`
Secondly, your if else conditions (reservedSeats[req.RowNumber, req.SeatNumber] != true) are exactly the same!
Hope this helps
I need to add functionality in my program so that any file imported it will find the text within the "" of the addTestingPageContentText method as seen below. The two values on each line will then be added to a datagridview which has 2 columns so first text in first column then second in the 2nd column. How would i go about Finding the "sometext" ?
addTestingPageContentText("Sometext", "Sometext");
addTestingPageContentText("Sometext2", "Sometext2");
... continues n number of times.
Neither fast nor efficient, but it's easier to understand for those new to regular expressions:
while (!endOfFile)
{
//get the next line of the file
string line = file.readLine();
EDIT: //Trim WhiteSpaces at start
line = line.Trim();
//check for your string
if (line.StartsWith("addTestingPageContentText"))
{
int start1;
int start2;
//get the first something by finding a "
for (start1 = 0; start1 < line.Length; start1++)
{
if (line.Substring(start1, 1) == '"'.ToString())
{
start1++;
break;
}
}
//get the end of the first something
for (start2 = start1; start2 < line.Length; start2++)
{
if (line.Substring(start2, 1) == '"'.ToString())
{
start2--;
break;
}
}
string sometext1 = line.Substring(start1, start2 - start1);
//get the second something by finding a "
for (start1 = start2 + 2; start1 < line.Length; start1++)
{
if (line.Substring(start1, 1) == '"'.ToString())
{
start1++;
break;
}
}
//get the end of the second something
for (start2 = start1; start2 < line.Length; start2++)
{
if (line.Substring(start2, 1) == '"'.ToString())
{
start2--;
break;
}
}
string sometext2 = line.Substring(start1, start2 - start1);
}
}
However I would seriously recommend going through some of the great tutorials out there on the internet. This is quite a good one
The expression "\"[^"]*\"" would find each...