Hi Everyone! I’m still pretty new to Unity3D, coding, and scripting in general, so please excuse what might be a n00b question. So, I’m trying to parse large amounts of information held in an Excel spreadsheet. 613 entries to be exact. I’m doing it like so:
void Start ()
{
// Make sure there this a text
// file assigned before continuing
if (textFile != null) {
// Add each line of the text file to
// the array using the new line
// as the delimiter
textData = (textFile.text.Split ('\n'));
}
print ("the array is " + textData.Length + " characters long");
}
This works great, except the problem is that when I initially get the information there is a much of stuff before the numbers I want to get, i.e “2013-11-5;” So, for example the information in the cell will be something like “2013-11-5; 8000”, where 8000 is the only thing I’m looking for.
So my question, how would I parse through the text, only grabbing the number AFTER the " ; " symbol? Currently, I’m going through the spreadsheet, deleting the access information, but that is ridiculous and a total pain in the ass. Thank you in advance. Any help is appreciated.
So, there are several .net libraries out their you can use to read and parse excel spreadsheets.
I don’t remember what the format of excel spreadsheets are, I do know that they’re basically an exapansion on the csv format (to support macros and pages and other stuff).
I also know that excel can save simple spreadsheets (with out macros and links and what not) as a csv file.
Csv files are really easy to parse. Csv stands for ‘comma separated values’. And really all it is is that each row is separated by a return line, and every column is separated by a ,. You can create a jagged array of these values like so:
//validate your txtfile exists and what not before this code
var rows = txtFile.text.Split('\n');
var data = new string[rows.Length] {};
for(int i = 0; i < data.Length; i++)
{
data[i] = rows[i].Split(',');
}
In that, if you wanted to access the 3rd row, second column’s value, you’d do something like:
(array’s use 0-based indices… 0,1,2… 2 is the 3rd entry)
Thanks for the reply. I’m confused by the 4th line of the code however (I’m doing this in C#). Right now I have:
if (textFile != null) {
// Add each line of the text file to
// the array using the new line
// as the delimiter
textData = (textFile.text.Split ('\n'));
data = textData.Length;
for(int i = 0; i < data.Length; i++){
data[i] = textData[i].Split(',');
}
So, where you have “row” I have textData. How do I correctly assign the data string? Sorry… this is so n00b.