Пример #1
0
    /// <summary>
    /// Import the specified excel file and prepare to set type of each cell.
    /// </summary>
    protected override void Import(bool reimport = false)
    {
        base.Import(reimport);

        ExcelMachine machine = target as ExcelMachine;

        string path = machine.excelFilePath;
        string sheet = machine.WorkSheetName;

        if (string.IsNullOrEmpty(path))
        {
            EditorUtility.DisplayDialog(
                "Error",
                "You should specify spreadsheet file first!",
                "OK"
            );
            return;
        }

        if (!File.Exists(path))
        {
            EditorUtility.DisplayDialog(
                "Error",
                "File at " + path + " does not exist.",
                "OK"
            );
            return;
        }

        var titles = new ExcelQuery(path, sheet).GetTitle();
        List<string> titleList = titles.ToList();

        if (machine.HasHeadColumn() && reimport == false)
        {
            var headerDic = machine.HeaderColumnList.ToDictionary(header => header.name);

            int i = 0;
            // collect non changed header columns
            var exist = from t in titleList
                        where headerDic.ContainsKey(t) == true
                        select new HeaderColumn { name = t, type = headerDic[t].type, OrderNO = headerDic[t].OrderNO };

            // collect newly added or changed header columns
            var changed = from t in titleList
                          where headerDic.ContainsKey(t) == false
                          select new HeaderColumn { name = t, type = CellType.Undefined, OrderNO = titleList.IndexOf(t) };

            // merge two
            var merged = exist.Union(changed).OrderBy(x => x.OrderNO);

            machine.HeaderColumnList.Clear();
            machine.HeaderColumnList = merged.ToList();
        }
        else
        {
            machine.HeaderColumnList.Clear();

            if (titles != null)
            {
                int i = 0;
                foreach (string s in titles)
                {
                    machine.HeaderColumnList.Add(new HeaderColumn { name = s, type = CellType.Undefined, OrderNO = i});
                    i++;
                }
            }
            else
            {
                Debug.LogWarning("The WorkSheet [" + sheet + "] may be empty.");
            }
        }

        EditorUtility.SetDirty(machine);
        AssetDatabase.SaveAssets();
    }
    /// <summary>
    /// Import the specified excel file and prepare to set type of each cell.
    /// </summary>
    protected override void Import(bool reimport = false)
    {
        base.Import(reimport);

        ExcelMachine machine = target as ExcelMachine;

        string path  = machine.excelFilePath;
        string sheet = machine.WorkSheetName;

        if (string.IsNullOrEmpty(path))
        {
            EditorUtility.DisplayDialog(
                "Error",
                "You should specify spreadsheet file first!",
                "OK"
                );
            return;
        }

        if (!File.Exists(path))
        {
            EditorUtility.DisplayDialog(
                "Error",
                "File at " + path + " does not exist.",
                "OK"
                );
            return;
        }

        var           titles    = new ExcelQuery(path, sheet).GetTitle();
        List <string> titleList = titles.ToList();

        if (machine.HasHeadColumn() && reimport == false)
        {
            var headerDic = machine.HeaderColumnList.ToDictionary(header => header.name);

            // collect non changed header columns
            var exist = from t in titleList
                        where headerDic.ContainsKey(t) == true
                        select new HeaderColumn {
                name = t, type = headerDic[t].type, OrderNO = headerDic[t].OrderNO
            };

            // collect newly added or changed header columns
            var changed = from t in titleList
                          where headerDic.ContainsKey(t) == false
                          select new HeaderColumn {
                name = t, type = CellType.Undefined, OrderNO = titleList.IndexOf(t)
            };

            // merge two
            var merged = exist.Union(changed).OrderBy(x => x.OrderNO);

            machine.HeaderColumnList.Clear();
            machine.HeaderColumnList = merged.ToList();
        }
        else
        {
            machine.HeaderColumnList.Clear();

            if (titles != null)
            {
                int i = 0;
                foreach (string s in titles)
                {
                    machine.HeaderColumnList.Add(new HeaderColumn {
                        name = s, type = CellType.Undefined, OrderNO = i
                    });
                    i++;
                }
            }
            else
            {
                Debug.LogWarning("The WorkSheet [" + sheet + "] may be empty.");
            }
        }

        EditorUtility.SetDirty(machine);
        AssetDatabase.SaveAssets();
    }