static IEnumerable <IMergeError> ReplaceMergeFieldsAndReturnMissingFieldNames(XmlDocument xdoc, Dictionary <string, string> dict, Engine engine) { var fields = GetFields(xdoc); var errors = new List <IMergeError> (); var properties = new Properties(dict); foreach (var field in fields) { var matches = regex.Match(field.InstrText); if (matches.Success) { var fieldName = matches.Groups ["name"].Value; var format = matches.Groups ["format"].Captures; var exp = engine.Parse(fieldName); if (exp == null) { errors.Add(new MergeError(v => v.InvalidExpression(fieldName))); } else { var missingProperties = new List <string> (); properties.FindMissingProperties(exp, missingProperties); errors.AddRange(missingProperties.Select(p => new MergeError(v => v.MissingField(p)))); if (missingProperties.Count == 0) // otherwise Eval will throw { field.Value = ApplyFormat(properties.Eval(exp), format); } } } } return(errors); }
internal string Eval(string expression, Properties properties) { IExpression exp = Parse(expression); return(exp == null ? null : properties.Eval(exp)); }
public static IEnumerable <IMergeError> ReplaceMergeFieldsAndReturnMissingFieldNames( XmlDocument sharedStringsDoc, List <XmlDocument> sheetDocList, Dictionary <string, string> replacements, Engine engine) { var fields = GetSharedStrings(sharedStringsDoc); var errors = new List <IMergeError>(); var properties = new Properties(replacements); int stringIdx = 0; foreach (var field in fields) { var value = field.Text; var matches = regex.Match(field.Text); if (matches.Success) { var fieldNames = matches.Groups["name"].Captures; var fieldTemplates = matches.Groups["template"].Captures; for (var fieldIdx = 0; fieldIdx < fieldNames.Count; ++fieldIdx) { var fieldName = fieldNames[fieldIdx].ToString(); var fieldTemlate = fieldTemplates[fieldIdx].ToString(); var exp = engine.Parse(fieldName); if (exp == null) { errors.Add(new MergeError(v => v.InvalidExpression(fieldName))); } else { var missingProperties = new List <string>(); properties.FindMissingProperties(exp, missingProperties); errors.AddRange(missingProperties.Select(p => new MergeError(v => v.MissingField(p)))); if (missingProperties.Count == 0) // otherwise Eval will throw { value = value.Replace(fieldTemlate, properties.Eval(exp)); } } } if (value != field.Text) { double dresult; if (Double.TryParse(value.Trim(), out dresult)) { foreach (var sheetDoc in sheetDocList) { var cells = GetStringCells(sheetDoc, stringIdx); foreach (var cell in cells) { cell.DoubleValue = dresult; } } value = ""; } } field.StringValue = value; } ++stringIdx; } return(errors); }