public void Delete(Int32 position) { int wsCount = _worksheets.Values.Count(w => w.Position == position); if (wsCount == 0) { throw new ArgumentException("There isn't a worksheet associated with that index."); } if (wsCount > 1) { throw new ArgumentException( "Can't delete the worksheet because there are multiple worksheets associated with that index."); } var ws = _worksheets.Values.Single(w => w.Position == position); if (!XLHelper.IsNullOrWhiteSpace(ws.RelId) && !Deleted.Contains(ws.RelId)) { Deleted.Add(ws.RelId); } _worksheets.RemoveAll(w => w.Position == position); _worksheets.Values.Where(w => w.Position > position).ForEach(w => w._position -= 1); _workbook.UnsupportedSheets.Where(w => w.Position > position).ForEach(w => w.Position -= 1); }
private void RescanFieldNames() { if (ShowHeaderRow) { var detectedFieldNames = new Dictionary <String, IXLTableField>(); var headersRow = HeadersRow(false); Int32 cellPos = 0; foreach (var cell in headersRow.Cells()) { var name = cell.GetString(); if (_fieldNames.ContainsKey(name) && _fieldNames[name].Column.ColumnNumber() == cell.Address.ColumnNumber) { (_fieldNames[name] as XLTableField).Index = cellPos; detectedFieldNames.Add(name, _fieldNames[name]); cellPos++; continue; } if (XLHelper.IsNullOrWhiteSpace(name)) { name = GetUniqueName("Column", cellPos + 1, true); cell.SetValue(name); cell.DataType = XLDataType.Text; } if (_fieldNames.ContainsKey(name)) { throw new ArgumentException("The header row contains more than one field name '" + name + "'."); } _fieldNames.Add(name, new XLTableField(this, name) { Index = cellPos++ }); detectedFieldNames.Add(name, _fieldNames[name]); } _fieldNames.Keys .Where(key => !detectedFieldNames.ContainsKey(key)) .ToArray() .ForEach(key => _fieldNames.Remove(key)); } else { Int32 colCount = ColumnCount(); for (Int32 i = 1; i <= colCount; i++) { if (_fieldNames.Values.All(f => f.Index != i - 1)) { var name = "Column" + i; _fieldNames.Add(name, new XLTableField(this, name) { Index = i - 1 }); } } } }
public Boolean IsDirty() { return (AllowedValues != XLAllowedValues.AnyValue || (ShowInputMessage && (!XLHelper.IsNullOrWhiteSpace(InputTitle) || !XLHelper.IsNullOrWhiteSpace(InputMessage))) || (ShowErrorMessage && (!XLHelper.IsNullOrWhiteSpace(ErrorTitle) || !XLHelper.IsNullOrWhiteSpace(ErrorMessage)))); }
public void Rename(String oldSheetName, String newSheetName) { if (XLHelper.IsNullOrWhiteSpace(oldSheetName) || !_worksheets.ContainsKey(oldSheetName)) { return; } var ws = _worksheets[oldSheetName]; _worksheets.Remove(oldSheetName); _worksheets.Add(newSheetName, ws); }
public static Double GetWidth(this IXLFontBase fontBase, String text, Dictionary <IXLFontBase, Font> fontCache) { if (XLHelper.IsNullOrWhiteSpace(text)) { return(0); } var font = GetCachedFont(fontBase, fontCache); var textSize = TextRenderer.MeasureText(text, font); double width = (((textSize.Width / (double)7) * 256) - (128 / 7)) / 256; width = (double)decimal.Round((decimal)width + 0.2M, 2); return(width); }
public static Double GetWidth(this IXLFontBase fontBase, String text, Dictionary <IXLFontBase, Font> fontCache) { if (XLHelper.IsNullOrWhiteSpace(text)) { return(0); } var font = GetCachedFont(fontBase, fontCache); var textWidth = GraphicsUtils.MeasureString(text, font).Width; double width = (textWidth / 7d * 256 - 128 / 7) / 256; width = Math.Round(width + 0.2, 2); return(width); }
public static bool IsValidColumn(string column) { var length = column.Length; if (XLHelper.IsNullOrWhiteSpace(column) || length > 3) { return(false); } var theColumn = column.ToUpper(); var isValid = theColumn[0] >= 'A' && theColumn[0] <= 'Z'; if (length == 1) { return(isValid); } if (length == 2) { return(isValid && theColumn[1] >= 'A' && theColumn[1] <= 'Z'); } if (theColumn[0] >= 'A' && theColumn[0] < 'X') { return(theColumn[1] >= 'A' && theColumn[1] <= 'Z' && theColumn[2] >= 'A' && theColumn[2] <= 'Z'); } if (theColumn[0] != 'X') { return(false); } if (theColumn[1] < 'F') { return(theColumn[2] >= 'A' && theColumn[2] <= 'Z'); } if (theColumn[1] != 'F') { return(false); } return(theColumn[2] >= 'A' && theColumn[2] <= 'D'); }
public void Rename(String oldSheetName, String newSheetName) { if (XLHelper.IsNullOrWhiteSpace(oldSheetName) || !_worksheets.ContainsKey(oldSheetName)) { return; } if (_worksheets.Any(ws1 => ws1.Key.Equals(newSheetName, StringComparison.OrdinalIgnoreCase))) { throw new ArgumentException(String.Format("A worksheet with the same name ({0}) has already been added.", newSheetName), nameof(newSheetName)); } var ws = _worksheets[oldSheetName]; _worksheets.Remove(oldSheetName); Add(newSheetName, ws); }
private void AddToTables(XLRange range, Boolean addToTables) { if (!addToTables) { return; } _uniqueNames = new HashSet <string>(); Int32 co = 1; foreach (IXLCell c in range.Row(1).Cells()) { if (XLHelper.IsNullOrWhiteSpace(((XLCell)c).InnerText)) { c.Value = GetUniqueName("Column", co, true); } _uniqueNames.Add(c.GetString()); co++; } Worksheet.Tables.Add(this); }
public static bool IsValidA1Address(string address) { if (XLHelper.IsNullOrWhiteSpace(address)) { return(false); } address = address.Replace("$", ""); var rowPos = 0; var addressLength = address.Length; while (rowPos < addressLength && (address[rowPos] > '9' || address[rowPos] < '0')) { rowPos++; } return (rowPos < addressLength && IsValidRow(address.Substring(rowPos)) && IsValidColumn(address.Substring(0, rowPos))); }
public IXLTable Resize(IXLRange range) { if (!this.ShowHeaderRow) { throw new NotImplementedException("Resizing of tables with no headers not supported yet."); } if (this.Worksheet != range.Worksheet) { throw new InvalidOperationException("You cannot resize a table to a range on a different sheet."); } var totalsRowChanged = this.ShowTotalsRow ? range.LastRow().RowNumber() - this.TotalsRow().RowNumber() : 0; var oldTotalsRowNumber = this.ShowTotalsRow ? this.TotalsRow().RowNumber() : -1; var existingHeaders = this.FieldNames.Keys; var newHeaders = new HashSet <string>(); var tempArray = this.Fields.Select(f => f.Column).ToArray(); var firstRow = range.Row(1); if (!firstRow.FirstCell().Address.Equals(this.HeadersRow().FirstCell().Address) || !firstRow.LastCell().Address.Equals(this.HeadersRow().LastCell().Address)) { _uniqueNames.Clear(); var co = 1; foreach (var c in firstRow.Cells()) { if (XLHelper.IsNullOrWhiteSpace(((XLCell)c).InnerText)) { c.Value = GetUniqueName("Column", co, true); } var header = c.GetString(); _uniqueNames.Add(header); if (!existingHeaders.Contains(header)) { newHeaders.Add(header); } co++; } } if (totalsRowChanged < 0) { range.Rows(r => r.RowNumber().Equals(this.TotalsRow().RowNumber() + totalsRowChanged)).Single().InsertRowsAbove(1); range = Worksheet.Range(range.FirstCell(), range.LastCell().CellAbove()); oldTotalsRowNumber++; } else if (totalsRowChanged > 0) { this.TotalsRow().RowBelow(totalsRowChanged + 1).InsertRowsAbove(1); this.TotalsRow().AsRange().Delete(XLShiftDeletedCells.ShiftCellsUp); } this.RangeAddress = range.RangeAddress as XLRangeAddress; RescanFieldNames(); if (this.ShowTotalsRow) { foreach (var f in this._fieldNames.Values) { var c = this.TotalsRow().Cell(f.Index + 1); if (!c.IsEmpty() && newHeaders.Contains(f.Name)) { f.TotalsRowLabel = c.GetFormattedString(); c.DataType = XLDataType.Text; } } if (totalsRowChanged != 0) { foreach (var f in this._fieldNames.Values.Cast <XLTableField>()) { f.UpdateUnderlyingCellFormula(); var c = this.TotalsRow().Cell(f.Index + 1); if (!XLHelper.IsNullOrWhiteSpace(f.TotalsRowLabel)) { c.DataType = XLDataType.Text; //Remove previous row's label var oldTotalsCell = this.Worksheet.Cell(oldTotalsRowNumber, f.Column.ColumnNumber()); if (oldTotalsCell.Value.ToString() == f.TotalsRowLabel) { oldTotalsCell.Value = null; } } if (f.TotalsRowFunction != XLTotalsRowFunction.None) { c.DataType = XLDataType.Number; } } } } return(this); }