/// <summary> /// Gets the index of the target value. /// </summary> /// <param name="type">The type of the target value.</param> /// <param name="value">The target value in the list.</param> /// <returns>The index of the value in the list.</returns> public int GetSharedItemIndex(PivotCacheRecordType type, object value) { string stringValue = ConvertUtil.ConvertObjectToXmlAttributeString(value); for (int i = 0; i < this.SharedItems.Count; i++) { var item = this.SharedItems[i]; if (type == PivotCacheRecordType.n && !string.IsNullOrWhiteSpace(item.Value)) { // Compare the value with the existing shared item value with precision as duplicate values // cause corrupt workbooks. double doubleItemValue = double.Parse(item.Value); var doubleTargetValue = Convert.ToDouble(stringValue); if (Math.Abs(doubleItemValue - doubleTargetValue) < CacheFieldNode.Epsilon) { return(i); } } else { // Empty strings are sometimes put in as string values by Excel // so we will let empty types match with empty string shared items. if ((type == PivotCacheRecordType.m || type == item.Type) && stringValue.IsEquivalentTo(item.Value)) { return(i); } } } return(-1); }
private void AddToList(CacheRecordNode record, int dataFieldIndex, List <object> matchingValues) { string itemValue = null; PivotCacheRecordType type = record.Items[dataFieldIndex].Type; if (type == PivotCacheRecordType.x) { int sharedItemIndex = int.Parse(record.Items[dataFieldIndex].Value); var cacheField = this.CacheDefinition.CacheFields[dataFieldIndex]; var sharedItem = cacheField.SharedItems[sharedItemIndex]; type = sharedItem.Type; itemValue = sharedItem.Value; } else { itemValue = record.Items[dataFieldIndex].Value; } if (type == PivotCacheRecordType.m) { matchingValues.Add(null); } else if (string.IsNullOrWhiteSpace(itemValue)) { matchingValues.Add(itemValue); } else if (type == PivotCacheRecordType.d) { matchingValues.Add(DateTime.Parse(itemValue)); } else { double.TryParse(itemValue, out var recordData); matchingValues.Add(recordData); } }
private string GetCacheFieldSharedItemIndexString(CacheFieldNode cacheField, PivotCacheRecordType type, object value) { int cacheFieldItemIndex = cacheField.GetSharedItemIndex(type, value); // Adds a new sharedItem if the item does not exist. if (cacheFieldItemIndex < 0) { cacheField.SharedItems.Add(value); cacheFieldItemIndex = cacheField.SharedItems.Count - 1; } return(ConvertUtil.ConvertObjectToXmlAttributeString(cacheFieldItemIndex)); }
/// <summary> /// Creates an instance of a <see cref="CacheItem"/> object given a type and value. /// </summary> /// <param name="namespaceManager">The namespace manager.</param> /// <param name="parentNode">The parent xml node. It must be a sharedItems <see cref="XmlNode"/> or a cacheRecord <see cref="XmlNode"/>.</param> /// <param name="type">The type of this item.</param> /// <param name="value">The value of this item.</param> public CacheItem(XmlNamespaceManager namespaceManager, XmlNode parentNode, PivotCacheRecordType type, string value) : base(namespaceManager, null) { if (parentNode == null) { throw new ArgumentNullException(nameof(parentNode)); } base.TopNode = parentNode.OwnerDocument.CreateElement(type.ToString(), parentNode.NamespaceURI); this.Type = type; var attr = parentNode.OwnerDocument.CreateAttribute("v"); base.TopNode.Attributes.Append(attr); this.Value = value; }
/// <summary> /// Gets the index of the target value. /// </summary> /// <param name="type">The type of the target value.</param> /// <param name="value">The target value in the list.</param> /// <returns>The index of the value in the list.</returns> public int GetSharedItemIndex(PivotCacheRecordType type, object value) { string stringValue = ConvertUtil.ConvertObjectToXmlAttributeString(value); for (int i = 0; i < this.SharedItems.Count; i++) { var item = this.SharedItems[i]; // Empty strings are sometimes put in as string values by Excel // so we will let empty types match with empty string shared items. if ((type == PivotCacheRecordType.m || type == item.Type) && stringValue.IsEquivalentTo(item.Value)) { return(i); } } return(-1); }
/// <summary> /// Replace <see cref="XmlNode"/> with new node type and value. /// </summary> /// <param name="type">The new <see cref="PivotCacheRecordType"/>.</param> /// <param name="value">The value.</param> /// <param name="parentNode">The parent <see cref="XmlNode"/>.</param> public void ReplaceNode(PivotCacheRecordType type, string value, XmlNode parentNode) { if (parentNode == null) { throw new ArgumentNullException(nameof(parentNode)); } var newNode = parentNode.OwnerDocument.CreateElement(type.ToString(), parentNode.NamespaceURI); if (type != PivotCacheRecordType.m) { var attr = parentNode.OwnerDocument.CreateAttribute("v"); newNode.Attributes.Append(attr); } parentNode.ReplaceChild(newNode, base.TopNode); base.TopNode = newNode; this.Type = type; this.Value = value; }
/// <summary> /// Creates an instance of a <see cref="CacheItem"/> object given a type and value. /// </summary> /// <param name="namespaceManager">The namespace manager.</param> /// <param name="parentNode">The parent xml node. It must be a sharedItems <see cref="XmlNode"/> or a cacheRecord <see cref="XmlNode"/>.</param> /// <param name="type">The type of this item.</param> /// <param name="value">The value of this item.</param> public CacheItem(XmlNamespaceManager namespaceManager, XmlNode parentNode, PivotCacheRecordType type, string value) : base(namespaceManager, null) { if (parentNode == null) { throw new ArgumentNullException(nameof(parentNode)); } if (parentNode.LocalName != "sharedItems" && parentNode.LocalName != "r") { throw new ArgumentException($"{nameof(parentNode)} type: '{parentNode.Name}' was not the expected type."); } base.TopNode = parentNode.OwnerDocument.CreateElement(type.ToString(), parentNode.NamespaceURI); this.Type = type; if (!string.IsNullOrEmpty(value)) { var attr = parentNode.OwnerDocument.CreateAttribute("v"); base.TopNode.Attributes.Append(attr); this.Value = value; } }
private void AssertCacheRecord(ExcelPivotCacheRecords records, int row, int col, PivotCacheRecordType type, string value) { Assert.AreEqual(value, records[row].Items[col].Value); Assert.AreEqual(type, records[row].Items[col].Type); }
private void AssertCacheItem(CacheItem item, string value, PivotCacheRecordType type) { Assert.AreEqual(value, item.Value); Assert.AreEqual(type, item.Type); }