ProjectDDD/Assets/0.Datas/02.Scripts/GenerateGoogleSheet/Core/GoogleSheetDiffHelper.cs

73 lines
2.2 KiB (Stored with Git LFS)
C#

using Newtonsoft.Json.Linq;
using System.Collections.Generic;
public static class GoogleSheetDiffHelper
{
public static List<GoogleSheetDiff> CompareJsonDiff(string oldJson, string newJson)
{
var diffs = new List<GoogleSheetDiff>();
if (string.IsNullOrEmpty(oldJson) || string.IsNullOrEmpty(newJson))
return diffs;
var oldObj = JObject.Parse(oldJson);
var newObj = JObject.Parse(newJson);
foreach (var sheet in newObj)
{
var sheetName = sheet.Key;
if (!oldObj.TryGetValue(sheetName, out var oldSheetToken))
continue;
var oldArray = oldSheetToken as JArray;
var newArray = sheet.Value as JArray;
for (int i = 1; i < newArray.Count; i++)
{
if (i >= oldArray.Count)
break;
var newRow = (JObject)newArray[i];
var oldRow = (JObject)oldArray[i];
foreach (var prop in newRow.Properties())
{
var field = prop.Name;
string newValue = prop.Value.ToString();
string oldValue = oldRow.TryGetValue(field, out var oldVal) ? oldVal.ToString() : "";
if (oldValue != newValue)
{
diffs.Add(new GoogleSheetDiff
{
Sheet = sheetName,
Field = field,
RowIndex = i,
OldValue = oldValue,
NewValue = newValue
});
}
}
}
}
return diffs;
}
public static string GenerateDiff(string oldJson, string newJson)
{
var diffs = CompareJsonDiff(oldJson, newJson);
if (diffs.Count == 0)
return "No differences found.";
var sb = new System.Text.StringBuilder();
sb.AppendLine("[GoogleSheetManager] 변경된 필드들:");
foreach (var diff in diffs)
{
sb.AppendLine($"{diff.Sheet} / Row {diff.RowIndex} / {diff.Field} : '{diff.OldValue}' → '{diff.NewValue}'");
}
return sb.ToString();
}
}