/// <summary> /// Get the template for an item given the TemplateSource and Global or spreadsheet values /// </summary> /// <returns>Sitecore TemplateItem</returns> private SCItems.Item getParent(XlsMapping mappingData) { if (mappingData.ParentIDSource == Enumerations.ParentSource.Global) { return(this._parent); } else { string parentFieldVal = mappingData.Source.DataSource.Rows[this._currentItemIndex][mappingData.ParentColumn].ToString(); var referenceMatch = this._referenceRegEx.Match(parentFieldVal); Guid parentGuid = Guid.Empty; if (referenceMatch.Success) { var captures = referenceMatch.Groups.Cast <Group>().Skip(1).Where(g => g.Success); string sheetName = captures.ElementAt(0).Value; // convert to 0 index row int row = int.Parse(captures.ElementAt(1).Value) - 1; XlsMappingSource parentSource = this.MappingSources.FirstOrDefault(ms => ms.Name == sheetName); if (parentSource != null) { parentFieldVal = parentSource.DataSource.Rows[row]["SitecoreID"].ToString(); } else { parentFieldVal = ""; } } parentGuid = Guid.Parse(parentFieldVal); return(this.GetItem(parentGuid)); } }
/// <summary> /// Adds a new Mapping from ImportSources to the ImportMappings using ImportSource index /// </summary> /// <param name="sourceIndex">Index of MappingSource to add according to ExcelWorksheet number</param> /// <param name="firstRowLabels">Specify whether first row of Source is label row</param> /// <returns>New Mapping</returns> public XlsMapping AddMapping(int sourceIndex, bool firstRowLabels = false) { XlsMappingSource source = this.MappingSources.FirstOrDefault(ms => ms.WorksheetIndex == sourceIndex); if (source == null) { throw new NullReferenceException(string.Format("No MappingSource found with specified index of {0}.", sourceIndex)); } XlsMapping mapping = new XlsMapping(source, firstRowLabels); this.addMapping(mapping); return(mapping); }
/// <summary> /// Adds a new Mapping from ImportSources to the ImportMappings using ImportSource name /// </summary> /// <param name="sourceName">Name of MappingSource to add according to ExcelWorksheet name</param> /// <param name="firstRowLabels">Specify whether first row of Source is label row</param> /// <returns>New Mapping</returns> public XlsMapping AddMapping(string sourceName, bool firstRowLabels = false) { XlsMappingSource mapSource = this.MappingSources.FirstOrDefault(ms => ms.Name == sourceName); if (mapSource == null) { StringBuilder sb = new StringBuilder(); if (string.IsNullOrWhiteSpace(sourceName)) { sb.Append("SourceName cannot be empty."); } else { sb.AppendFormat("Could not find MappingSource with name \"{0}\". Verify a MappingSource with the supplied name exists.", sourceName); } throw new ArgumentException(sb.ToString(), "sourceName"); } XlsMapping mapping = new XlsMapping(mapSource, firstRowLabels); this.addMapping(mapping); return(mapping); }