From b5695d493bce717b745f7f8971c850ab97d8f048 Mon Sep 17 00:00:00 2001 From: Raphael Noeldner Date: Sun, 2 Jun 2024 10:36:27 +0200 Subject: [PATCH] Fixing Excel Copy and Paste issue with colors --- Library/ClassLibraryCSV/ErrorInformation.cs | 33 +++++--- Library/ClassLibraryCSV/HTMLStyle.cs | 78 ++++++++++--------- .../WinFormControls/DataGridViewCopyPaste.cs | 5 +- 3 files changed, 68 insertions(+), 48 deletions(-) diff --git a/Library/ClassLibraryCSV/ErrorInformation.cs b/Library/ClassLibraryCSV/ErrorInformation.cs index b9f44674..7c02506e 100644 --- a/Library/ClassLibraryCSV/ErrorInformation.cs +++ b/Library/ClassLibraryCSV/ErrorInformation.cs @@ -11,6 +11,7 @@ * If not, see http://www.gnu.org/licenses/ . * */ + #nullable enable using System; using System.Collections.Generic; @@ -29,9 +30,11 @@ public static class ErrorInformation /// Char to separate two or more errors and warnings /// public const char cSeparator = '\n'; + private const char cOpenField = '['; private const char cClosingField = ']'; private const char cAlternateColumnMessageSeparator = ':'; + /// /// Char to separate two column names /// @@ -66,7 +69,7 @@ public static string AddMessage(this string errorList, in string newError, bool if (errorList.Contains(newError)) return errorList; - var sb = new StringBuilder(errorList.Length + newError.Length +1); + var sb = new StringBuilder(errorList.Length + newError.Length + 1); // If the new message is considered an error put it in front, this way it's easier to check if // there is an error @@ -88,6 +91,7 @@ public static string AddMessage(this string errorList, in string newError, bool return sb.ToString(); } + /// /// String method to append a message an error list text /// @@ -111,7 +115,7 @@ public static string AddMessage(this string errorList, in string newError) if (errorList.Contains(newError)) return errorList; - var sb = new StringBuilder(errorList.Length + newError.Length +1); + var sb = new StringBuilder(errorList.Length + newError.Length + 1); // If the new message is considered an error put it in front, this way it's easier to check if // there is an error @@ -160,7 +164,7 @@ public static string CombineColumnAndError(in string column, in string errorMess if (errorMessage is null) throw new ArgumentNullException(nameof(errorMessage)); // pass back messages that already have column information - if (column.Length==0 && errorMessage[0] == cOpenField) + if (column.Length == 0 && errorMessage[0] == cOpenField) return errorMessage; return $"{cOpenField}{column}{cClosingField} {errorMessage}"; } @@ -175,7 +179,8 @@ public static string GetErrorInformation(this DataRow row) var list = new List(); if (!string.IsNullOrEmpty(row.RowError)) list.Add(new ColumnAndMessage(string.Empty, row.RowError)); - list.AddRange(row.GetColumnsInError().Select(col => new ColumnAndMessage(col.ColumnName, row.GetColumnError(col)))); + list.AddRange( + row.GetColumnsInError().Select(col => new ColumnAndMessage(col.ColumnName, row.GetColumnError(col)))); return BuildList(list); } @@ -228,6 +233,7 @@ public static ColumnAndMessage GetErrorsAndWarnings(this string errorList) return new ColumnAndMessage(sbErrors.ToString(), sbWaring.ToString()); } + /// /// String method to check if the text should be regarded as an error in an error list text /// @@ -363,6 +369,7 @@ private static string BuildList(in IEnumerable errorList) errors.Append(cSeparator); errors.Append(CombineColumnAndError(entry.Column, entry.Message)); } + return errors.ToString(); } @@ -386,6 +393,7 @@ private static IEnumerable ParseList(string errorList) } } + /// /// Struct for Column and Message information /// @@ -423,15 +431,20 @@ private static ColumnAndMessage SplitColumnAndMessage(in string text) if (text[0] == cOpenField) { var close = text.IndexOf(cClosingField); - return close == -1 ? - new ColumnAndMessage(string.Empty, text) : - new ColumnAndMessage(text.Substring(1, close - 1), text.Substring(close + 2)); + return close == -1 + ? new ColumnAndMessage(string.Empty, text) + : new ColumnAndMessage(text.Substring(1, close - 1), text.Substring(close + 2)); } + return new ColumnAndMessage(string.Empty, text); + /* not sure anymore why this was added, but it causes problems + when copy past to HTMl in QuickViewer + var splitterAlt = text.IndexOf(cAlternateColumnMessageSeparator); - return splitterAlt == -1 ? - new ColumnAndMessage(string.Empty, text) : - new ColumnAndMessage(text.Substring(0, splitterAlt), text.Substring(splitterAlt + 1).TrimStart()); + return splitterAlt == -1 + ? new ColumnAndMessage(string.Empty, text) + : new ColumnAndMessage(text.Substring(0, splitterAlt), text.Substring(splitterAlt + 1).TrimStart()); + */ } } } \ No newline at end of file diff --git a/Library/ClassLibraryCSV/HTMLStyle.cs b/Library/ClassLibraryCSV/HTMLStyle.cs index 8564676f..507c55d9 100644 --- a/Library/ClassLibraryCSV/HTMLStyle.cs +++ b/Library/ClassLibraryCSV/HTMLStyle.cs @@ -11,6 +11,7 @@ * If not, see http://www.gnu.org/licenses/ . * */ + #nullable enable using Newtonsoft.Json; @@ -31,24 +32,30 @@ public sealed class HtmlStyle { /// Default HtmlStyle public static readonly HtmlStyle Default = new HtmlStyle(cDefaultStyle); - private string m_Style; - private const string cDefaultStyle = ""; + /// /// Initializes a new instance of the class. /// @@ -105,7 +112,6 @@ public string Style /// The table template. public static string TableClose => ""; - /// /// Gets or sets the table template. /// @@ -151,7 +157,6 @@ public string Style /// The TR template. public static string TrOpen => "\r\n"; - /// /// Gets or sets the TR template alternate. /// @@ -186,7 +191,7 @@ public string Style /// The contents. /// public static string AddTd(string? template, - params object?[]? contents) + params object?[]? contents) { if (template is null || contents is null || template.Length == 0) return string.Empty; @@ -296,7 +301,7 @@ public static string HtmlEncode(string text) } return sb.ToString(); - } + } /// /// Get the JSON element / variable name @@ -326,11 +331,12 @@ public static string JsonElementName(string text) return allowed; } -/// -/// Get a valid HTML document string builder that stats with common HTML tags -/// -/// Background color in hex -/// + + /// + /// Get a valid HTML document string builder that stats with common HTML tags + /// + /// Background color in hex + /// public StringBuilder StartHtmlDoc(string hexColor = "") { var text = new StringBuilder(500); @@ -356,7 +362,7 @@ public static string TextToHtmlEncode(in string text) && text.EndsWith("]]>", StringComparison.OrdinalIgnoreCase)) return text.Substring(9, text.Length - 12); return text.HandleCrlfCombinations("
").Replace('\t', ' ').Replace(" ", " ").Replace(" ", " "); - } + } /// /// Get the XML element name @@ -411,13 +417,16 @@ public static string XmlElementName(string text) { if (errorsAndWarnings.Message.Length == 0 && errorsAndWarnings.Column.Length > 0) { - sbHtml.Append(string.Format(CultureInfo.CurrentCulture, tdTemplate, AddTd(Error, errorsAndWarnings.Column))); + sbHtml.Append(string.Format(CultureInfo.CurrentCulture, tdTemplate, + AddTd(Error, errorsAndWarnings.Column))); return; } if (errorsAndWarnings.Message.Length > 0 && errorsAndWarnings.Column.Length == 0) { - sbHtml.Append(string.Format(CultureInfo.CurrentCulture, tdTemplate, AddTd(Warning, errorsAndWarnings.Message))); + sbHtml.Append( + string.Format(CultureInfo.CurrentCulture, tdTemplate, + AddTd(Warning, errorsAndWarnings.Message))); return; } @@ -466,14 +475,12 @@ public static string XmlElementName(string text) /// The HTML format is found here http://msdn2.microsoft.com/en-us/library/aa767917.aspx public string ConvertToHtmlFragment(string fragment) { - // Minimal implementation of HTML clipboard format - const string source = "http://www.csvquickviewer.com/"; - - const string markerBlock = "Version:1.0\r\n" + "StartHTML:{0,8}\r\n" + "EndHTML:{1,8}\r\n" - + "StartFragment:{2,8}\r\n" + "EndFragment:{3,8}\r\n" + "StartSelection:{2,8}\r\n" - + "EndSelection:{3,8}\r\n" + "SourceURL:{4}\r\n" + "{5}"; + // return $"{Style}{fragment}"; + const string markerBlock = + "Version:1.0\r\nStartHTML:{0,8}\r\nEndHTML:{1,8}\r\nStartFragment:{2,8}\r\nEndFragment:{3,8}\r\nStartSelection:{2,8}\r\nEndSelection:{3,8}\r\n{4}"; - var prefixLength = string.Format(CultureInfo.InvariantCulture, markerBlock, 0, 0, 0, 0, source, "").Length; + var prefixLength = string.Format(CultureInfo.InvariantCulture, markerBlock, 0, 0, 0, 0, "") + .Length; var html = string.Format( CultureInfo.InvariantCulture, @@ -490,7 +497,6 @@ public string ConvertToHtmlFragment(string fragment) prefixLength + html.Length, startFragment, endFragment, - source, html); } } diff --git a/Library/WinFormControls/DataGridViewCopyPaste.cs b/Library/WinFormControls/DataGridViewCopyPaste.cs index 1f85c2f3..9accea80 100644 --- a/Library/WinFormControls/DataGridViewCopyPaste.cs +++ b/Library/WinFormControls/DataGridViewCopyPaste.cs @@ -137,7 +137,7 @@ private static bool HasRowErrors(int topRow, int bottomRow, DataGridViewRowColle /// if set to true [append tab]. /// if set to true [add error info]. /// Maximum length of the resulting text - private void AddCell( + private static void AddCell( DataGridViewCell cell, StringBuilder stringBuilder, StringBuilder sbHtml, @@ -167,7 +167,8 @@ private static bool HasRowErrors(int topRow, int bottomRow, DataGridViewRowColle /// The StringBuilder for HTML. /// The error Text /// if set to true [add error info]. - private void AppendRowError(StringBuilder stringBuilder, StringBuilder sbHtml, string? errorText, bool addErrorInfo) + private static void AppendRowError(StringBuilder stringBuilder, StringBuilder sbHtml, string? errorText, + bool addErrorInfo) { if (!addErrorInfo) return;