2025-05-31 19:26:02 +02:00
|
|
|
|
using System.Globalization;
|
2025-11-05 20:50:25 +01:00
|
|
|
|
using DiunaBI.Domain.Entities;
|
2025-12-02 21:24:37 +01:00
|
|
|
|
using DiunaBI.Infrastructure.Plugins;
|
2025-11-05 20:50:25 +01:00
|
|
|
|
using DiunaBI.Infrastructure.Services;
|
2025-05-31 19:26:02 +02:00
|
|
|
|
using Google.Apis.Sheets.v4;
|
|
|
|
|
|
using Google.Apis.Sheets.v4.Data;
|
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
|
|
2025-11-05 20:50:25 +01:00
|
|
|
|
namespace DiunaBI.Plugins.Morska.Exporters;
|
2025-05-31 19:26:02 +02:00
|
|
|
|
|
2025-12-02 21:24:37 +01:00
|
|
|
|
public class GoogleSheetExport : BaseDataExporter
|
2025-05-31 19:26:02 +02:00
|
|
|
|
{
|
2025-06-02 16:54:33 +02:00
|
|
|
|
public override string ExporterType => "GoogleSheet";
|
2025-05-31 19:26:02 +02:00
|
|
|
|
private readonly GoogleDriveHelper _googleDriveHelper;
|
|
|
|
|
|
private readonly SpreadsheetsResource.ValuesResource _googleSheetValues;
|
|
|
|
|
|
private readonly IConfiguration _configuration;
|
|
|
|
|
|
public GoogleSheetExport(
|
|
|
|
|
|
GoogleDriveHelper googleDriveHelper,
|
|
|
|
|
|
SpreadsheetsResource.ValuesResource googleSheetValues,
|
|
|
|
|
|
IConfiguration configuration)
|
|
|
|
|
|
{
|
|
|
|
|
|
_googleDriveHelper = googleDriveHelper;
|
|
|
|
|
|
_googleSheetValues = googleSheetValues;
|
|
|
|
|
|
_configuration = configuration;
|
|
|
|
|
|
}
|
2025-06-02 16:54:33 +02:00
|
|
|
|
public override void Export(Layer layer)
|
2025-05-31 19:26:02 +02:00
|
|
|
|
{
|
|
|
|
|
|
if (_googleDriveHelper.Service is null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new Exception("Google Drive API not initialized");
|
|
|
|
|
|
}
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
|
2025-06-02 16:54:33 +02:00
|
|
|
|
var data = new List<IList<object>> { new List<object> { layer.Name! } };
|
|
|
|
|
|
|
2025-05-31 19:26:02 +02:00
|
|
|
|
switch (layer.Type)
|
|
|
|
|
|
{
|
2025-06-02 16:54:33 +02:00
|
|
|
|
case LayerType.Import:
|
|
|
|
|
|
{
|
|
|
|
|
|
data.Add(new List<object> { "Code", "Value1" });
|
|
|
|
|
|
data.AddRange(layer.Records!.Select(record => new List<object> { record.Code!, record.Value1! }));
|
|
|
|
|
|
break;
|
2025-05-31 19:26:02 +02:00
|
|
|
|
}
|
2025-06-02 16:54:33 +02:00
|
|
|
|
case LayerType.Administration:
|
|
|
|
|
|
{
|
|
|
|
|
|
data.Add(new List<object> { "Code", "Desc1" });
|
|
|
|
|
|
data.AddRange(layer.Records!.Select(record => new List<object> { record.Code!, record.Desc1! }));
|
|
|
|
|
|
break;
|
2025-05-31 19:26:02 +02:00
|
|
|
|
}
|
2025-06-02 16:54:33 +02:00
|
|
|
|
case LayerType.Processed:
|
|
|
|
|
|
{
|
2025-05-31 19:26:02 +02:00
|
|
|
|
data.Add(new List<object> { "Code", "Value1", "Value2", "Value3", "Value3",
|
|
|
|
|
|
"Value5", "Value6", "Value7", "Value8", "Value9", "Value10",
|
|
|
|
|
|
"Value11", "Value12", "Value13", "Value14", "Value15", "Value16",
|
|
|
|
|
|
"Value17", "Value18", "Value19", "Value20", "Value21", "Value22",
|
|
|
|
|
|
"Value23", "Value24", "Value25", "Value26", "Value27", "Value28",
|
2025-06-02 16:54:33 +02:00
|
|
|
|
"Value29", "Value30", "Value31", "Value32"});
|
|
|
|
|
|
|
2025-05-31 19:26:02 +02:00
|
|
|
|
data.AddRange(layer.Records!.Select(record => new List<object>
|
|
|
|
|
|
{
|
|
|
|
|
|
record.Code!,
|
|
|
|
|
|
record.Value1!,
|
|
|
|
|
|
record.Value2!,
|
|
|
|
|
|
record.Value3!,
|
|
|
|
|
|
record.Value4!,
|
|
|
|
|
|
record.Value5!,
|
|
|
|
|
|
record.Value6!,
|
|
|
|
|
|
record.Value7!,
|
|
|
|
|
|
record.Value8!,
|
|
|
|
|
|
record.Value9!,
|
|
|
|
|
|
record.Value10!,
|
|
|
|
|
|
record.Value11!,
|
|
|
|
|
|
record.Value12!,
|
|
|
|
|
|
record.Value13!,
|
|
|
|
|
|
record.Value14!,
|
|
|
|
|
|
record.Value15!,
|
|
|
|
|
|
record.Value16!,
|
|
|
|
|
|
record.Value17!,
|
|
|
|
|
|
record.Value18!,
|
|
|
|
|
|
record.Value19!,
|
|
|
|
|
|
record.Value20!,
|
|
|
|
|
|
record.Value21!,
|
|
|
|
|
|
record.Value22!,
|
|
|
|
|
|
record.Value23!,
|
|
|
|
|
|
record.Value24!,
|
|
|
|
|
|
record.Value25!,
|
|
|
|
|
|
record.Value26!,
|
|
|
|
|
|
record.Value27!,
|
|
|
|
|
|
record.Value28!,
|
|
|
|
|
|
record.Value29!,
|
|
|
|
|
|
record.Value30!,
|
|
|
|
|
|
record.Value31!,
|
|
|
|
|
|
record.Value32!
|
2025-06-02 16:54:33 +02:00
|
|
|
|
}));
|
|
|
|
|
|
break;
|
2025-05-31 19:26:02 +02:00
|
|
|
|
}
|
|
|
|
|
|
default:
|
|
|
|
|
|
throw new Exception("Wrong LayerType");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var body = new Google.Apis.Drive.v3.Data.File
|
|
|
|
|
|
{
|
|
|
|
|
|
Name = $"{DateTime.Now.ToString(new CultureInfo("pl-PL"))}",
|
|
|
|
|
|
MimeType = "application/vnd.google-apps.spreadsheet",
|
|
|
|
|
|
Parents = new List<string?> { _configuration["exportDirectory"] }
|
|
|
|
|
|
};
|
|
|
|
|
|
var request = _googleDriveHelper.Service.Files.Create(body);
|
|
|
|
|
|
var file = request.Execute();
|
|
|
|
|
|
|
|
|
|
|
|
var sheetId = file.Id;
|
|
|
|
|
|
var range = $"Sheet1!A1:AG${data.Count}";
|
|
|
|
|
|
|
|
|
|
|
|
var valueRange = new ValueRange { Values = data };
|
|
|
|
|
|
|
|
|
|
|
|
var updateRequest = _googleSheetValues.Update(valueRange, sheetId, range);
|
|
|
|
|
|
updateRequest.ValueInputOption = SpreadsheetsResource.ValuesResource.UpdateRequest.ValueInputOptionEnum.RAW;
|
|
|
|
|
|
updateRequest.Execute();
|
|
|
|
|
|
|
2025-06-02 16:54:33 +02:00
|
|
|
|
}
|
2025-05-31 19:26:02 +02:00
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine(e.ToString());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|