Skip to content
59 changes: 59 additions & 0 deletions ooxml/XSSF/Model/SharedStringsTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ namespace NPOI.XSSF.Model
using System.Security;
using System.Text.RegularExpressions;
using System.Text;
using NPOI.Util;
using NPOI.XSSF.UserModel;
using NPOI.SS.UserModel;

/**
* Table of strings shared across all sheets in a workbook.
Expand Down Expand Up @@ -137,11 +140,24 @@ private static String GetKey(CT_Rst st)
* @param idx index of item to return.
* @return the item at the specified position in this Shared String table.
*/
[Obsolete("use <c>addSharedStringItem(RichTextString string)</c> instead")]
[Removal( Version = "4.2")]
public CT_Rst GetEntryAt(int idx)
{
return strings[idx];
}

/**
* Return a string item by index
*
* @param idx index of item to return.
* @return the item at the specified position in this Shared String table.
*/
public IRichTextString getItemAt(int idx)
{
return new XSSFRichTextString(strings[idx]);
}

/**
* Return an integer representing the total count of strings in the workbook. This count does not
* include any numbers, it counts only the total of text strings in the workbook.
Expand Down Expand Up @@ -182,6 +198,8 @@ public int UniqueCount
* @param st the entry to add
* @return index the index of Added entry
*/
[Obsolete("use <code>addSharedStringItem(RichTextString string)</code> instead")]
[Removal(Version = "4.2")]
public int AddEntry(CT_Rst st)
{
String s = GetKey(st);
Expand All @@ -201,11 +219,34 @@ public int AddEntry(CT_Rst st)
strings.Add(newSt);
return idx;
}

/**
* Add an entry to this Shared String table (a new value is appended to the end).
*
* <p>
* If the Shared String table already contains this string entry, its index is returned.
* Otherwise a new entry is added.
* </p>
*
* @param string the entry to add
* @since POI 4.0.0
* @return index the index of added entry
*/
public int AddSharedStringItem(IRichTextString str)
{
if(!(str is XSSFRichTextString)){
throw new ArgumentException("Only XSSFRichTextString argument is supported");
}
return AddEntry(((XSSFRichTextString) str).GetCTRst());
}

/**
* Provide low-level access to the underlying array of CT_Rst beans
*
* @return array of CT_Rst beans
*/
[Obsolete("use <code>getSharedStringItems</code> instead")]
[Removal(Version = "4.2")]
public IList<CT_Rst> Items
{
get
Expand All @@ -214,6 +255,24 @@ public IList<CT_Rst> Items
}
}

/**
* Provide access to the strings in the SharedStringsTable
*
* @return list of shared string instances
*/
public IList<IRichTextString> SharedStringItems
{
get
{
List<IRichTextString> items = new List<IRichTextString>();
foreach(CT_Rst rst in strings)
{
items.Add(new XSSFRichTextString(rst));
}
return items.AsReadOnly();
}
}

/**
*
* this table out as XML.
Expand Down
2 changes: 1 addition & 1 deletion ooxml/XSSF/UserModel/Extensions/XSSFCellBorder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public XSSFColor GetBorderColor(BorderSide side)

if (borderPr != null && borderPr.IsSetColor())
{
XSSFColor clr = new XSSFColor(borderPr.color, _indexedColorMap);
XSSFColor clr = XSSFColor.From(borderPr.color, _indexedColorMap);
if (_theme != null)
{
_theme.InheritFromThemeAsRequired(clr);
Expand Down
24 changes: 20 additions & 4 deletions ooxml/XSSF/UserModel/Extensions/XSSFCellFill.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public XSSFColor GetFillBackgroundColor()
if (ptrn == null) return null;

CT_Color CT_Color = ptrn.bgColor;
return CT_Color == null ? null : new XSSFColor(CT_Color, _indexedColorMap);
return XSSFColor.From(CT_Color, _indexedColorMap);
}

/**
Expand All @@ -84,7 +84,15 @@ public void SetFillBackgroundColor(int index)
public void SetFillBackgroundColor(XSSFColor color)
{
CT_PatternFill ptrn = EnsureCTPatternFill();
ptrn.bgColor = (color.GetCTColor());

if(color == null)
{
ptrn.UnsetBgColor();
}
else
{
ptrn.bgColor = (color.GetCTColor());
}
}

/**
Expand All @@ -98,7 +106,7 @@ public XSSFColor GetFillForegroundColor()
if (ptrn == null) return null;

CT_Color ctColor = ptrn.fgColor;
return ctColor == null ? null : new XSSFColor(ctColor, _indexedColorMap);
return XSSFColor.From(ctColor, _indexedColorMap);
}

/**
Expand All @@ -121,7 +129,15 @@ public void SetFillForegroundColor(int index)
public void SetFillForegroundColor(XSSFColor color)
{
CT_PatternFill ptrn = EnsureCTPatternFill();
ptrn.fgColor = color.GetCTColor();

if(color == null)
{
ptrn.UnsetFgColor();
}
else
{
ptrn.fgColor = color.GetCTColor();
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion ooxml/XSSF/UserModel/XSSFBorderFormatting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ private static short GetIndexedColor(XSSFColor color)

private XSSFColor GetColor(CT_BorderPr pr)
{
return pr == null ? null : new XSSFColor(pr.color, _colorMap);
return pr == null ? null : XSSFColor.From(pr.color, _colorMap);
}
#endregion
}
Expand Down
4 changes: 2 additions & 2 deletions ooxml/XSSF/UserModel/XSSFCell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ public ICell SetCellValue(IRichTextString str)
_cell.t = ST_CellType.s;
XSSFRichTextString rt = (XSSFRichTextString)str;
rt.SetStylesTableReference(_stylesSource);
int sRef = _sharedStringSource.AddEntry(rt.GetCTRst());
int sRef = _sharedStringSource.AddSharedStringItem(rt);
_cell.v=sRef.ToString();
}
break;
Expand Down Expand Up @@ -1060,7 +1060,7 @@ public ICell SetCellType(CellType cellType)
String str = ConvertCellValueToString();
XSSFRichTextString rt = new XSSFRichTextString(str);
rt.SetStylesTableReference(_stylesSource);
int sRef = _sharedStringSource.AddEntry(rt.GetCTRst());
int sRef = _sharedStringSource.AddSharedStringItem(rt);
_cell.v= sRef.ToString();
}
_cell.t= (ST_CellType.s);
Expand Down
12 changes: 6 additions & 6 deletions ooxml/XSSF/UserModel/XSSFCellStyle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ public short BottomBorderColor
}
set
{
XSSFColor clr = new XSSFColor();
XSSFColor clr = XSSFColor.From(new CT_Color(), _stylesSource.IndexedColors);
clr.Indexed = value;
SetBottomBorderColor(clr);
}
Expand Down Expand Up @@ -490,7 +490,7 @@ public short FillBackgroundColor
}
set
{
XSSFColor clr = new XSSFColor();
XSSFColor clr = XSSFColor.From(new CT_Color(), _stylesSource.IndexedColors);
clr.Indexed = (value);
SetFillBackgroundColor(clr);
}
Expand Down Expand Up @@ -568,7 +568,7 @@ public short FillForegroundColor
}
set
{
XSSFColor clr = new XSSFColor();
XSSFColor clr = XSSFColor.From(new CT_Color(), _stylesSource.IndexedColors);
clr.Indexed = (value);
SetFillForegroundColor(clr);
}
Expand Down Expand Up @@ -761,7 +761,7 @@ public short LeftBorderColor
}
set
{
XSSFColor clr = new XSSFColor();
XSSFColor clr = XSSFColor.From(new CT_Color(), _stylesSource.IndexedColors);
clr.Indexed = (value);
SetLeftBorderColor(clr);
}
Expand Down Expand Up @@ -850,7 +850,7 @@ public short RightBorderColor
}
set
{
XSSFColor clr = new XSSFColor();
XSSFColor clr = XSSFColor.From(new CT_Color(), _stylesSource.IndexedColors);
clr.Indexed = (value);
SetRightBorderColor(clr);
}
Expand Down Expand Up @@ -913,7 +913,7 @@ public short TopBorderColor
}
set
{
XSSFColor clr = new XSSFColor();
XSSFColor clr = XSSFColor.From(new CT_Color(), _stylesSource.IndexedColors);
clr.Indexed = (value);
SetTopBorderColor(clr);
}
Expand Down
35 changes: 30 additions & 5 deletions ooxml/XSSF/UserModel/XSSFColor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ public class XSSFColor : ExtendedColor
private CT_Color ctColor;
private IIndexedColorMap indexedColorMap;

/**
* @param color
* @param map
* @return null if color is null, new instance otherwise
*/
public static XSSFColor From(CT_Color color, IIndexedColorMap map)
{
return color == null ? null : new XSSFColor(color, map);
}

/**
* Create an instance of XSSFColor from the supplied XML bean
*/
Expand All @@ -46,6 +56,9 @@ public XSSFColor(CT_Color color)
{

}

[Obsolete("we want to have the indexed map, and all calling contexts have access to it.")]
[Removal( Version = "4.2")]
public XSSFColor(CT_Color color, IIndexedColorMap map)
{
this.ctColor = color;
Expand All @@ -54,20 +67,32 @@ public XSSFColor(CT_Color color, IIndexedColorMap map)
/**
* Create an new instance of XSSFColor
*/
[Obsolete("we want to have the indexed map, and all calling contexts have access to it.")]
[Removal(Version = "4.2")]
public XSSFColor()
: this(new CT_Color(), null)
: this(new CT_Color(), new DefaultIndexedColorMap())
{
}

public XSSFColor(Color clr)
: this()
/**
* new color with the given indexed color map
* @param colorMap
*/
public XSSFColor(IIndexedColorMap colorMap)
: this(new CT_Color(), colorMap)
{

}

public XSSFColor(Color clr, IIndexedColorMap map)
: this(map)
{
var c = clr.ToPixel<Rgb24>();
ctColor.SetRgb(c.R, c.G, c.B);
}

public XSSFColor(Rgb24 clr)
: this() {
public XSSFColor(Rgb24 clr, IIndexedColorMap map)
: this(map) {

ctColor.SetRgb(clr.R, clr.G, clr.B);
}
Expand Down
4 changes: 2 additions & 2 deletions ooxml/XSSF/UserModel/XSSFColorScaleFormatting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public IColor[] Colors
XSSFColor[] c = new XSSFColor[ctcols.Length];
for(int i = 0; i < ctcols.Length; i++)
{
c[i] = new XSSFColor(ctcols[i], _indexedColorMap);
c[i] = XSSFColor.From(ctcols[i], _indexedColorMap);
}
return c;
}
Expand Down Expand Up @@ -107,7 +107,7 @@ public IConditionalFormattingThreshold[] Thresholds

public XSSFColor CreateColor()
{
return new XSSFColor(_scale.AddNewColor(), _indexedColorMap);
return XSSFColor.From(_scale.AddNewColor(), _indexedColorMap);
}
public IConditionalFormattingThreshold CreateThreshold()
{
Expand Down
2 changes: 1 addition & 1 deletion ooxml/XSSF/UserModel/XSSFCreationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public IClientAnchor CreateClientAnchor()

public ExtendedColor CreateExtendedColor()
{
return new XSSFColor(new CT_Color(),
return XSSFColor.From(new CT_Color(),
(workbook as XSSFWorkbook).GetStylesSource().IndexedColors);
}

Expand Down
2 changes: 1 addition & 1 deletion ooxml/XSSF/UserModel/XSSFDataBarFormatting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public IColor Color
{
get
{
return new XSSFColor(_databar.color, _colorMap);
return XSSFColor.From(_databar.color, _colorMap);
}
set
{
Expand Down
2 changes: 1 addition & 1 deletion ooxml/XSSF/UserModel/XSSFFont.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public XSSFColor GetXSSFColor()
Spreadsheet.CT_Color ctColor = _ctFont.sizeOfColorArray() == 0 ? null : _ctFont.GetColorArray(0);
if (ctColor != null)
{
XSSFColor color = new XSSFColor(ctColor, _indexedColorMap);
XSSFColor color = XSSFColor.From(ctColor, _indexedColorMap);
if (_themes != null)
{
_themes.InheritFromThemeAsRequired(color);
Expand Down
2 changes: 1 addition & 1 deletion ooxml/XSSF/UserModel/XSSFFontFormatting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public IColor FontColor
{
if (_font.sizeOfColorArray() == 0) return null;

return new XSSFColor(_font.GetColorArray(0), _colorMap);
return XSSFColor.From(_font.GetColorArray(0), _colorMap);
}
set
{
Expand Down
4 changes: 2 additions & 2 deletions ooxml/XSSF/UserModel/XSSFPatternFormatting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public IColor FillBackgroundColorColor
get
{
if (!_fill.IsSetPatternFill()) return null;
return new XSSFColor(_fill.GetPatternFill().bgColor, _colorMap);
return XSSFColor.From(_fill.GetPatternFill().bgColor, _colorMap);
}
set
{
Expand Down Expand Up @@ -70,7 +70,7 @@ public IColor FillForegroundColorColor
{
if (!_fill.IsSetPatternFill() || !_fill.GetPatternFill().IsSetFgColor())
return null;
return new XSSFColor(_fill.GetPatternFill().fgColor, _colorMap);
return XSSFColor.From(_fill.GetPatternFill().fgColor, _colorMap);
}
set
{
Expand Down
2 changes: 1 addition & 1 deletion ooxml/XSSF/UserModel/XSSFSheet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1193,7 +1193,7 @@ public XSSFColor TabColor
return null;
}

return new XSSFColor(pr.tabColor, (Workbook as XSSFWorkbook).GetStylesSource().IndexedColors);
return XSSFColor.From(pr.tabColor, (Workbook as XSSFWorkbook).GetStylesSource().IndexedColors);
}
set
{
Expand Down
Loading