Excel導表工具(開源)

小紫蘇發表於2022-05-31

功能

image-20220531144853430

  • 支援int、float、bool、string基礎型別
  • 支援陣列
  • 支援kv
  • 支援列舉
  • 支援unity型別vector3,vector2,color
  • 自動生成csharp類
  • 單個excel中多個sheet,依次匯出

使用

  1. 設定config.txt檔案,按需求配置;

#為註釋行必須;結尾

#excel存放路徑;
excelPath:./Excel/;
#資料儲存路徑;
dataPath:./DataTable/;
#c#類儲存路徑;
classPath:./CSharp/;
#輸出型別;
exportType:Json;
isExportServer:False
  1. 雙擊執行DataTable.exe,等待執行完畢;

配表

  • 第一行註釋

  • 第二行欄位型別

  • 第三行變數名(屬性名)

  • 第一列留空

  • 陣列:型別+[] e.g: int[]

  • kv使用

    型別:dic<string,int>

    變數名:變數名+:+key值

    e.g:

    dic<string,float> dic<string,float> dic<string,float>
    Attribute:atk Attribute:def Attribute:spd
  • 列舉:自動生成的列舉型別從1開始,Enum型別為:Enum+變數名欄位;

Json序列化

使用json庫需要對Vector3等Unity欄位魔改;

Litjson庫魔改:將自定義型別註冊進json庫;

namespace LitJson.Extensions
{
    public static class JsonExtensions
    {

        public static void WriteProperty(this JsonWriter w, string name, long value)
        {
            w.WritePropertyName(name);
            w.Write(value);
        }

        public static void WriteProperty(this JsonWriter w, string name, string value)
        {
            w.WritePropertyName(name);
            w.Write(value);
        }

        public static void WriteProperty(this JsonWriter w, string name, bool value)
        {
            w.WritePropertyName(name);
            w.Write(value);
        }

        public static void WriteProperty(this JsonWriter w, string name, double value)
        {
            w.WritePropertyName(name);
            w.Write(value);
        }

    }
}

 using UnityEngine;
 using System;
 using System.Collections;
 
 using LitJson.Extensions;

 namespace LitJson
 {

#if UNITY_EDITOR
     [UnityEditor.InitializeOnLoad]
#endif
     /// <summary>
     /// Unity內建型別擴充
     /// </summary>
     public static class UnityTypeBindings
     {

         static bool registerd;

         static UnityTypeBindings()
         {
             Register();
         }

         public static void Register()
         {

             if (registerd) return;
             registerd = true;


             // 註冊Type型別的Exporter
             JsonMapper.RegisterExporter<Type>((v, w) => { w.Write(v.FullName); });

             JsonMapper.RegisterImporter<string, Type>((s) => { return Type.GetType(s); });

             // 註冊Vector2型別的Exporter
             Action<Vector2, JsonWriter> writeVector2 = (v, w) =>
             {
                 w.WriteObjectStart();
                 w.WriteProperty("x", v.x);
                 w.WriteProperty("y", v.y);
                 w.WriteObjectEnd();
             };

             JsonMapper.RegisterExporter<Vector2>((v, w) => { writeVector2(v, w); });

             // 註冊Vector3型別的Exporter
             Action<Vector3, JsonWriter> writeVector3 = (v, w) =>
             {
                 w.WriteObjectStart();
                 w.WriteProperty("x", v.x);
                 w.WriteProperty("y", v.y);
                 w.WriteProperty("z", v.z);
                 w.WriteObjectEnd();
             };

             JsonMapper.RegisterExporter<Vector3>((v, w) => { writeVector3(v, w); });

             // 註冊Vector4型別的Exporter
             JsonMapper.RegisterExporter<Vector4>((v, w) =>
             {
                 w.WriteObjectStart();
                 w.WriteProperty("x", v.x);
                 w.WriteProperty("y", v.y);
                 w.WriteProperty("z", v.z);
                 w.WriteProperty("w", v.w);
                 w.WriteObjectEnd();
             });

             // 註冊Quaternion型別的Exporter
             JsonMapper.RegisterExporter<Quaternion>((v, w) =>
             {
                 w.WriteObjectStart();
                 w.WriteProperty("x", v.x);
                 w.WriteProperty("y", v.y);
                 w.WriteProperty("z", v.z);
                 w.WriteProperty("w", v.w);
                 w.WriteObjectEnd();
             });

             // 註冊Color型別的Exporter
             JsonMapper.RegisterExporter<Color>((v, w) =>
             {
                 w.WriteObjectStart();
                 w.WriteProperty("r", v.r);
                 w.WriteProperty("g", v.g);
                 w.WriteProperty("b", v.b);
                 w.WriteProperty("a", v.a);
                 w.WriteObjectEnd();
             });

             // 註冊Color32型別的Exporter
             JsonMapper.RegisterExporter<Color32>((v, w) =>
             {
                 w.WriteObjectStart();
                 w.WriteProperty("r", v.r);
                 w.WriteProperty("g", v.g);
                 w.WriteProperty("b", v.b);
                 w.WriteProperty("a", v.a);
                 w.WriteObjectEnd();
             });

             // 註冊Bounds型別的Exporter
             JsonMapper.RegisterExporter<Bounds>((v, w) =>
             {
                 w.WriteObjectStart();

                 w.WritePropertyName("center");
                 writeVector3(v.center, w);

                 w.WritePropertyName("size");
                 writeVector3(v.size, w);

                 w.WriteObjectEnd();
             });

             // 註冊Rect型別的Exporter
             JsonMapper.RegisterExporter<Rect>((v, w) =>
             {
                 w.WriteObjectStart();
                 w.WriteProperty("x", v.x);
                 w.WriteProperty("y", v.y);
                 w.WriteProperty("width", v.width);
                 w.WriteProperty("height", v.height);
                 w.WriteObjectEnd();
             });

             // 註冊RectOffset型別的Exporter
             JsonMapper.RegisterExporter<RectOffset>((v, w) =>
             {
                 w.WriteObjectStart();
                 w.WriteProperty("top", v.top);
                 w.WriteProperty("left", v.left);
                 w.WriteProperty("bottom", v.bottom);
                 w.WriteProperty("right", v.right);
                 w.WriteObjectEnd();
             });

         }

     }
 }

後續計劃加入protobuf和bytes匯出;

計劃可匯出.go檔案;

相比其他導表工具,優勢就是簡單使用,不用看長長的文件;

有bug或修改建議歡迎交流;

開源地址:https://github.com/Rebort1012/DataTable.git

個人部落格:perilla.work

相關文章