Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions main/SS/Util/CellReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -544,29 +544,31 @@ public static bool CellReferenceIsWithinRange(ReadOnlySpan<char> colStr, ReadOnl
return IsRowWithinRange(rowStr, ssVersion);
}

/**
* @deprecated 3.15 beta 2. Use {@link #isColumnWithinRange}.
*/
[Obsolete("deprecated 3.15 beta 2. Use {@link #isColumnWithinRange}.")]
public static bool IsColumnWithnRange(String colStr, SpreadsheetVersion ssVersion)
{
return IsColumnWithinRange(colStr, ssVersion);
}

/// <summary>
/// Determines whether <c>rowStr</c> is a valid row number for a given SpreadsheetVersion.
/// </summary>
/// <param name="rowStr">the numeric portion of an A1-style cell reference (1-based index)</param>
/// <param name="ssVersion">the spreadsheet version</param>
/// <returns></returns>
public static bool IsRowWithinRange(String rowStr, SpreadsheetVersion ssVersion)
=> IsRowWithinRange(rowStr.AsSpan(), ssVersion);

public static bool IsRowWithinRange(ReadOnlySpan<char> rowStr, SpreadsheetVersion ssVersion)
{
CellReferenceParser.TryParsePositiveInt32Fast(rowStr, out var rowNum);
rowNum -= 1;
return 0 <= rowNum && rowNum <= ssVersion.LastRowIndex;
return IsRowWithinRange(rowNum, ssVersion);
}

[Obsolete("deprecated 3.15 beta 2. Use {@link #isRowWithinRange}")]
public static bool isRowWithnRange(String rowStr, SpreadsheetVersion ssVersion)
/// <summary>
/// Determines whether <c>row</c> is a valid row number for a given SpreadsheetVersion.
/// </summary>
/// <param name="rowNum">the row number (0-based index)</param>
/// <param name="ssVersion">the spreadsheet version</param>
/// <returns></returns>
public static bool IsRowWithinRange(int rowNum, SpreadsheetVersion ssVersion)
{
return IsRowWithinRange(rowStr, ssVersion);
return 0 <= rowNum && rowNum <= ssVersion.LastRowIndex;
}

public static bool IsColumnWithinRange(String colStr, SpreadsheetVersion ssVersion)
Expand Down
198 changes: 198 additions & 0 deletions ooxml/XSSF/Streaming/SXSSFSheet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ limitations under the License.
using NPOI.SS.Util;
using NPOI.Util;
using NPOI.XSSF.UserModel;
using NPOI.OpenXmlFormats.Spreadsheet;

namespace NPOI.XSSF.Streaming
{
Expand Down Expand Up @@ -1477,6 +1478,203 @@ public void AutoSizeRow(int row, bool useMergedCells)
throw new NotImplementedException();
}

/// <summary>
/// Enable sheet protection
/// </summary>
public void EnableLocking()
{
SafeGetProtectionField().sheet = true;
}

/// <summary>
/// Disable sheet protection
/// </summary>
public void DisableLocking()
{
SafeGetProtectionField().sheet = false;
}

/// <summary>
/// Enable or disable Autofilters locking.
/// This does not modify sheet protection status.
/// To enforce this un-/locking, call <see cref="DisableLocking()" /> or <see cref="EnableLocking()" />
/// </summary>
public void LockAutoFilter(bool enabled)
{
SafeGetProtectionField().autoFilter = enabled;
}

/// <summary>
/// Enable or disable Deleting columns locking.
/// This does not modify sheet protection status.
/// To enforce this un-/locking, call <see cref="DisableLocking()" /> or <see cref="EnableLocking()" />
/// </summary>
public void LockDeleteColumns(bool enabled)
{
SafeGetProtectionField().deleteColumns = enabled;
}

/// <summary>
/// Enable or disable Deleting rows locking.
/// This does not modify sheet protection status.
/// To enforce this un-/locking, call <see cref="DisableLocking()" /> or <see cref="EnableLocking()" />
/// </summary>
public void LockDeleteRows(bool enabled)
{
SafeGetProtectionField().deleteRows = enabled;
}

/// <summary>
/// Enable or disable Formatting cells locking.
/// This does not modify sheet protection status.
/// To enforce this un-/locking, call <see cref="DisableLocking()" /> or <see cref="EnableLocking()" />
/// </summary>
public void LockFormatCells(bool enabled)
{
SafeGetProtectionField().formatCells = enabled;
}

/// <summary>
/// Enable or disable Formatting columns locking.
/// This does not modify sheet protection status.
/// To enforce this un-/locking, call <see cref="DisableLocking()" /> or <see cref="EnableLocking()" />
/// </summary>
public void LockFormatColumns(bool enabled)
{
SafeGetProtectionField().formatColumns = enabled;
}

/// <summary>
/// Enable or disable Formatting rows locking.
/// This does not modify sheet protection status.
/// To enforce this un-/locking, call <see cref="DisableLocking()" /> or <see cref="EnableLocking()" />
/// </summary>
public void LockFormatRows(bool enabled)
{
SafeGetProtectionField().formatRows = enabled;
}

/// <summary>
/// Enable or disable Inserting columns locking.
/// This does not modify sheet protection status.
/// To enforce this un-/locking, call <see cref="DisableLocking()" /> or <see cref="EnableLocking()" />
/// </summary>
public void LockInsertColumns(bool enabled)
{
SafeGetProtectionField().insertColumns = enabled;
}

/// <summary>
/// Enable or disable Inserting hyperlinks locking.
/// This does not modify sheet protection status.
/// To enforce this un-/locking, call <see cref="DisableLocking()" /> or <see cref="EnableLocking()" />
/// </summary>
public void LockInsertHyperlinks(bool enabled)
{
SafeGetProtectionField().insertHyperlinks = enabled;
}

/// <summary>
/// Enable or disable Inserting rows locking.
/// This does not modify sheet protection status.
/// To enforce this un-/locking, call <see cref="DisableLocking()" /> or <see cref="EnableLocking()" />
/// </summary>
public void LockInsertRows(bool enabled)
{
SafeGetProtectionField().insertRows = enabled;
}

/// <summary>
/// Enable or disable Pivot Tables locking.
/// This does not modify sheet protection status.
/// To enforce this un-/locking, call <see cref="DisableLocking()" /> or <see cref="EnableLocking()" />
/// </summary>
public void LockPivotTables(bool enabled)
{
SafeGetProtectionField().pivotTables = enabled;
}

/// <summary>
/// Enable or disable Sort locking.
/// This does not modify sheet protection status.
/// To enforce this un-/locking, call <see cref="DisableLocking()" /> or <see cref="EnableLocking()" />
/// </summary>
public void LockSort(bool enabled)
{
SafeGetProtectionField().sort = enabled;
}

/// <summary>
/// Enable or disable Objects locking.
/// This does not modify sheet protection status.
/// To enforce this un-/locking, call <see cref="DisableLocking()" /> or <see cref="EnableLocking()" />
/// </summary>
public void LockObjects(bool enabled)
{
SafeGetProtectionField().objects = enabled;
}

/// <summary>
/// Enable or disable Scenarios locking.
/// This does not modify sheet protection status.
/// To enforce this un-/locking, call <see cref="DisableLocking()" /> or <see cref="EnableLocking()" />
/// </summary>
public void LockScenarios(bool enabled)
{
SafeGetProtectionField().scenarios = enabled;
}

/// <summary>
/// Enable or disable Selection of locked cells locking.
/// This does not modify sheet protection status.
/// To enforce this un-/locking, call <see cref="DisableLocking()" /> or <see cref="EnableLocking()" />
/// </summary>
public void LockSelectLockedCells(bool enabled)
{
SafeGetProtectionField().selectLockedCells = enabled;
}

/// <summary>
/// Enable or disable Selection of unlocked cells locking.
/// This does not modify sheet protection status.
/// To enforce this un-/locking, call <see cref="DisableLocking()" /> or <see cref="EnableLocking()" />
/// </summary>
public void LockSelectUnlockedCells(bool enabled)
{
SafeGetProtectionField().selectUnlockedCells = enabled;
}

private CT_SheetProtection SafeGetProtectionField()
{
CT_Worksheet ct = _sh.GetCTWorksheet();
if (!IsSheetProtectionEnabled())
{
return ct.AddNewSheetProtection();
}
return ct.sheetProtection;
}

/* package */
internal bool IsSheetProtectionEnabled()
{
CT_Worksheet ct = _sh.GetCTWorksheet();
return (ct.IsSetSheetProtection());
}

/// <summary>
/// Set background color of the sheet tab
/// </summary>
/// <param name="colorIndex"> the indexed color to Set, must be a constant from <see cref="IndexedColors"/></param>
public void SetTabColor(int colorIndex)
{
CT_Worksheet ct = _sh.GetCTWorksheet();
CT_SheetPr pr = ct.sheetPr;
if (pr == null) pr = ct.AddNewSheetPr();
CT_Color color = new CT_Color();
color.indexed = (uint)colorIndex;
pr.tabColor = color;
}

IEnumerator<IRow> IEnumerable<IRow>.GetEnumerator()
{
return ((IEnumerable<IRow>) _sh).GetEnumerator();
Expand Down
11 changes: 9 additions & 2 deletions ooxml/XSSF/UserModel/XSSFName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -422,9 +422,16 @@ thus we are stuck with Character.isLetter (for now).
{
string col = Regex.Replace(name, "\\d", "");
string row = Regex.Replace(name, "[A-Za-z]", "");
if (CellReference.CellReferenceIsWithinRange(col, row, SpreadsheetVersion.EXCEL97))
try
{
throw new ArgumentException("Invalid name: '" + name + "': cannot be $A$1-style cell reference");
if (CellReference.CellReferenceIsWithinRange(col, row, SpreadsheetVersion.EXCEL2007))
{
throw new ArgumentException("Invalid name: '" + name + "': cannot be $A$1-style cell reference");
}
}
catch (FormatException) {
// row was not parseable as an Integer, such as a BigInt
// therefore name passes the not-a-cell-reference criteria
}
}

Expand Down
52 changes: 29 additions & 23 deletions testcases/main/HSSF/UserModel/TestEscherGraphics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ namespace TestCases.HSSF.UserModel


using NUnit.Framework;using NUnit.Framework.Legacy;
using SixLabors.Fonts;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;

Expand Down Expand Up @@ -54,7 +55,7 @@ public void SetUp()
System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US");
workbook = new HSSFWorkbook();

NPOI.SS.UserModel.ISheet sheet = workbook.CreateSheet("Test");
ISheet sheet = workbook.CreateSheet("Test");
patriarch = (HSSFPatriarch)sheet.CreateDrawingPatriarch();
escherGroupA = patriarch.CreateGroup(new HSSFClientAnchor(0, 0, 1022, 255, (short)0, 0, (short)0, 0));
escherGroupB = patriarch.CreateGroup(new HSSFClientAnchor(20, 30, 500, 200, (short)0, 0, (short)0, 0));
Expand All @@ -63,41 +64,46 @@ public void SetUp()

}

/* TODO-Fonts:
[Test]
public void TestGetFont()
{
System.Drawing.Font f = graphics.Font;
if (f.ToString().IndexOf("dialog") == -1 && f.ToString().IndexOf("Dialog") == -1)
Font f = graphics.Font;
if (!f.ToString().Contains("dialog") && !f.ToString().Contains("Dialog"))
{
//ClassicAssert.AreEqual("java.awt.Font[family=Arial,name=Arial,style=plain,size=10]", f.ToString());
ClassicAssert.AreEqual("[Font: Name=Arial, Size=10, Units=3, GdiCharSet=1, GdiVerticalFont=False]", f.ToString());
//ClassicAssert.AreEqual("[Font: Name=Arial, Size=10, Units=3, GdiCharSet=1, GdiVerticalFont=False]", f.ToString());
ClassicAssert.AreEqual("Arial", f.Family.Name);
ClassicAssert.AreEqual("Arial", f.Name);
ClassicAssert.AreEqual(10, f.Size);
ClassicAssert.AreEqual(FontStyle.Regular, f.FontMetrics.Description.Style);
}
}
*/

//[Test]
//public void TestGetFontMetrics()
//{
// Font f = graphics.Font;
// if (f.ToString().IndexOf("dialog") != -1 || f.ToString().IndexOf("Dialog") != -1)
// return;

// ClassicAssert.AreEqual(7, TextRenderer.MeasureText("X", f).Width);
// ClassicAssert.AreEqual("Arial", f.FontFamily.Name);
// ClassicAssert.AreEqual(10, f.Size);
// //ClassicAssert.AreEqual("java.awt.Font[family=Arial,name=Arial,style=plain,size=10]", fontMetrics.GetFont().ToString());
//}

/* TODO-Fonts:

[Test]
public void TestGetFontMetrics()
{
Font f = graphics.Font;
if (f.ToString().Contains("dialog") || f.ToString().Contains("Dialog"))
return;

ClassicAssert.AreEqual(7, TextMeasurer.MeasureSize("X", new TextOptions(f)).Width);
ClassicAssert.AreEqual("Arial", f.Family.Name);
ClassicAssert.AreEqual(10, f.Size);
ClassicAssert.AreEqual(FontStyle.Regular, f.FontMetrics.Description.Style);
//ClassicAssert.AreEqual("java.awt.Font[family=Arial,name=Arial,style=plain,size=10]", fontMetrics.GetFont().ToString());
}

[Test]
public void TestSetFont()
{
System.Drawing.Font f = new System.Drawing.Font("Helvetica", 12,FontStyle.Regular);
FontCollection fonts = new FontCollection();
var fi = POIDataSamples.GetSpreadSheetInstance().GetFileInfo("Helvetica.ttf");
SixLabors.Fonts.FontFamily font1 = fonts.Add(fi.FullName);
Font f = new Font(font1, 12, FontStyle.Regular);
graphics.SetFont(f);
ClassicAssert.AreEqual(f, graphics.Font);
}
*/

[Test]
public void TestSetColor()
{
Expand Down
19 changes: 19 additions & 0 deletions testcases/main/SS/Util/TestCellReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,27 @@ public void IsRowWithinRange()
ClassicAssert.IsTrue(CellReference.IsRowWithinRange("1", ss), "first row");
ClassicAssert.IsTrue(CellReference.IsRowWithinRange("1048576", ss), "last row");
ClassicAssert.IsFalse(CellReference.IsRowWithinRange("1048577", ss), "1 beyond last row");

ClassicAssert.IsFalse(CellReference.IsRowWithinRange(-1, ss), "1 before first row");
ClassicAssert.IsTrue(CellReference.IsRowWithinRange(0, ss), "first row");
ClassicAssert.IsTrue(CellReference.IsRowWithinRange(1048575, ss), "last row");
ClassicAssert.IsFalse(CellReference.IsRowWithinRange(1048576, ss), "1 beyond last row");
}

[Test] //(expected= NumberFormatException.class)
public void IsRowWithinRangeNonInteger_BigNumber()
{
String rowNum = "4000000000";
CellReference.IsRowWithinRange(rowNum, SpreadsheetVersion.EXCEL2007);
}

[Test] //(expected= NumberFormatException.class)
public void IsRowWithinRangeNonInteger_Alpha()
{
String rowNum = "NotANumber";
CellReference.IsRowWithinRange(rowNum, SpreadsheetVersion.EXCEL2007);
}

[Test]
public void IsColWithinRange()
{
Expand Down
Loading
Loading