Skip to content
This repository has been archived by the owner on Sep 11, 2021. It is now read-only.

Commit

Permalink
#1 간단한 펼치기 가능
Browse files Browse the repository at this point in the history
복잡한 테스트 하지 않음.
껐다 다시 켰을 때 안 열리는 문제 있음..
  • Loading branch information
jkwchunjae committed Apr 17, 2019
1 parent 638dc09 commit 8e78087
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ public enum DataType
public class CellData
{
public DataType Type { get; set; }
public string Address { get; set; }
public Excel.Range Cell { get; set; }
public int Index { get; set; }
public IJsonToken Key { get; set; }
public JsonTitle Key { get; set; }
public IJsonToken Value { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ private IEnumerable<CellData> MakeCellData(Excel.Worksheet sheet, JArray token)
{
Type = DataType.Value,
Index = x.Index,
Address = x.Cell?.Address,
Cell = x.Cell,
Key = null,
Value = x.JToken,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public class JsonObject : IJsonToken
public JToken GetToken() => _token;
public string Path() => GetToken()?.Path;

public IEnumerable<JProperty> Keys => _cellDatas
public IEnumerable<string> Keys => _cellDatas
.Where(x => x.Type == DataType.Key)
.Select(x => (JProperty)x.Key.GetToken());
.Select(x => x.Key.Title);

public JsonObject(JObject jObject)
{
Expand Down Expand Up @@ -83,23 +83,21 @@ private IEnumerable<CellData> MakeCellData(Excel.Worksheet sheet, JObject token)
{
Index = i,
Property = x,
PropertyToken = x.CreateJsonToken(),
PropertyToken = new JsonTitle(x.Name),
ValueToken = x.Value.CreateJsonToken(),
})
.SelectMany(x => new CellData[]
{
new CellData
{
Type = DataType.Key,
Address = ((Excel.Range)sheet?.Cells[x.Index + _titleRow + 1, 1])?.Address,
Cell = (Excel.Range)sheet?.Cells[x.Index + _titleRow + 1, 1],
Key = x.PropertyToken,
Value = x.PropertyToken,
},
new CellData
{
Type = DataType.Value,
Address = ((Excel.Range)sheet?.Cells[x.Index + _titleRow + 1, 2])?.Address,
Cell = (Excel.Range)sheet?.Cells[x.Index + _titleRow + 1, 2],
Key = x.PropertyToken,
Value = x.ValueToken,
Expand All @@ -110,7 +108,7 @@ private void SetNamedRange(Excel.Worksheet sheet, IEnumerable<CellData> keys)
{
keys.ToList().ForEach(x =>
{
var propertyName = ((JProperty) x.Key.GetToken()).Name;
var propertyName = x.Key.Title;
sheet.Names.Add(propertyName, x.Cell.Offset[0, 1]);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ public void Spread(Excel.Worksheet sheet)
{
_sheet = sheet;

_cellDatas = MakeCellData(_sheet, _token).ToList();
_cellDatas = MakeTitle(_sheet, _token).ToList();
FillCellData(_sheet, _token, _cellDatas);


SetNamedRange(sheet, _cellDatas.Where(x => x.Type == DataType.Title));

Expand All @@ -51,6 +53,60 @@ public bool OnDoubleClick(Excel.Workbook book, Excel.Range target)
{
book.SpreadJsonToken(_sheet, cellData.Value);
}
if (cellData.Value.Type() == JsonTokenType.Title)
{
Globals.ThisAddIn.Application.EnableEvents = false;
if (cellData.Key.Extended)
{
var left = cellData.Cell.Offset[0, 1];
var right = cellData.Cell.Offset[0, cellData.Key.ColumnCount - 1];

var childs = cellData.Key.ChildTitles;
_cellDatas = _cellDatas.Where(x => !childs.Contains(x.Key)).ToList();
childs.ForEach(x => cellData.Key.RemoveChildTitle(x));

_sheet.Range[left, right].EntireColumn.Delete();
}
else
{
var titles = _cellDatas.Where(x => x.Cell.Column == cellData.Cell.Column)
.Where(x => x.Type == DataType.Value)
.Select(x => (JsonObject)x.Value)
.SelectMany(x => x.Keys)
.Distinct()
.Select((x, i) => new
{
Column = cellData.Cell.Column + i + 1,
Path = $"{cellData.Key.Title}.{x}",
})
.ToList();

var left = cellData.Cell.Offset[0, 1];
var right = cellData.Cell.Offset[0, titles.Count()];
_sheet.Range[left, right].EntireColumn.Insert(Excel.XlInsertShiftDirection.xlShiftToRight);

var titleCellDatas = titles
.Select(x => new
{
Cell = _sheet.Cells[1, x.Column],
JsonTitle = new JsonTitle(x.Path),
})
.Select(x => new CellData
{
Type = DataType.Title,
Cell = x.Cell,
Key = x.JsonTitle,
Value = x.JsonTitle,
})
.ToList();

titleCellDatas.ForEach(x => x.Key.Spread(x.Cell));
titleCellDatas.ForEach(x => cellData.Key.AddChildTitle(x.Key));
_cellDatas.AddRange(titleCellDatas);
FillCellData(_sheet, _token, _cellDatas);
}
Globals.ThisAddIn.Application.EnableEvents = true;
}
return true;
}

Expand All @@ -66,60 +122,61 @@ public void OnChangeValue(Excel.Range target)
cellData?.Value.OnChangeValue(target);
}

private IEnumerable<CellData> MakeCellData(Excel.Worksheet sheet, JArray token)
private List<CellData> MakeTitle(Excel.Worksheet sheet, JArray token)
{
var titles = _token.Where(x => x.Type == JTokenType.Object)
var titles = token.Where(x => x.Type == JTokenType.Object)
.Select(x => (JsonObject)x.CreateJsonToken())
.SelectMany(x => x.Keys)
.GroupBy(x => x.Name)
.Select(x => new JsonTitle(x))
.GroupBy(x => x)
.Select(x => new JsonTitle(x.First()))
.Select((x, i) => new
{
Index = i,
JsonTitle = x,
Cell = (Excel.Range)sheet?.Cells[_titleRow, i + 1],
Cell = sheet.Cells[_titleRow, i + 1],
})
.Select(x => new CellData
{
Type = DataType.Title,
Address = x.Cell?.Address,
Cell = x.Cell,
Index = x.Index,
Key = x.JsonTitle,
Value = x.JsonTitle,
})
.ToList();

var columnDic = titles
.ToDictionary(x => ((JsonTitle)x.Key).Title, x => new { Column = x.Index + 1, JsonTitle = (JsonTitle)x.Key });
return titles;
}

private void FillCellData(Excel.Worksheet sheet, JArray token, List<CellData> cellDatas)
{
var titles = cellDatas.Where(x => x.Type == DataType.Title).ToList();

var datas = _token.Where(x => x.Type == JTokenType.Object)
var objects = token.Where(x => x.Type == JTokenType.Object)
.Select((x, i) => new { Row = _titleRow + i + 1, JsonObject = (JsonObject)x.CreateJsonToken() })
.SelectMany(x => x.JsonObject.Keys.Select(e => new { JsonProperty = e, JToken = e.Value, x.Row }))
.Select(x => new
{
x.Row,
Column = columnDic[x.JsonProperty.Name].Column,
x.JToken,
JsonTitle = columnDic[x.JsonProperty.Name].JsonTitle,
})
.ToList();

var newDatas = titles.Join(objects, a => 1, b => 1, (title, obj) => new { Title = title, Object = obj })
.Select(x => new
{
Cell = (Excel.Range)sheet?.Cells[x.Row, x.Column],
x.JToken,
x.JsonTitle,
x.Object.Row,
x.Title.Cell.Column,
x.Title,
JsonTitle = (JsonTitle)x.Title.Key,
Object = x.Object.JsonObject,
})
.Where(x => cellDatas.Empty(e => e.Cell.Row == x.Row && e.Cell.Column == x.Column))
.Select(x => new CellData
{
Type = DataType.Value,
Address = x.Cell?.Address,
Cell = x.Cell,
Cell = sheet.Cells[x.Row, x.Column],
Key = x.JsonTitle,
Value = x.JToken.CreateJsonToken(),
Value = x.Object.GetToken().SelectToken(x.JsonTitle.Title).CreateJsonToken(),
})
.ToList();

return titles.Concat(datas);
newDatas.ForEach(x => x.Value.Spread(x.Cell));
cellDatas.AddRange(newDatas);
}

private void SetNamedRange(Excel.Worksheet sheet, IEnumerable<CellData> titles)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,39 @@ namespace ExcelJsonEditorAddin.JsonTokenModel
public class JsonTitle : IJsonToken
{
private List<JProperty> _tokens;
public string Title => _tokens.FirstOrDefault()?.Name;
private List<JsonTitle> _childTitles = new List<JsonTitle>();
public string Title { get; private set; }

public JsonTokenType Type() => JsonTokenType.Title;
public JToken GetToken() => null;
public string Path() => GetToken()?.Path;

public JsonTitle(IEnumerable<JProperty> jProperties)
public bool Extended => _childTitles.Any();
public int ColumnCount => 1 + _childTitles.Sum(x => x.ColumnCount);
public List<JsonTitle> ChildTitles => _childTitles
.Concat(_childTitles.SelectMany(e => e.ChildTitles))
.ToList();

public JsonTitle(string path)
{
Title = path;
}

public void AddChildTitle(JsonTitle title)
{
_childTitles.Add(title);
}

public void RemoveChildTitle(JsonTitle title)
{
_tokens = jProperties.ToList();
if (_childTitles.Contains(title))
{
_childTitles.Remove(title);
}
else
{
_childTitles.ForEach(x => x.RemoveChildTitle(title));
}
}

public void Spread(Excel.Worksheet ws)
Expand Down
26 changes: 18 additions & 8 deletions ExcelJsonEditorAddin/ExcelJsonEditorAddin/json-array-object.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,34 @@
{
"Id": 1234,
"Name": "hahaha",
"Others": {
"Item1": "C#",
"Item2": "C++",
"Property": {
"Power": 456,
"ABCDEFG": "123qwe"
}
},
"Roles": [
"admin",
"user"
],
"Others": {
"A": "a",
"B": "b"
}
"Checked": false
},
{
"Id": 678,
"Name": "jkw",
"Others": {
"Item1": "JavaScript",
"Item2": "NodeJs",
"Property": {
"Power": 123,
"ABCDEFG": "123qwe"
}
},
"Roles": [
"User"
],
"Others": {
"A": "a",
"B": "b"
}
"Checked": false
}
]

0 comments on commit 8e78087

Please sign in to comment.