gsimardnet / PowerQueryNet

Run M Language (Power Query Formula Language) from anywhere.
MIT License
189 stars 32 forks source link

Commas not working as expected in CSV output #17

Open amks1 opened 3 years ago

amks1 commented 3 years ago

I've attached a screenshot of the returned CSV file to show the issue:

image

These are supposed to be a single column having URLs pointing to a geo coordinate. After looking through your code I understood the issue: when there are commas within quotes, it does not work and the entire string breaks.

 public static string ToDelimitedFile(this DataTable dataTable, char delimiter, bool inQuote)
        {
            StringBuilder content = new StringBuilder();

            string lastColumn = dataTable.Columns[dataTable.Columns.Count - 1].ColumnName;
            foreach (DataColumn dc in dataTable.Columns)
            {
                content.Append(dc.ColumnName);
                if (dc.ColumnName != lastColumn)
                    content.Append(delimiter);
            }

            int i = 0;
            foreach (DataRow dr in dataTable.Rows)
            {
                content.Append(Environment.NewLine);

                foreach (DataColumn dc in dataTable.Columns)
                {
                    if (inQuote)
                        content.Append('"' + dr[dc.ColumnName].ToString() + '"');
                    else
                        content.Append(dr[dc.ColumnName].ToString());

                    if (dc.ColumnName != lastColumn)
                        content.Append(delimiter);
                }
            }

            return content.ToString();
        }
gsimardnet commented 3 years ago

You are right, this is a bug I will have to review.