Esempio n. 1
0
        public void FillVariableValue(Dictionary <string, object> Data, IVTWorksheet OtherRange = null)
        {
            foreach (IRangeRow row in Range.Rows)
            {
                foreach (IRange cell in row.Columns)
                {
                    string value = cell.Value == null ? "" : cell.Value.ToString();
                    // value = ${abc[].xyz.123}
                    MatchCollection matches = Regex.Matches(value, @"\$\{([^\}]+)\}");

                    foreach (Match match in matches)
                    {
                        // Finally, we get the Group value and display it.
                        string key = match.Groups[1].Value;

                        string[] arrKey = key.Split('.');
                        string   key0   = arrKey[0];
                        if (key0.EndsWith("[]"))
                        {
                            object        val0   = Data.ContainsKey(key0.Replace("[]", "")) ? Data[key0.Replace("[]", "")] : new List <object>();
                            object        val    = null;
                            List <object> list   = (List <object>)val0;
                            IRange        cell1  = cell;
                            string        value1 = value;
                            if (list.Count > 0)
                            {
                                foreach (object o in list)
                                {
                                    val = o;
                                    for (int i = 1; i < arrKey.Count(); i++)
                                    {
                                        string s = arrKey[i];
                                        val = GetPropertyValue(val, s);
                                    }

                                    if (value == "${" + key + "}")
                                    {
                                        cell1.Formula = val;
                                        if (OtherRange != null)
                                        {
                                            OtherRange.SetCellValue(cell1.Address().Replace("$", ""), val);
                                        }
                                    }
                                    else
                                    {
                                        value1        = value1.Replace("${" + key + "}", val == null ? "" : val.ToString());
                                        cell1.Formula = value1;
                                        if (OtherRange != null)
                                        {
                                            OtherRange.SetCellValue(cell1.Address().Replace("$", ""), value1);
                                        }
                                    }
                                    cell1 = Sheet.Worksheet.Range[cell1.Row + 1, cell1.Column];
                                }
                            }
                            else
                            {
                                cell1.Formula = "";
                            }
                        }
                        else
                        {
                            object val0 = Data.ContainsKey(arrKey[0]) ? Data[arrKey[0]] : null;
                            object val  = null;
                            foreach (string s in arrKey)
                            {
                                if (val == null)
                                {
                                    val = val0;
                                }
                                else
                                {
                                    val = GetPropertyValue(val, s);
                                }
                            }
                            if (value == "${" + key + "}")
                            {
                                cell.Formula = val;
                                if (OtherRange != null)
                                {
                                    OtherRange.SetCellValue(cell.Address().Replace("$", ""), val);
                                }
                            }
                            else
                            {
                                value        = value.Replace("${" + key + "}", val == null ? "" : val.ToString());
                                cell.Formula = value;
                                if (OtherRange != null)
                                {
                                    OtherRange.SetCellValue(cell.Address().Replace("$", ""), value);
                                }
                            }
                        }
                    }
                }
            }
        }