Example #1
0
        public String Get_Line_Content(String InputFile, int LineNumber)
        {
            CsvUtils cu = new CsvUtils();

            cu.SetFile(InputFile);
            return(cu.Get_Line_Content(LineNumber));
        }
        // Tested - Switch Columns
        public Boolean Switch_Columns(String InputFile, String ColumnName1, String ColumnName2)
        {
            CsvUtils cu = new CsvUtils();

            cu.SetFile(InputFile);
            IDictionary <int, List <String> > NewDict = new Dictionary <int, List <String> >();
            int ColumnIndex1 = cu.Get_Column_Index(ColumnName1);
            int ColumnIndex2 = cu.Get_Column_Index(ColumnName2);

            if (ColumnIndex1 > -1 && ColumnIndex2 > -1)
            {
                foreach (KeyValuePair <int, List <String> > entry in cu.dict)
                {
                    String MyLine = cu.Get_Line_Content(entry.Key);

                    cu.Swap(entry.Value, ColumnIndex1, ColumnIndex2);

                    // entry.Value.RemoveAt(ColumnIndex);
                }
                cu.Save_File_As_CSV(InputFile);
            }
            else
            {
                return(false);
            }
            return(true);
        }
Example #3
0
        // Kepp only Lines that match a regular expression
        public int Keep_Line_If_Cell_Matches_Pattern(String InputFile, String ColumnName, String RegExPattern)
        {
            int      CntLinesKept = 0;
            CsvUtils cu           = new CsvUtils();

            cu.SetFile(InputFile);
            IDictionary <int, List <String> > NewDict = new Dictionary <int, List <String> >();
            int ColumnIndex = cu.Get_Column_Index(ColumnName);

            if (ColumnIndex > -1)
            {
                foreach (KeyValuePair <int, List <String> > entry in cu.dict)
                {
                    String MyLine = cu.Get_Line_Content(entry.Key);
                    //Console.WriteLine("Debug Line: " + MyLine);
                    String CellValueToCheck = entry.Value[ColumnIndex];
                    //Console.WriteLine("Checking Value: " + CellValueToCheck + " against Regex: " + @RegExPattern);
                    var pattern = @RegExPattern;
                    var matches = Regex.Matches(CellValueToCheck, pattern);
                    if (entry.Key == 0) // if First Line, then keep it regardless of match or no match
                    {
                        NewDict.Add(entry);
                    }
                    else
                    {
                        if (matches.Count == 0)
                        {
                        }
                        else
                        {
                            NewDict.Add(entry);
                            CntLinesKept++;
                            // Console.WriteLine("Match Found, Keeping Line.");
                        }
                        //Console.ReadKey();
                    }
                }
                cu.dict = NewDict;
                cu.Save_File_As_CSV(InputFile);
            }

            return(CntLinesKept);
        }
        // Tested - Delete an entire Line if a particular cell of a given column matches a regular expression pattern
        public int Delete_Column(String InputFile, String ColumnName)
        {
            CsvUtils cu = new CsvUtils();

            cu.SetFile(InputFile);
            IDictionary <int, List <String> > NewDict = new Dictionary <int, List <String> >();
            int ColumnIndex = cu.Get_Column_Index(ColumnName);

            if (ColumnIndex > -1)
            {
                foreach (KeyValuePair <int, List <String> > entry in cu.dict)
                {
                    String MyLine = cu.Get_Line_Content(entry.Key);
                    entry.Value.RemoveAt(ColumnIndex);
                }
                cu.Save_File_As_CSV(InputFile);
            }
            return(ColumnIndex);
        }
        // Append a string to the content of a Cell IF a certain corresponding Cell in the same column or a different column matches a Regex (ex: if "Description" contains "Credit" then append a "-" to column "Amount")
        public int Append_If_Column_Matches_Pattern(String InputFile, String ColumnNameToMatch, String RegExPattern, String ColumnNameToModify, String StringToAppend, Boolean AppendAtEnd)
        {
            CsvUtils cu = new CsvUtils();

            cu.SetFile(InputFile);
            int CntMods             = 0;
            int ColumnIndex         = cu.Get_Column_Index(ColumnNameToMatch);
            int ColumnIndexToModify = cu.Get_Column_Index(ColumnNameToModify);

            if (ColumnIndex < 0 || ColumnIndexToModify < 0)
            {
                return(-1);
            }

            foreach (KeyValuePair <int, List <String> > entry in cu.dict)
            {
                //Console.WriteLine("Debug Column Index | Name: " + ColumnIndex + ":" + ColumnName);
                String MyLine           = cu.Get_Line_Content(entry.Key);
                String CellValueToCheck = entry.Value[ColumnIndex];
                var    pattern          = @RegExPattern;
                var    matches          = Regex.Matches(CellValueToCheck, pattern);
                if (entry.Key > 0 && matches.Count != 0) // not first line AND match
                {
                    String OriginalValueOfCell = entry.Value[ColumnIndexToModify];
                    String NewValueCell        = "";
                    if (OriginalValueOfCell.StartsWith("\""))
                    {
                        NewValueCell = cu.ReplaceFirst(OriginalValueOfCell, "\"", "\"" + StringToAppend);
                    }
                    else
                    {
                        NewValueCell = StringToAppend + OriginalValueOfCell;
                    }
                    entry.Value[ColumnIndexToModify] = NewValueCell;
                    CntMods++;
                }
            }
            cu.Save_File_As_CSV(InputFile);
            return(CntMods);
        }