1 00:00:00,510 --> 00:00:04,290 I've been keeping track of soccer game results in a Google spreadsheet. 2 00:00:04,290 --> 00:00:07,530 We can use this data to do some analysis. 3 00:00:07,530 --> 00:00:11,400 Here we've got a date and time the match was held, team name, 4 00:00:11,400 --> 00:00:13,960 whether it was a home or an away game. 5 00:00:13,960 --> 00:00:18,740 Number of goals, goal attempts, shots on goal, shots off goal, and 6 00:00:18,740 --> 00:00:20,422 a possession percentage. 7 00:00:20,422 --> 00:00:24,080 I wanna get this data into our console application. 8 00:00:24,080 --> 00:00:27,830 First we'll need to get this spreadsheet into a file on our hard drive. 9 00:00:27,830 --> 00:00:31,520 In Google Sheets we can download a spreadsheet in various formats with 10 00:00:31,520 --> 00:00:35,220 File > Download as. 11 00:00:35,220 --> 00:00:40,390 And we'll choose Comma-separated values, also known as a CSV file. 12 00:00:40,390 --> 00:00:42,390 This is good for us because it's a text file. 13 00:00:43,400 --> 00:00:46,946 You can get this CSV file in the project files links in 14 00:00:46,946 --> 00:00:49,695 the download section below this video. 15 00:00:49,695 --> 00:00:52,761 Now we'll need to add it to our solution. 16 00:00:52,761 --> 00:00:57,551 Right-click on the project and choose Add > Existing Item. 17 00:00:59,697 --> 00:01:03,427 Then we'll need to go to our Downloads folder, and 18 00:01:03,427 --> 00:01:07,280 we'll need to change this to All Files. 19 00:01:07,280 --> 00:01:08,547 There it is. 20 00:01:08,547 --> 00:01:13,300 I wanna rename this, take off this Sheet1 here. 21 00:01:15,010 --> 00:01:17,939 Okay, let's open it up to see what it looks like. 22 00:01:19,960 --> 00:01:24,010 Here at the top we've got the names of each of our columns in the spreadsheet, 23 00:01:24,010 --> 00:01:26,850 and then the lines after that we've got our data. 24 00:01:26,850 --> 00:01:30,230 Each value from the spreadsheet is separated by a comma. 25 00:01:30,230 --> 00:01:31,660 We need to open this file and 26 00:01:31,660 --> 00:01:35,040 read the data in order to get it into our application. 27 00:01:35,040 --> 00:01:38,622 Just like we did with the text file, we can use a stream reader. 28 00:01:38,622 --> 00:01:44,027 We also need to change the Copy to Output Directory property to Copy if newer. 29 00:01:47,058 --> 00:01:51,975 Back in our Program class let's collapse the Main method, and 30 00:01:51,975 --> 00:01:54,210 we'll create a new method. 31 00:01:54,210 --> 00:01:57,121 We'll call it ReadFile, and it'll return a string. 32 00:01:57,121 --> 00:02:03,040 Public static string ReadFile. 33 00:02:03,040 --> 00:02:06,341 And we'll give it a string parameter for the file name. 34 00:02:06,341 --> 00:02:11,416 FileName, open close curly brace. 35 00:02:11,416 --> 00:02:15,803 Then inside this method we'll use the stream reader to load up the file and 36 00:02:15,803 --> 00:02:17,158 return its contents. 37 00:02:17,158 --> 00:02:23,976 Using(var reader = new StreamReader), 38 00:02:23,976 --> 00:02:29,141 and we'll pass it the fileName. 39 00:02:29,141 --> 00:02:31,948 Then a using block. 40 00:02:31,948 --> 00:02:38,824 Then we'll return reader.ReadToEnd. 41 00:02:38,824 --> 00:02:41,550 That'll return everything inside the text file. 42 00:02:42,840 --> 00:02:45,912 We'll need to get the file name to pass to our method like we did with our 43 00:02:45,912 --> 00:02:47,217 data.text file. 44 00:02:47,217 --> 00:02:52,257 Let's go back up to Main, and we'll delete this stuff here. 45 00:02:55,192 --> 00:03:02,734 We'll change data.txt to SoccerGameResults.csv. 46 00:03:02,734 --> 00:03:09,510 Then we'll create a variable, var fileContents = ReadFile, 47 00:03:09,510 --> 00:03:14,034 and we'll pass it our fileName variable, 48 00:03:17,297 --> 00:03:23,484 And then we'll just write everything to the console, WriteLine(fileContents). 49 00:03:26,529 --> 00:03:27,546 Let's see what it gives us. 50 00:03:27,546 --> 00:03:30,629 I'll run with Ctrl+F5. 51 00:03:30,629 --> 00:03:31,697 There's our data.