Esempio n. 1
0
        /// <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);
        }
Esempio n. 2
0
        /// <summary>
        /// Adds a new field item to the list.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns>The index of the new item.</returns>
        public void Add(object value)
        {
            string stringValue = ConvertUtil.ConvertObjectToXmlAttributeString(value);
            var    item        = new CacheItem(this.NameSpaceManager, base.TopNode, CacheItem.GetObjectType(value), stringValue);

            item.AddSelf(base.TopNode);
            base.AddItem(item);
        }
Esempio n. 3
0
        /// <summary>
        /// Adds a new field item to the list.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns>The index of the new item.</returns>
        public void Add(object value)
        {
            string stringValue = ConvertUtil.ConvertObjectToXmlAttributeString(value);
            var    item        = new CacheItem(this.NameSpaceManager, base.TopNode, CacheItem.GetObjectType(value), stringValue);

            if (item.Type == PivotCacheRecordType.m)
            {
                this.ContainsBlank = true;
            }
            base.AddItem(item);
        }
Esempio n. 4
0
        public void ConvertObjectToXmlAttributeString()
        {
            var dateTime = DateTime.Now;

            Assert.AreEqual(dateTime.ToString("yyyy’-‘MM’-‘dd’T’HH’:’mm’:’ss"), ConvertUtil.ConvertObjectToXmlAttributeString(dateTime));
            Assert.IsNull(ConvertUtil.ConvertObjectToXmlAttributeString(null));
            Assert.AreEqual("832", ConvertUtil.ConvertObjectToXmlAttributeString(832));
            Assert.AreEqual("832.382", ConvertUtil.ConvertObjectToXmlAttributeString(832.382));
            Assert.AreEqual("jet", ConvertUtil.ConvertObjectToXmlAttributeString("jet"));
            Assert.AreEqual("0", ConvertUtil.ConvertObjectToXmlAttributeString(false));
            Assert.AreEqual("#NAME?", ConvertUtil.ConvertObjectToXmlAttributeString(ExcelErrorValue.Create(eErrorType.Name)));
        }
Esempio n. 5
0
        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));
        }
Esempio n. 6
0
        /// <summary>
        /// Creates a new <see cref="CacheRecordNode"/> and items as specified by the <paramref name="row"/> values.
        /// Adds the resulting <see cref="CacheRecordNode"/> to the specified <paramref name="parentNode"/>.
        /// </summary>
        /// <param name="namespaceManager">The namespace manager.</param>
        /// <param name="parentNode">The parent <see cref="ExcelPivotCacheRecords"/> <see cref="XmlNode"/>.</param>
        /// <param name="row">A list of object values that this node represents.</param>
        /// <param name="cacheDefinition">The parent <see cref="ExcelPivotCacheDefinition"/>.</param>
        public CacheRecordNode(XmlNamespaceManager namespaceManager, XmlNode parentNode, IEnumerable <object> row, ExcelPivotCacheDefinition cacheDefinition)
        {
            if (parentNode == null)
            {
                throw new ArgumentNullException(nameof(parentNode));
            }
            if (namespaceManager == null)
            {
                throw new ArgumentNullException(nameof(namespaceManager));
            }
            if (row == null)
            {
                throw new ArgumentNullException(nameof(row));
            }
            if (cacheDefinition == null)
            {
                throw new ArgumentNullException(nameof(cacheDefinition));
            }
            if (row.Count() != cacheDefinition.CacheFields.Count)
            {
                throw new InvalidOperationException("An attempt was made to create a CacheRecord node with an invalid number of fields.");
            }
            this.NameSpaceManager = namespaceManager;
            var recordNode = parentNode.OwnerDocument.CreateElement("d:r");
            int col        = 0;

            foreach (var value in row)
            {
                var type       = CacheItem.GetObjectType(value);
                var cacheField = cacheDefinition.CacheFields[col];
                if (cacheField.HasSharedItems)
                {
                    // The corresponding cacheField has shared items; map the new cacheRecord entry
                    // into shared items if a matching entry exists, otherwise create a new sharedItem entry and map accordingly.
                    var indexStringValue = this.GetCacheFieldSharedItemIndexString(cacheField, type, value);
                    var item             = new CacheItem(namespaceManager, recordNode, PivotCacheRecordType.x, indexStringValue);
                    item.AddSelf(recordNode);
                    myItems.Add(item);
                }
                else
                {
                    // If no SharedItems exist, simply create a record item entry.
                    var stringValue = ConvertUtil.ConvertObjectToXmlAttributeString(value);
                    var item        = new CacheItem(namespaceManager, recordNode, type, stringValue);
                    item.AddSelf(recordNode);
                    myItems.Add(item);
                }
                col++;
            }
            parentNode.AppendChild(recordNode);
        }
Esempio n. 7
0
        /// <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);
        }
Esempio n. 8
0
        /// <summary>
        /// Update the existing <see cref="CacheRecordNode"/>.
        /// </summary>
        /// <param name="row">The row of data from the source table.</param>
        /// <param name="cacheDefinition">The cacheDefinition.</param>
        public void Update(IEnumerable <object> row, ExcelPivotCacheDefinition cacheDefinition)
        {
            if (row == null)
            {
                throw new ArgumentNullException(nameof(row));
            }
            if (cacheDefinition == null)
            {
                throw new ArgumentNullException(nameof(cacheDefinition));
            }
            if (row.Count() != this.Items.Count)
            {
                throw new InvalidOperationException("An attempt was made to update a CacheRecordNode with a different number of fields.");
            }
            int col = 0;

            foreach (var value in row)
            {
                var type        = CacheItem.GetObjectType(value);
                var currentItem = myItems[col];
                var cacheField  = cacheDefinition.CacheFields[col];
                if (cacheField.HasSharedItems)
                {
                    // If shared items contains value, update this.Value to index
                    // otherwise, create and add new sharedItem, update this.Value to new index
                    currentItem.Value = this.GetCacheFieldSharedItemIndexString(cacheField, type, value);
                }
                else
                {
                    // If only the value changed, update it. If the type changed,
                    // replace the node with one of the correct type and value.
                    string stringValue = ConvertUtil.ConvertObjectToXmlAttributeString(value);
                    if (currentItem.Type == type && currentItem.Value != stringValue)
                    {
                        currentItem.Value = stringValue;
                    }
                    else if (currentItem.Type != type)
                    {
                        currentItem.ReplaceNode(type, stringValue, this.Node);
                    }
                }
                col++;
            }
        }
Esempio n. 9
0
        public void ConvertObjectToXmlAttributeString()
        {
            var dateTime = DateTime.Parse("2007-07-23T00:00:00");

            Assert.AreEqual("2007-07-23T00:00:00", ConvertUtil.ConvertObjectToXmlAttributeString(dateTime));
            Assert.IsNull(ConvertUtil.ConvertObjectToXmlAttributeString(null));
            Assert.AreEqual("832", ConvertUtil.ConvertObjectToXmlAttributeString(832));
            Assert.AreEqual("832.382", ConvertUtil.ConvertObjectToXmlAttributeString(832.382));

            // Values close to integers are rounded in order to avoid duplicating values that Excel has created.
            Assert.AreEqual("832", ConvertUtil.ConvertObjectToXmlAttributeString(832.0000000000001));
            Assert.AreEqual("832.000000000001", ConvertUtil.ConvertObjectToXmlAttributeString(832.000000000001));

            // Trailing zeros are truncated to avoid causing problems with Excel.
            Assert.AreEqual("0", ConvertUtil.ConvertObjectToXmlAttributeString(0.0000000000000000000000000d));
            Assert.AreEqual("0", ConvertUtil.ConvertObjectToXmlAttributeString(0.00000000000000000000m));

            Assert.AreEqual("jet", ConvertUtil.ConvertObjectToXmlAttributeString("jet"));
            Assert.AreEqual("0", ConvertUtil.ConvertObjectToXmlAttributeString(false));
            Assert.AreEqual("#NAME?", ConvertUtil.ConvertObjectToXmlAttributeString(ExcelErrorValue.Create(eErrorType.Name)));
        }
Esempio n. 10
0
 public void ConvertObjectToXmlAttributeStringInvalidType()
 {
     ConvertUtil.ConvertObjectToXmlAttributeString(new List <int>());
 }