/// <summary> /// Applies border on top of this border.<br/> /// Where the frame thickness is 0, the underlying border is not updated,<br/> /// otherwise it is over-written with a new border, using the applied colour. /// </summary> /// <param name="rowHeight"></param> public static void UpdateBorder(this ExcelCellBorderInfo borderToUpdate, StyleBase styleToApply) { // Anything to apply? if (styleToApply == null || styleToApply.HasAnyBorder() == false) { return; } // Apply updates to the thickness (ie. any non-zero values) if (styleToApply.HasLeftBorder()) { borderToUpdate.WidthLeft = styleToApply.BorderThickness.Value.Left; borderToUpdate.ColourLeft = styleToApply.BorderColour; } if (styleToApply.HasTopBorder()) { borderToUpdate.WidthTop = styleToApply.BorderThickness.Value.Top; borderToUpdate.ColourTop = styleToApply.BorderColour; } if (styleToApply.HasRightBorder()) { borderToUpdate.WidthRight = styleToApply.BorderThickness.Value.Right; borderToUpdate.ColourRight = styleToApply.BorderColour; } if (styleToApply.HasBottomBorder()) { borderToUpdate.WidthBottom = styleToApply.BorderThickness.Value.Bottom; borderToUpdate.ColourBottom = styleToApply.BorderColour; } }
/// <summary> /// Creates a new style which is based on an existing style, merging over current values. /// </summary> /// <param name="styleToMerge">Style which is to be merged over the base style</param> /// <param name="basedOnStyle">Style on which the new style is based</param> /// <returns></returns> public static CellStyle CreateMergedStyle(StyleBase basedOnStyle, CellStyle styleToMerge) { // First clone var newStyle = (CellStyle)styleToMerge; // Then override newStyle.BackgroundColour = styleToMerge.BackgroundColour.HasValue ? styleToMerge.BackgroundColour : basedOnStyle.BackgroundColour; newStyle.BorderColour = styleToMerge.BorderColour.HasValue ? styleToMerge.BorderColour : basedOnStyle.BorderColour; newStyle.BorderThickness = styleToMerge.BorderThickness.HasValue ? styleToMerge.BorderThickness : basedOnStyle.BorderThickness; // If supplied is style is CellStyle then apply these attributes also if (basedOnStyle is CellStyle) { var typedValueToMerge = basedOnStyle as CellStyle; // using new internal readonly FontColour property which gets a typed verison of the bindable Color? FontColour newStyle.FontColour = styleToMerge.InternalFontColour.HasValue ? styleToMerge.InternalFontColour : typedValueToMerge.InternalFontColour; newStyle.ExcelFormat = !string.IsNullOrEmpty(styleToMerge.ExcelFormat) ? styleToMerge.ExcelFormat : typedValueToMerge.ExcelFormat; newStyle.FontFamily = !string.IsNullOrEmpty(styleToMerge.FontFamily) ? styleToMerge.FontFamily : typedValueToMerge.FontFamily; newStyle.FontSize = styleToMerge.FontSize.HasValue ? styleToMerge.FontSize : typedValueToMerge.FontSize; newStyle.FontWeight = styleToMerge.FontWeight.HasValue ? styleToMerge.FontWeight : typedValueToMerge.FontWeight; newStyle.FontUnderlined = styleToMerge.FontUnderlined.HasValue ? styleToMerge.FontUnderlined : typedValueToMerge.FontUnderlined; newStyle.HorizontalAlignment = styleToMerge.HorizontalAlignment.HasValue ? styleToMerge.HorizontalAlignment : typedValueToMerge.HorizontalAlignment; newStyle.RotationAngle = styleToMerge.RotationAngle.HasValue ? styleToMerge.RotationAngle : typedValueToMerge.RotationAngle; newStyle.TextAlignment = styleToMerge.TextAlignment.HasValue ? styleToMerge.TextAlignment : typedValueToMerge.TextAlignment; newStyle.TextWrapping = styleToMerge.TextWrapping.HasValue ? styleToMerge.TextWrapping : typedValueToMerge.TextWrapping; newStyle.VerticalAlignment = styleToMerge.VerticalAlignment.HasValue ? styleToMerge.VerticalAlignment : typedValueToMerge.VerticalAlignment; } return(newStyle); }
public StyleBase Merge(StyleBase value) { if (!string.IsNullOrEmpty(value.BasedOnKey)) { // Look up mandatory existing style StyleBase baseStyle = this.ResourceStore.GetResourceByKey <StyleBase>(value.BasedOnKey); StyleBase newStyle; // And update with properties of the supplied style if (value is Style) { if (baseStyle is Style) { // ExcelMapStyle based on ExcelMapStyle newStyle = Style.CreateMergedStyle(baseStyle, (Style)value); } else if (baseStyle is CellStyle) { // ExcelMapStyle can't be based on a ExcelCellMapStyle throw new InvalidOperationException(string.Format("Can't base ExcelMapStyle '{0}' on ExcelCellMapStyle '{1}' (not yet anyway)", value.Key, baseStyle.Key)); } else { // Whoops.. not an accounted for style throw new InvalidOperationException(string.Format("Style Key='{0}' BasedOnStyle is not valid style", baseStyle.Key)); } } else if (value is CellStyle) { if (baseStyle is Style) { // ExcelCellMapStyle based on ExcelMapStyle newStyle = CellStyle.CreateMergedStyle(baseStyle, (CellStyle)value); } else if (baseStyle is CellStyle) { // Straight Copy & Merge newStyle = CellStyle.CreateMergedStyle(baseStyle, (CellStyle)value); } else { // Whoops.. not an accounted for style throw new InvalidOperationException(string.Format("Style Key='{0}' BasedOnStyle is not valid style", baseStyle.Key)); } } else { // Whoops.. not an accounted for style throw new InvalidOperationException(string.Format("Style Key='{0}' is not valid style", value.Key)); } return(newStyle); } return(value); }
/// <summary> /// Adds a style to an internal list of styles /// </summary> /// <param name="style">A <see cref="StyleBase"/> derived style</param> internal void AddStyle(StyleBase style) { if (style != null) { if (this.styles == null) { this.styles = new List <StyleBase>(); } this.styles.Add(style); } }
/// <summary> /// Creates a new style which is based on an existing style, merging over current values. /// </summary> /// <param name="basedOnStyle">Style on which the new style is based</param> /// <param name="styleToMerge">Style which is to be berged over the base style</param> /// <returns></returns> public static Style CreateMergedStyle(StyleBase basedOnStyle, Style styleToMerge) { // First clone var newStyle = (Style)styleToMerge.Clone(); // Then override newStyle.BackgroundColour = styleToMerge.BackgroundColour.HasValue ? styleToMerge.BackgroundColour : basedOnStyle.BackgroundColour; newStyle.BorderColour = styleToMerge.BorderColour.HasValue ? styleToMerge.BorderColour : basedOnStyle.BorderColour; newStyle.BorderThickness = styleToMerge.BorderThickness.HasValue ? styleToMerge.BorderThickness : basedOnStyle.BorderThickness; return(newStyle); }
/// <summary> /// Compares and updates the number format for the cell /// </summary> private void UpdateNumberFormat(StyleBase mapStyle) { var typedMapStyle = mapStyle as CellStyle; if (typedMapStyle != null) { //Only applies colour if exists, is not transparent and is not already set. if (string.IsNullOrEmpty(this.numberFormat) && !string.IsNullOrEmpty(typedMapStyle.ExcelFormat)) { this.numberFormat = typedMapStyle.ExcelFormat; this.hasCellInfo = true; } } }
/// <summary> /// Compares and updates the fill colour for the cell /// </summary> private void UpdateFillColour(StyleBase mapStyle) { if (mapStyle != null) { //Only applies colour if exists, is not transparent and is not already set. if (this.fillColour == null || this.fillColour.HasValue == false || this.fillColour.Value == Colors.Transparent) { if (mapStyle.BackgroundColour != null && mapStyle.BackgroundColour.HasValue && mapStyle.BackgroundColour.Value != Colors.Transparent) { this.fillColour = mapStyle.BackgroundColour; this.hasCellInfo = true; } } } }
/// <summary> /// Checks the <see cref="StyleBase"/> BorderColour and BorderThickness values to determine if there is any /// border information to be applied to the <see cref="StyleBase"/>. /// </summary> /// <param name="mapStyle"></param> /// <returns></returns> public static bool HasRightBorder(this StyleBase mapStyle) { //if (mapStyle == null) return false; if (mapStyle.BorderThickness == null || !mapStyle.BorderThickness.HasValue) { return(false); } if (mapStyle.BorderColour == null || !mapStyle.BorderColour.HasValue || mapStyle.BorderColour.Value == Colors.Transparent) { return(false); } return(mapStyle.BorderThickness.Value.Right > 0); }
/// <summary> /// Creates a <see cref="ExcelCellBorderInfo"/> from the border colour and thickness specified in a <see cref="StyleBase"/> derived class instance /// which has uniform border thickness and border colour. /// NB! This is intended to raise an error if either border colour or thickness is null/not specified. /// </summary> /// <param name="mapStyle"></param> /// <returns></returns> private static ExcelCellBorderInfo CreateBorderInfo(StyleBase mapStyle) { var borderInfo = new ExcelCellBorderInfo(); borderInfo.ColourLeft = mapStyle.BorderColour.Value; borderInfo.ColourTop = mapStyle.BorderColour.Value; borderInfo.ColourRight = mapStyle.BorderColour.Value; borderInfo.ColourBottom = mapStyle.BorderColour.Value; borderInfo.WidthLeft = mapStyle.BorderThickness.Value.Left; borderInfo.WidthTop = mapStyle.BorderThickness.Value.Top; borderInfo.WidthRight = mapStyle.BorderThickness.Value.Right; borderInfo.WidthBottom = mapStyle.BorderThickness.Value.Bottom; return(borderInfo); }
/// <summary> /// Updates this <see cref="ExcelCellStyleInfo"/> with <see cref="StyleBase">Map Style</see> properties. /// </summary> /// <param name="exportStyle"></param> private void UpdateCellStyle(StyleBase style) { // Update fill colour (if not already set) this.UpdateFillColour(style); // Update border (this is layered) this.UpdateBorderInfo(style); // Update font (if not already set) this.UpdateFontInfo(style); // Update alignment (if not already set) this.UpdateAlignmentInfo(style); // Update the excel number format (if not already set) this.UpdateNumberFormat(style); }
/// <summary> /// Compares and updates border thickness for the cell /// </summary> /// <param name="rowHeight"></param> private void UpdateBorderInfo(StyleBase mapStyle) { // Anything to apply? if (mapStyle != null && mapStyle.HasAnyBorder()) { if (this.borderInfo == null) { // Clone the border and apply to the cell this.borderInfo = CreateBorderInfo(mapStyle); this.hasCellInfo = true; } else { // Update the border with layered information this.borderInfo.UpdateBorder(mapStyle); } } }
/// <summary> /// Compares and updates font information for the cell. /// </summary> /// <param name="font"></param> private void UpdateFontInfo(StyleBase mapStyle) { // Only applies to CellMapStyles CellStyle typedMapStyle = mapStyle as CellStyle; if (typedMapStyle != null && HasAnyFontInfo(typedMapStyle)) { if (this.fontInfo == null) { this.fontInfo = new ExcelCellFontInfo(); } // Update any font-related properties that are non-null/empty if (!string.IsNullOrEmpty(typedMapStyle.FontFamily)) { this.fontInfo.FontFamily = typedMapStyle.FontFamily; } if (typedMapStyle.FontSize.HasValue) { this.fontInfo.FontSize = typedMapStyle.FontSize.Value; } if (typedMapStyle.FontWeight.HasValue) { this.fontInfo.FontWeight = typedMapStyle.FontWeight.Value; } if (typedMapStyle.FontUnderlined.HasValue) { this.fontInfo.FontUnderlined = typedMapStyle.FontUnderlined.Value; } if (typedMapStyle.InternalFontColour.HasValue) { this.fontInfo.FontColour = typedMapStyle.InternalFontColour.Value; } this.hasCellInfo = true; } }
/// <summary> /// Checks the <see cref="StyleBase"/> BorderColour and BorderThickness values to determine if there is any /// border information to be applied to the <see cref="StyleBase"/>. /// </summary> /// <param name="mapStyle"></param> /// <returns></returns> public static bool HasAnyBorder(this StyleBase mapStyle) { //if (mapStyle == null) return false; if (mapStyle.BorderThickness == null || !mapStyle.BorderThickness.HasValue) { return(false); } if (mapStyle.BorderColour == null || !mapStyle.BorderColour.HasValue || mapStyle.BorderColour.Value == Colors.Transparent) { return(false); } if (mapStyle.BorderThickness.Value.Left == 0 && mapStyle.BorderThickness.Value.Top == 0 && mapStyle.BorderThickness.Value.Right == 0 && mapStyle.BorderThickness.Value.Bottom == 0) { return(false); } return(true); }
/// <summary> /// Compares and updates alignment information for the cell. /// </summary> /// <param name="font"></param> private void UpdateAlignmentInfo(StyleBase mapStyle) { // Only applies to CellMapStyles CellStyle typedMapStyle = mapStyle as CellStyle; if (typedMapStyle != null && HasAnyAlignmentInfo(typedMapStyle)) { if (this.alignmentInfo == null) { this.alignmentInfo = new ExcelCellAlignmentInfo(); } // Update any allignment-related properties that are non-null/empty if (typedMapStyle.TextAlignment.HasValue) { this.alignmentInfo.TextAlignment = typedMapStyle.TextAlignment.Value; } if (typedMapStyle.RotationAngle.HasValue && typedMapStyle.RotationAngle.Value != 0) { this.alignmentInfo.TextRotationAngle = typedMapStyle.RotationAngle.Value; } if (typedMapStyle.TextWrapping.HasValue) { this.alignmentInfo.TextWrapping = typedMapStyle.TextWrapping.Value; } if (typedMapStyle.Indentation.HasValue) { this.alignmentInfo.LeftMargin = typedMapStyle.Indentation.Value * 2; // NB! CH-Can we remove this /2... It's there because we / 2 when applying to cell } if (typedMapStyle.VerticalAlignment.HasValue) { this.alignmentInfo.VerticalAlignment = typedMapStyle.VerticalAlignment.Value; } this.hasCellInfo = true; } }
/// <summary> /// Updates this collection with the supplied style, applying taking into consideration the BasedOn property of the supplied value. /// </summary> /// <param name="value"></param> internal void MergeCloneStyle(StyleBase value) { if (!string.IsNullOrEmpty(value.BasedOnKey)) { // Look up mandatory existing style StyleBase baseStyle = this.dictionary[value.BasedOnKey] as StyleBase; if (baseStyle == null) { throw new MetadataException(string.Format("Supplied BasedOnKey is not a StyleBase <{0}>", value.BasedOnKey)); } StyleBase newStyle; // And update with properties of the supplied style if (value is Style) { if (baseStyle is Style) { // ExcelMapStyle based on ExcelMapStyle newStyle = Style.CreateMergedStyle(baseStyle, (Style)value); this.Add(newStyle); } else if (baseStyle is CellStyle) { // ExcelMapStyle can't be based on a ExcelCellMapStyle throw new InvalidOperationException(string.Format("Can't base ExcelMapStyle '{0}' on ExcelCellMapStyle '{1}' (not yet anyway)", value.Key, baseStyle.Key)); } else { // Whoops.. not an accounted for style throw new InvalidOperationException(string.Format("Style Key='{0}' BasedOnStyle is not valid style", baseStyle.Key)); } } else if (value is CellStyle) { if (baseStyle is Style) { // ExcelCellMapStyle based on ExcelMapStyle newStyle = CellStyle.CreateMergedStyle(baseStyle, (CellStyle)value); } else if (baseStyle is CellStyle) { // Straight Copy & Merge newStyle = CellStyle.CreateMergedStyle(baseStyle, (CellStyle)value); this.Add(newStyle); } else { // Whoops.. not an accounted for style throw new InvalidOperationException(string.Format("Style Key='{0}' BasedOnStyle is not valid style", baseStyle.Key)); } } else { // Whoops.. not an accounted for style throw new InvalidOperationException(string.Format("Style Key='{0}' is not valid style", value.Key)); } } else { // Clone and update (Potentially override at some later date) this.Add(value.Clone()); } }
/// <summary> /// Get a style based on supplied <see cref="ExcelCellInfo"/>.<br/> /// The style may have to be created in the stylesheet. /// </summary> /// <param name="mapStyle">Information about the style that has be be created.</param> /// <returns>Index of the style in the stylesheet</returns> public uint GetOrCreateStyle(StyleBase mapStyle) { System.Diagnostics.Debug.Assert(false, "TO BE WRITTEN"); return(0); }