实验报告功能新增自动导入数据

This commit is contained in:
shenjianxing 2025-04-21 09:59:18 +08:00
parent ce2ccad0e0
commit 4a78da1f20
4 changed files with 93 additions and 23 deletions

View File

@ -1,7 +1,9 @@
using Newtonsoft.Json;
using QFramework;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using TMPro;
using UnityEngine;
using UnityEngine.SocialPlatforms.Impl;
@ -48,6 +50,29 @@ public class ScoreController : MonoSingleton<ScoreController>
}
public string GetModuleDictJson(string name = "")
{
var resultDict = new Dictionary<string, object>();
resultDict.Add("name", name);
foreach (var dataPair in moduleDict)
{
var data = dataPair.Value;
if (data.scoreDict != null)
{
foreach (var scoreStepPair in data.scoreDict)
{
var scoreStep = scoreStepPair.Value;
string key = $"{scoreStep.step}{scoreStep.name}";
resultDict[key] = scoreStep.value;
resultDict.Add(key + "Time", string.IsNullOrEmpty(scoreStep.time) == true ? "" : scoreStep.time);
}
}
}
return JsonConvert.SerializeObject(resultDict);
}
private void OnStart(OnModuleStart start)
{
@ -84,6 +109,8 @@ public class ScoreController : MonoSingleton<ScoreController>
{
scoreDict[key].value = 0;
}
DateTime currentTime = DateTime.Now;
scoreDict[key].time = currentTime.ToString(scoreDict[key].timeFormat);
}
else
{

View File

@ -9,33 +9,76 @@ public class AsposeHelper : MonoBehaviour
{
//public static void Writer(string json, Action callback = null)
//{
// // 加载Word文档
// Document doc = new Document(Global.reportDemoPath);
// JObject jObject = JObject.Parse(json);
// foreach (JProperty property in jObject.Properties())
// {
// string key = property.Name;
// string value = property.Value.ToString();
// doc.Range.Replace($"{{{key}}}", $"{value}", new FindReplaceOptions());
// }
// string filePath = ChinarFileController.SaveProject(Path.GetFileName(Global.reportDemoPath).Split('.')[1]);
// if (string.IsNullOrEmpty(filePath) == false)
// {
// doc.Save(filePath);
// }
// callback?.Invoke();
// // 替换文本
// //SaveWithDialog(doc, callback);
// //Debug.Log("文档处理完成,新文档已保存到: " + outputFilePath);
//}
public static void Writer(string json, Action callback = null)
{
// ¼ÓÔØWordÎĵµ
Document doc = new Document(Global.reportDemoPath);
JObject jObject = JObject.Parse(json);
JToken jToken = JToken.Parse(json);
TraverseAndReplace(jToken, doc);
foreach (JProperty property in jObject.Properties())
string filePath = ChinarFileController.SaveProject(Path.GetFileName(Global.reportDemoPath).Split('.')[1]);
if (!string.IsNullOrEmpty(filePath))
{
doc.Save(filePath);
}
callback?.Invoke();
}
private static void TraverseAndReplace(JToken jToken, Document doc)
{
if (jToken.Type == JTokenType.Object)
{
foreach (JProperty property in ((JObject)jToken).Properties())
{
if (property.Value.Type == JTokenType.Object || property.Value.Type == JTokenType.Array)
{
TraverseAndReplace(property.Value, doc);
}
else
{
string key = property.Name;
string value = property.Value.ToString();
doc.Range.Replace($"{{{key}}}", $"{value}", new FindReplaceOptions());
}
string filePath = ChinarFileController.SaveProject(Path.GetFileName(Global.reportDemoPath).Split('.')[1]);
if (string.IsNullOrEmpty(filePath) == false)
}
}
else if (jToken.Type == JTokenType.Array)
{
doc.Save(filePath);
foreach (JToken item in jToken)
{
TraverseAndReplace(item, doc);
}
callback?.Invoke();
// 替换文本
//SaveWithDialog(doc, callback);
//Debug.Log("文档处理完成,新文档已保存到: " + outputFilePath);
}
}
//private static void SaveWithDialog(Document doc, Action<DialogResult> callback)
//{

View File

@ -58,11 +58,7 @@ namespace QFramework.Example
public string GetScoreDataJson()
{
var data = new LabReprotData();
data.realname = InputName.text;
data.biaobencaiji_1_buzhou_1 = "[1111]";
string json = JsonConvert.SerializeObject(data);
return json;
return ScoreController.Instance.GetModuleDictJson(InputName.text);
}
@ -131,9 +127,9 @@ namespace QFramework.Example
this.Score.text = score.ToString();
this.Sum.text = sum.ToString();
//#if UNITY_WEBGL
// LYTWebGLHelper.Instance.UpLoadData((int)score, stepNames, maxScore, scores);
//#endif
//#if UNITY_WEBGL
// LYTWebGLHelper.Instance.UpLoadData((int)score, stepNames, maxScore, scores);
//#endif
}
protected override void OnShow()

View File

@ -1,5 +1,6 @@
using QFramework;
using QFramework.Example;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
@ -73,6 +74,8 @@ namespace XMLTool
public string sum;
public string bind;
public float value;
public string time;
public string timeFormat = "yyyy-MM-dd HH:mm:ss";
}
@ -431,6 +434,7 @@ namespace XMLTool
name = item.Attribute("name")?.Value,
sum = item.Attribute("sum")?.Value,
bind = item.Attribute("bind")?.Value,
timeFormat = item.Attribute("timeFormat")?.Value,
});
}
}