Example #1
0
        public void Initialize(IGoogleSpreadsheetClient client)
        {
            Reset();

            _client          = client;
            _spreadsheetData = client.SpreadsheetData;

            foreach (var importer in importers.Concat <ISpreadsheetTriggerAssetsHandler>(serializabledImporters))
            {
                importer.Initialize(client);
                importer.ExportCommand.
                Where(x => x.CanExport).
                Do(x => ExportSheets(Export(_spreadsheetData, x))).
                Subscribe().
                AddTo(LifeTime);

                importer.ImportCommand.
                Where(x => x.CanImport).
                Do(x => {
                    if (autoReloadSpreadsheetOnImport)
                    {
                        _client.ReloadAll();
                    }
                }).
                Do(x => Import(_spreadsheetData, x)).
                Subscribe().
                AddTo(LifeTime);
            }

            LifeTime.AddCleanUpAction(() => _client          = null);
            LifeTime.AddCleanUpAction(() => _spreadsheetData = null);
        }
Example #2
0
        public bool UpdateSheetValue(object source, ISpreadsheetData data, string sheetId = "", string sheetKeyField = "")
        {
            if (source == null)
            {
                return(false);
            }
            var type       = source.GetType();
            var syncScheme = type.CreateSheetScheme();

            var keyField = string.IsNullOrEmpty(sheetKeyField) ? syncScheme.keyField : syncScheme.GetFieldBySheetFieldName(sheetKeyField);

            if (keyField == null)
            {
                return(false);
            }

            var sheetValueInfo = new SheetValueInfo
            {
                Source          = source,
                SpreadsheetData = data,
                SyncScheme      = syncScheme,
                SyncFieldName   = sheetKeyField,
                SheetName       = sheetId
            };

            return(UpdateSheetValue(sheetValueInfo));
        }
Example #3
0
        public static object ApplySpreadsheetData(
            this object asset,
            ISpreadsheetData spreadsheetData,
            string sheetName,
            object keyValue       = null,
            string sheetFieldName = "")
        {
            if (spreadsheetData.HasSheet(sheetName) == false)
            {
                return(asset);
            }

            var syncAsset = asset.CreateSheetScheme();

            var sheetValueIndo = new SheetValueInfo
            {
                Source          = asset,
                SheetName       = sheetName,
                SpreadsheetData = spreadsheetData,
                SyncScheme      = syncAsset,
                SyncFieldName   = sheetFieldName,
                SyncFieldValue  = keyValue,
                StartColumn     = 0
            };

            return(DefaultProcessor.ApplyData(sheetValueIndo));
        }
Example #4
0
        /// <summary>
        /// Sync folder assets by spreadsheet data
        /// </summary>
        /// <param name="filterType"></param>
        /// <param name="assets"></param>
        /// <param name="folder"></param>
        /// <param name="createMissing">if true - create missing assets</param>
        /// <param name="spreadsheetData"></param>
        /// <param name="maxItemsCount"></param>
        /// <param name="overrideSheetId">force override target sheet id</param>
        /// <returns></returns>
        public List <Object> SyncFolderAssets(
            Type filterType,
            string folder,
            ISpreadsheetData spreadsheetData,
            Object[] assets        = null,
            bool createMissing     = true,
            int maxItemsCount      = -1,
            string overrideSheetId = "")
        {
            var result = assets != null ? new List <Object>(assets) : new List <Object>();

            if (!filterType.IsScriptableObject() && !filterType.IsComponent())
            {
                Debug.LogError($"SyncFolderAssets: BAD target type {filterType}");
                return(result);
            }

            var syncScheme = filterType.CreateSheetScheme();

            var sheetId = string.IsNullOrEmpty(overrideSheetId) ? syncScheme.sheetId : overrideSheetId;

            var sheet = spreadsheetData[sheetId];

            if (sheet == null)
            {
                Debug.LogWarning($"{nameof(AssetSheetDataProcessor)} Missing Sheet with name {sheetId}");
                return(result);
            }

            var keyField = syncScheme.keyField;

            if (keyField == null)
            {
                Debug.LogWarning($"{nameof(AssetSheetDataProcessor)} Key field missing sheet = {sheetId}");
                return(result);
            }

            var keysId = keyField.sheetField;
            var column = sheet.GetColumn(keysId);

            if (column == null)
            {
                Debug.LogWarning($"{nameof(AssetSheetDataProcessor)} Keys line missing with id = {keysId}");
                return(result);
            }

            var updatedItems = ApplyAssets(
                filterType,
                sheetId,
                folder,
                syncScheme,
                spreadsheetData,
                sheet.GetColumnValues(keysId).ToArray(),
                assets, maxItemsCount, createMissing);

            result.AddRange(updatedItems);

            return(result);
        }
Example #5
0
 public static List <Object> SyncFolderAssets(
     this Type type,
     string folder,
     bool createMissing,
     ISpreadsheetData spreadsheetData)
 {
     return(DefaultProcessor.SyncFolderAssets(type, folder, createMissing, spreadsheetData));
 }
        public override ISpreadsheetData ExportObjects(IEnumerable <object> source, ISpreadsheetData data)
        {
            foreach (var importer in source.OfType <ISpreadsheetAssetsExporter>().Where(x => x.CanExport))
            {
                importer.Export(data);
            }

            return(data);
        }
Example #7
0
        public ISpreadsheetData Export(ISpreadsheetData data)
        {
            foreach (var importer in Importers.Where(x => x.CanExport))
            {
                Export(data, importer);
            }

            return(data);
        }
Example #8
0
        public ISpreadsheetData ExportSheets(ISpreadsheetData data)
        {
            var sheets = data.Sheets.ToList();

            foreach (var sheetData in sheets.Where(sheetData => sheetData.IsChanged))
            {
                _client.Upload(sheetData);
            }

            return(data);
        }
Example #9
0
 public static List <Object> SyncFolderAssets(
     this Type filterType,
     string folder,
     ISpreadsheetData spreadsheetData,
     Object[] assets        = null,
     bool createMissing     = true,
     int maxItemsCount      = -1,
     string overrideSheetId = "")
 {
     return(DefaultProcessor.SyncFolderAssets(filterType, folder, spreadsheetData, assets, createMissing, maxItemsCount, overrideSheetId));
 }
 public override ISpreadsheetData ExportObjects(IEnumerable <object> source, ISpreadsheetData spreadsheetData)
 {
     foreach (var item in assets)
     {
         if (item.asset == null)
         {
             continue;
         }
         item.asset.UpdateSheetValue(spreadsheetData, item.sheetId);
     }
     return(spreadsheetData);
 }
Example #11
0
        public IEnumerable <object> Import(ISpreadsheetData spreadsheetData)
        {
            if (autoReloadSpreadsheetOnImport)
            {
                _client.ReloadAll();
            }

            var result = new List <object>();

            foreach (var importer in Importers.Where(x => x.CanImport))
            {
                result.AddRange(Import(spreadsheetData, importer));
            }

            return(result);
        }
Example #12
0
        public object ApplyData(object source, ISpreadsheetData spreadsheetData)
        {
            var syncScheme = source.CreateSheetScheme();

            var syncValue = new SheetValueInfo
            {
                Source          = source,
                SpreadsheetData = spreadsheetData,
                SheetName       = syncScheme.sheetId,
                SyncScheme      = syncScheme,
                SyncFieldName   = syncScheme.keyField.sheetField,
            };

            var result = ApplyData(syncValue);

            return(result);
        }
Example #13
0
        public static object ApplySpreadsheetData(
            this object asset,
            Type type,
            ISpreadsheetData sheetData,
            string sheetId,
            object keyValue       = null,
            string sheetFieldName = "")
        {
            var syncAsset = type.CreateSheetScheme();

            var sheetValue = new SheetValueInfo
            {
                Source          = asset,
                SheetName       = sheetId,
                SpreadsheetData = sheetData,
                SyncScheme      = syncAsset,
                SyncFieldName   = sheetFieldName,
                SyncFieldValue  = keyValue,
                StartColumn     = 0
            };

            return(DefaultProcessor.ApplyData(sheetValue));
        }
Example #14
0
        /// <summary>
        /// Sync folder assets by spreadsheet data
        /// </summary>
        /// <param name="filterType"></param>
        /// <param name="folder"></param>
        /// <param name="createMissing">if true - create missing assets</param>
        /// <param name="spreadsheetData"></param>
        /// <param name="maxItems"></param>
        /// <param name="overrideSheetId"></param>
        /// <returns></returns>
        public List <Object> SyncFolderAssets(
            Type filterType,
            string folder,
            bool createMissing,
            ISpreadsheetData spreadsheetData,
            int maxItems           = -1,
            string overrideSheetId = "")
        {
            if (!filterType.IsScriptableObject() && !filterType.IsComponent())
            {
                Debug.LogError($"SyncFolderAssets: BAD target type {filterType}");
                return(null);
            }

            var assets = AssetEditorTools.GetAssets <Object>(filterType, folder);
            var result = SyncFolderAssets(
                filterType,
                folder,
                spreadsheetData,
                assets.ToArray(),
                createMissing, maxItems, overrideSheetId);

            return(result);
        }
 public virtual ISpreadsheetData ExportObjects(IEnumerable <object> source, ISpreadsheetData spreadsheetData) => spreadsheetData;
 public virtual IEnumerable <object> ImportObjects(IEnumerable <object> source, ISpreadsheetData spreadsheetData)
 {
     return(source);
 }
        public override IEnumerable <object> ImportObjects(IEnumerable <object> source, ISpreadsheetData spreadsheetData)
        {
            var result = new List <object>();

            foreach (var item in source.OfType <SheetSyncItem>())
            {
                if (!spreadsheetData.HasSheet(item.sheetId) || item.asset == null)
                {
                    continue;
                }
                var asset = item.asset.
                            ApplySpreadsheetData(spreadsheetData, item.sheetId);
                result.Add(asset);
            }

            return(result);
        }
Example #18
0
 private ISpreadsheetData Export(ISpreadsheetData data, ISpreadsheetAssetsHandler importer)
 {
     importer.Load();
     return(importer.Export(data));
 }
Example #19
0
        public static object ApplySpreadsheetData(this object asset, SheetSyncScheme syncAsset, ISpreadsheetData data)
        {
            var sheetValueInfo = new SheetValueInfo
            {
                Source          = asset,
                SyncScheme      = syncAsset,
                SpreadsheetData = data,
                StartColumn     = 0
            };

            return(DefaultProcessor.ApplyData(sheetValueInfo));
        }
Example #20
0
 public abstract IEnumerable <object> Import(ISpreadsheetData spreadsheetData);
Example #21
0
        public IEnumerable <Object> ApplyAssets(
            Type filterType,
            string sheetId,
            string folder,
            SheetSyncScheme syncScheme,
            ISpreadsheetData spreadsheetData,
            object[] keys,
            Object[] assets     = null,
            int count           = -1,
            bool createMissing  = true,
            string keyFieldName = "")
        {
            count = count < 0 ? keys.Length : count;
            count = Math.Min(keys.Length, count);

            var keyField = string.IsNullOrEmpty(keyFieldName) ? syncScheme.keyField : syncScheme.GetFieldBySheetFieldName(keyFieldName);

            try {
                for (var i = 0; i < count; i++)
                {
                    var keyValue    = keys[i];
                    var key         = keyValue.TryConvert <string>();
                    var targetAsset = assets?.FirstOrDefault(x => string.Equals(keyField.GetValue(x).TryConvert <string>(),
                                                                                key, StringComparison.OrdinalIgnoreCase));

                    //create asset if missing
                    if (targetAsset == null)
                    {
                        //skip asset creation step
                        if (createMissing == false)
                        {
                            continue;
                        }

                        targetAsset = filterType.CreateAsset();
                        targetAsset.SaveAsset($"{filterType.Name}_{i + 1}", folder, false);
                        Debug.Log($"Create Asset [{targetAsset}] for path {folder}", targetAsset);
                    }

                    //show assets progression
                    AssetEditorTools.ShowProgress(new ProgressData()
                    {
                        IsDone   = false,
                        Progress = i / (float)count,
                        Content  = $"{i}:{count}  {targetAsset.name}",
                        Title    = "Spreadsheet Importing"
                    });

                    var spreadsheetValueInfo = new SheetValueInfo()
                    {
                        Source          = targetAsset,
                        SheetName       = sheetId,
                        SpreadsheetData = spreadsheetData,
                        SyncScheme      = syncScheme,
                        SyncFieldName   = keyField.sheetField,
                        SyncFieldValue  = keyValue,
                    };
                    ApplyData(spreadsheetValueInfo);

                    yield return(targetAsset);
                }
            }
            finally {
                AssetEditorTools.ShowProgress(new ProgressData()
                {
                    IsDone = true,
                });
                AssetDatabase.SaveAssets();
            }
        }
        public sealed override IEnumerable <object> ImportObjects(IEnumerable <object> source, ISpreadsheetData spreadsheetData)
        {
            var result = new List <object>();

            foreach (var importer in OnPreImport(source.OfType <T>()).Where(x => x.CanImport))
            {
                result.AddRange(importer.Import(spreadsheetData));
            }

            result = OnPostImport(result).ToList();
            return(result);
        }
Example #23
0
 public static bool UpdateSheetValue(this object source, ISpreadsheetData data, string sheetId)
 {
     return(DefaultProcessor.UpdateSheetValue(source, data, sheetId));
 }
        public IEnumerable <object> Import(ISpreadsheetData spreadsheetData)
        {
            var source = Load();

            return(ImportObjects(source, spreadsheetData));
        }
        public ISpreadsheetData Export(ISpreadsheetData data)
        {
            var source = Load();

            return(ExportObjects(source, data));
        }
Example #26
0
 private IEnumerable <object> Import(ISpreadsheetData data, ISpreadsheetAssetsHandler importer)
 {
     importer.Load();
     return(importer.Import(data));
 }
Example #27
0
 public virtual ISpreadsheetData Export(ISpreadsheetData data)
 {
     return(data);
 }
Example #28
0
 public static object ApplySpreadsheetData(this object asset, ISpreadsheetData data)
 {
     return(DefaultProcessor.ApplyData(asset, data));
 }