Skip to content

Commit

Permalink
Fixing Excel Copy and Paste issue with colors
Browse files Browse the repository at this point in the history
  • Loading branch information
RNoeldner committed Jun 2, 2024
1 parent dbeb9ab commit b5695d4
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 48 deletions.
33 changes: 23 additions & 10 deletions Library/ClassLibraryCSV/ErrorInformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* If not, see http://www.gnu.org/licenses/ .
*
*/

#nullable enable
using System;
using System.Collections.Generic;
Expand All @@ -29,9 +30,11 @@ public static class ErrorInformation
/// Char to separate two or more errors and warnings
/// </summary>
public const char cSeparator = '\n';

private const char cOpenField = '[';
private const char cClosingField = ']';
private const char cAlternateColumnMessageSeparator = ':';

/// <summary>
/// Char to separate two column names
/// </summary>
Expand Down Expand Up @@ -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
Expand All @@ -88,6 +91,7 @@ public static string AddMessage(this string errorList, in string newError, bool

return sb.ToString();
}

/// <summary>
/// String method to append a message an error list text
/// </summary>
Expand All @@ -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
Expand Down Expand Up @@ -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}";
}
Expand All @@ -175,7 +179,8 @@ public static string GetErrorInformation(this DataRow row)
var list = new List<ColumnAndMessage>();
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);
}

Expand Down Expand Up @@ -228,6 +233,7 @@ public static ColumnAndMessage GetErrorsAndWarnings(this string errorList)

return new ColumnAndMessage(sbErrors.ToString(), sbWaring.ToString());
}

/// <summary>
/// String method to check if the text should be regarded as an error in an error list text
/// </summary>
Expand Down Expand Up @@ -363,6 +369,7 @@ private static string BuildList(in IEnumerable<ColumnAndMessage> errorList)
errors.Append(cSeparator);
errors.Append(CombineColumnAndError(entry.Column, entry.Message));
}

return errors.ToString();
}

Expand All @@ -386,6 +393,7 @@ private static IEnumerable<ColumnAndMessage> ParseList(string errorList)
}
}


/// <summary>
/// Struct for Column and Message information
/// </summary>
Expand Down Expand Up @@ -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());
*/
}
}
}
78 changes: 42 additions & 36 deletions Library/ClassLibraryCSV/HTMLStyle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* If not, see http://www.gnu.org/licenses/ .
*
*/

#nullable enable

using Newtonsoft.Json;
Expand All @@ -31,24 +32,30 @@ public sealed class HtmlStyle
{
/// <summary>Default HtmlStyle</summary>
public static readonly HtmlStyle Default = new HtmlStyle(cDefaultStyle);
private string m_Style;

private const string cDefaultStyle = "<STYLE type=\"text/css\">\r\n"
+ " html * { font-family:'Calibri','Trebuchet MS', Arial, Helvetica, sans-serif; }\r\n"
+ " h1 { style=\"color:DarkBlue; font-size : 14px; }\r\n"
+ " h2 { style=\"color:DarkBlue; font-size : 13px; }\r\n"
+ " table { border-collapse:collapse; font-size : 11px; }\r\n"
+ " td { border: 1px solid lightgrey; padding:2px; }\r\n"
+ " td.info { mso-number-format:\\@; background: #f0f8ff; font-weight:bold;}\r\n"
+ " td.inforight { mso-number-format:\\@; text-align:right; background: #f0f8ff; font-weight:bold;}\r\n"
+ " td.value { text-align:right; color:DarkBlue; }\r\n"
+ " td.text { mso-number-format:\\@; color:black; }\r\n"
+ " tr.alt { background: #EEEEEE; }\r\n"
+ " br { mso-data-placement:same-cell; }\r\n"
+ " span { background: #F7F8E0; }\r\n"
+ " span.err { color:#B40404; }\r\n"
+ " span.war { color:#2E64FE; }\r\n"
private string m_Style;
private const string cSecColor = "DarkBlue";
private const string cBackCol = "#c8c8c8";

private const string cDefaultStyle = "<STYLE type=\"text/css\">"
+ "html * {font-family:'Calibri','Trebuchet MS',Arial,Helvetica,sans-serif}\n"
+ "h1 {color:" + cSecColor + ";font-size:14px;font-weight:bold}\n"
+ "h2 {color:" + cSecColor + ";font-size:13px;font-weight:bold}\n"
+ "table {border-collapse:collapse;font-size:11px}\n"
+ "td {border:1px solid lightgrey;padding:2px}\n"
+ "td.info {mso-number-format:\\@;background:" + cBackCol +
";font-weight:bold}\n"
+ "td.inforight {mso-number-format:\\@;text-align:right;background:" +
cBackCol + ";font-weight:bold}\n"
+ "td.value {text-align:right;color:" + cSecColor + "}\n"
+ "td.text {mso-number-format:\\@}\n"
+ "tr.alt {background:#eeeeee}\n"
+ "br {mso-data-placement:same-cell}\n"
+ "span {background:#f7f8e0}\n"
+ "span.err {color:#b40404}\n"
+ "span.war {color:#2e64fe}\n"
+ "</STYLE>";

/// <summary>
/// Initializes a new instance of the <see cref="HtmlStyle" /> class.
/// </summary>
Expand Down Expand Up @@ -105,7 +112,6 @@ public string Style
/// <value>The table template.</value>
public static string TableClose => "</table>";


/// <summary>
/// Gets or sets the table template.
/// </summary>
Expand Down Expand Up @@ -151,7 +157,6 @@ public string Style
/// <value>The TR template.</value>
public static string TrOpen => "<tr>\r\n";


/// <summary>
/// Gets or sets the TR template alternate.
/// </summary>
Expand Down Expand Up @@ -186,7 +191,7 @@ public string Style
/// <param name="contents">The contents.</param>
/// <returns></returns>
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;
Expand Down Expand Up @@ -296,7 +301,7 @@ public static string HtmlEncode(string text)
}

return sb.ToString();
}
}

/// <summary>
/// Get the JSON element / variable name
Expand Down Expand Up @@ -326,11 +331,12 @@ public static string JsonElementName(string text)

return allowed;
}
/// <summary>
/// Get a valid HTML document string builder that stats with common HTML tags
/// </summary>
/// <param name="hexColor">Background color in hex</param>
/// <returns></returns>

/// <summary>
/// Get a valid HTML document string builder that stats with common HTML tags
/// </summary>
/// <param name="hexColor">Background color in hex</param>
/// <returns></returns>
public StringBuilder StartHtmlDoc(string hexColor = "")
{
var text = new StringBuilder(500);
Expand All @@ -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("<br>").Replace('\t', ' ').Replace(" ", " ").Replace(" ", " ");
}
}

/// <summary>
/// Get the XML element name
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -466,14 +475,12 @@ public static string XmlElementName(string text)
/// <remarks>The HTML format is found here http://msdn2.microsoft.com/en-us/library/aa767917.aspx</remarks>
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 $"<HTML><HEAD>{Style}</HEAD><BODY>{fragment}</BODY></HTML>";
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,
Expand All @@ -490,7 +497,6 @@ public string ConvertToHtmlFragment(string fragment)
prefixLength + html.Length,
startFragment,
endFragment,
source,
html);
}
}
Expand Down
5 changes: 3 additions & 2 deletions Library/WinFormControls/DataGridViewCopyPaste.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ private static bool HasRowErrors(int topRow, int bottomRow, DataGridViewRowColle
/// <param name="appendTab">if set to <c>true</c> [append tab].</param>
/// <param name="addErrorInfo">if set to <c>true</c> [add error info].</param>
/// <param name="cutLength">Maximum length of the resulting text</param>
private void AddCell(
private static void AddCell(
DataGridViewCell cell,
StringBuilder stringBuilder,
StringBuilder sbHtml,
Expand Down Expand Up @@ -167,7 +167,8 @@ private static bool HasRowErrors(int topRow, int bottomRow, DataGridViewRowColle
/// <param name="sbHtml">The StringBuilder for HTML.</param>
/// <param name="errorText">The error Text</param>
/// <param name="addErrorInfo">if set to <c>true</c> [add error info].</param>
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;
Expand Down

0 comments on commit b5695d4

Please sign in to comment.