Files
bim-dogma-poc/SchemaEditor/Services/ExcelService.cs

74 lines
2.1 KiB
C#

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ExcelKv.Core;
namespace SchemaEditor.Services;
public record ParsedItem(string Key, string Value, int Row, int Col);
public class InMemoryStorage : IStorageWrapper
{
public ConcurrentDictionary<string, ParsedItem> Data { get; private set; } = new();
public Task SetAsync(string key, string value)
{
Data[key] = new ParsedItem(key, value, -1, -1);
return Task.CompletedTask;
}
public Task SetAsync(string key, string value, int row, int col)
{
Data[key] = new ParsedItem(key, value, row, col);
return Task.CompletedTask;
}
public Task IncrementAsync(string key, double value)
{
Data[key] = new ParsedItem(key, $"[Metric] {value} (Accumulated)", -1, -1);
return Task.CompletedTask;
}
public List<ParsedItem> GetEntries()
{
return Data.Values.OrderBy(x => x.Key).ToList();
}
}
public class ExcelService
{
// Changed to List<ParsedItem> for metadata access
public List<ParsedItem> LoadedData { get; private set; } = new();
public async Task<List<string>> GetSheetsAsync(string filePath)
{
return await ExcelLoader.GetSheetNamesAsync(filePath);
}
public async Task<List<string[]>> GetPreviewAsync(string filePath, string sheetName)
{
// Increased limit to 1000 to ensure highlighting works for larger files
return await ExcelLoader.GetPreviewRowsAsync(filePath, sheetName, 1000);
}
public async Task LoadFileAsync(string filePath, string sheetName, RegionConfig config)
{
var storage = new InMemoryStorage();
var registry = new SchemaRegistry();
await ExcelLoader.ProcessFileAsync(filePath, sheetName, config, storage, registry);
LoadedData = storage.GetEntries();
}
public async Task SaveToStorageAsync(IStorageWrapper targetStorage)
{
foreach (var entry in LoadedData)
{
await targetStorage.SetAsync(entry.Key, entry.Value);
}
}
}