Skip to content

Commit cca6bf6

Browse files
committed
fix: support of export to google docs
1 parent 98d9ac9 commit cca6bf6

10 files changed

Lines changed: 376 additions & 95 deletions

File tree

HwProj.APIGateway/HwProj.APIGateway.API/Controllers/StatisticsController.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ public StatisticsController(
3333
ISolutionsServiceClient solutionClient,
3434
ICoursesServiceClient coursesServiceClient,
3535
IAuthServiceClient authServiceClient,
36-
GoogleService googleService,
37-
SheetsService sheetsService)
36+
GoogleService googleService)
3837
: base(authServiceClient)
3938
{
4039
_solutionClient = solutionClient;
@@ -96,10 +95,13 @@ public async Task<IActionResult> GetFile(long courseId, string userId, [FromBody
9695
}
9796

9897
[HttpPost("getSheetTitles")]
99-
public async Task<Result<string[]>> GetSheetTitles([FromBody] string sheetUrl)
100-
=> await _googleService.GetSheetTitles(sheetUrl);
98+
public async Task<Result<string[]>> GetSheetTitles([FromBody] GetSheetTitlesModel getSheetTitlesModel)
99+
{
100+
var sheetUrl = getSheetTitlesModel.SheetUrl;
101+
return await _googleService.GetSheetTitles(sheetUrl);
102+
}
101103

102-
public Result<string> ProcessTheLink([FromBody] string sheetUrl)
104+
private Result<string> ProcessTheLink([FromBody] string sheetUrl)
103105
{
104106
if (sheetUrl.Contains("https://docs.google.com")) return GoogleService.ParseLink(sheetUrl);
105107
if (sheetUrl.Contains("https://disk.yandex.ru")) return YandexService.ParseLink(sheetUrl);
@@ -111,17 +113,16 @@ public Result<string> ProcessTheLink([FromBody] string sheetUrl)
111113
/// </summary>
112114
/// <param name="courseId">The course Id the report is based on.</param>
113115
/// <param name="userId">Id of the user requesting the report.</param>
114-
/// <param name="requestParameters">Parameters required to make requests to the Google Sheets.</param>
116+
/// <param name="requestModelParameters">Parameters required to make requests to the Google Sheets.</param>
115117
/// <returns>Operation status.</returns>
116118
[HttpPost("exportToSheet")]
117119
public async Task<Result> ExportToGoogleSheets(
118-
long courseId, string userId, [FromBody] GoogleSheetsRequest requestParameters)
120+
long courseId, string userId, [FromBody] GoogleSheetsRequestModel requestModelParameters)
119121
{
120122
var course = await _coursesClient.GetCourseById(courseId, userId);
121123
var statistics = await GetStatistics(courseId);
122124
if (course == null || statistics == null) return Result.Failed("Ошибка при получении статистики");
123-
124-
var result = await _googleService.Export(course, statistics, requestParameters);
125+
var result = await _googleService.Export(course, statistics, requestModelParameters);
125126
return result;
126127
}
127128
}

HwProj.APIGateway/HwProj.APIGateway.API/ExportServices/GoogleService.cs

Lines changed: 78 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,28 @@ public GoogleService(SheetsService sheetsService)
2424
_sheetsService = sheetsService;
2525
}
2626

27+
private static int SeparationColumnPixelWidth { get; set; } = 20;
28+
2729
public async Task<Result> Export(
2830
CourseDTO course,
2931
IOrderedEnumerable<StatisticsCourseMatesModel> statistics,
30-
[FromBody] GoogleSheetsRequest requestParameters)
32+
[FromBody] GoogleSheetsRequestModel requestModelParameters)
3133
{
32-
if (requestParameters.SheetName == null || requestParameters.SheetUrl == null)
34+
if (requestModelParameters.SheetName == null || requestModelParameters.SheetUrl == null)
3335
return Result.Failed("Ошибка при получении данных о гугл-документе");
3436

35-
var gettingSpreadsheetIdResult = ParseLink(requestParameters.SheetUrl);
37+
var gettingSpreadsheetIdResult = ParseLink(requestModelParameters.SheetUrl);
3638
if (!gettingSpreadsheetIdResult.Succeeded) return Result.Failed(gettingSpreadsheetIdResult.Errors);
3739
var spreadsheetId = gettingSpreadsheetIdResult.Value;
3840
Result result;
3941
try
4042
{
41-
var sheetId = await GetSheetId(spreadsheetId, requestParameters.SheetName);
43+
var sheetId = await GetSheetId(spreadsheetId, requestModelParameters.SheetName);
4244
if (sheetId == null) return Result.Failed("Лист с таким названием не найден");
4345

4446
var (valueRange, range, updateStyleRequestBody) = Generate(
45-
statistics.ToList(), course, requestParameters.SheetName, (int)sheetId);
46-
47+
statistics.ToList(), course, requestModelParameters.SheetName, (int)sheetId);
48+
4749
var clearRequest = _sheetsService.Spreadsheets.Values.Clear(new ClearValuesRequest(), spreadsheetId, range);
4850
await clearRequest.ExecuteAsync();
4951
var updateStyleRequest = _sheetsService.Spreadsheets.BatchUpdate(updateStyleRequestBody, spreadsheetId);
@@ -125,11 +127,11 @@ private static (ValueRange ValueRange, string Range, BatchUpdateSpreadsheetReque
125127
headersFieldEndAddress = cell.LocalAddress;
126128
}
127129

128-
if (cell.Style.Fill.BackgroundColor.Rgb == ExcelGenerator.RedColorRgb)
130+
if (cell.Style.Fill.BackgroundColor.Rgb == ExcelGenerator.BlueArgbColor)
129131
{
130132
redCellsAddresses.Add(cell.LocalAddress);
131133
}
132-
else if (cell.Style.Fill.BackgroundColor.Rgb == ExcelGenerator.GrayColorRgb)
134+
else if (cell.Style.Fill.BackgroundColor.Rgb == ExcelGenerator.GrayArgbColor)
133135
{
134136
grayCellsAddresses.Add(cell.LocalAddress);
135137
}
@@ -156,11 +158,14 @@ private static (ValueRange ValueRange, string Range, BatchUpdateSpreadsheetReque
156158
}
157159

158160
var batchUpdateRequest = new BatchUpdateSpreadsheetRequest();
161+
batchUpdateRequest.Requests = new List<Request>();
162+
AddClearStylesRequest(batchUpdateRequest, worksheet, sheetId, range);
159163
AddMergeRequests(batchUpdateRequest, worksheet, sheetId, worksheet.MergedCells);
160-
AddColouredCellsRequests(batchUpdateRequest, worksheet, sheetId, redCellsAddresses, ExcelGenerator.RedColor);
161-
AddColouredCellsRequests(batchUpdateRequest, worksheet, sheetId, grayCellsAddresses, ExcelGenerator.GrayColor);
164+
AddColouredCellsRequests(batchUpdateRequest, worksheet, sheetId, redCellsAddresses, ExcelGenerator.BlueFloatArgbColor);
165+
AddColouredCellsRequests(batchUpdateRequest, worksheet, sheetId, grayCellsAddresses, ExcelGenerator.GrayFloatArgbColor);
166+
AddUpdateCellsWidthRequest(batchUpdateRequest, worksheet, sheetId, grayCellsAddresses, SeparationColumnPixelWidth);
162167
AddCellsFormattingRequest(batchUpdateRequest, worksheet, sheetId, range);
163-
AddHeadersFormattingRequest(batchUpdateRequest, worksheet, sheetId, headersFieldEndAddress);
168+
AddHeadersFormattingRequest(batchUpdateRequest, worksheet, sheetId, $"{sheetName}!A1:{headersFieldEndAddress}");
164169
AddBordersFormattingRequest(batchUpdateRequest, worksheet, sheetId, cellsWithBorderAddresses, ExcelGenerator.EquivalentBorderStyle);
165170
return (valueRange, rangeWithSheetTitle, batchUpdateRequest);
166171
}
@@ -177,6 +182,25 @@ private static GridRange FillGridRange(ExcelWorksheet worksheet, string rangeAdd
177182
return gridRange;
178183
}
179184

185+
private static void AddClearStylesRequest(
186+
BatchUpdateSpreadsheetRequest batchUpdateRequest,
187+
ExcelWorksheet worksheet,
188+
int sheetId,
189+
string range)
190+
{
191+
var clearStylesRequest = new RepeatCellRequest();
192+
clearStylesRequest.Range = FillGridRange(worksheet, range, sheetId);
193+
var cell = new CellData();
194+
cell.UserEnteredFormat = new CellFormat();
195+
clearStylesRequest.Cell = cell;
196+
clearStylesRequest.Fields = "userEnteredFormat";
197+
198+
var request = new Request();
199+
request.RepeatCell = clearStylesRequest;
200+
batchUpdateRequest.Requests.Add(request);
201+
}
202+
203+
180204
private static void AddBordersFormattingRequest(
181205
BatchUpdateSpreadsheetRequest batchUpdateRequest,
182206
ExcelWorksheet worksheet,
@@ -206,13 +230,13 @@ private static void AddBordersFormattingRequest(
206230
updateBorderRequest.Right = border;
207231
break;
208232
}
209-
233+
210234
var request = new Request();
211235
request.UpdateBorders = updateBorderRequest;
212236
batchUpdateRequest.Requests.Add(request);
213237
}
214238
}
215-
239+
216240
private static void AddHeadersFormattingRequest(
217241
BatchUpdateSpreadsheetRequest batchUpdateRequest,
218242
ExcelWorksheet worksheet,
@@ -222,9 +246,11 @@ private static void AddHeadersFormattingRequest(
222246
var styleBoldCellsRequest = new RepeatCellRequest();
223247
styleBoldCellsRequest.Range = FillGridRange(worksheet, range, sheetId);
224248
var cell = new CellData();
249+
cell.UserEnteredFormat = new CellFormat();
250+
cell.UserEnteredFormat.TextFormat = new TextFormat();
225251
cell.UserEnteredFormat.TextFormat.Bold = true;
226252
styleBoldCellsRequest.Cell = cell;
227-
styleBoldCellsRequest.Fields = "userEnteredFormat(textFormat)";
253+
styleBoldCellsRequest.Fields = "userEnteredFormat(textFormat.bold)";
228254

229255
var request = new Request();
230256
request.RepeatCell = styleBoldCellsRequest;
@@ -240,12 +266,14 @@ private static void AddCellsFormattingRequest(
240266
var cellsFormatRequest = new RepeatCellRequest();
241267
cellsFormatRequest.Range = FillGridRange(worksheet, range, sheetId);
242268
var cell = new CellData();
269+
cell.UserEnteredFormat = new CellFormat();
243270
cell.UserEnteredFormat.HorizontalAlignment = "CENTER";
244-
cell.UserEnteredFormat.VerticalAlignment = "CENTER";
271+
cell.UserEnteredFormat.VerticalAlignment = "MIDDLE";
272+
cell.UserEnteredFormat.TextFormat = new TextFormat();
245273
cell.UserEnteredFormat.TextFormat.FontSize = ExcelGenerator.FontSize;
246274
cell.UserEnteredFormat.TextFormat.FontFamily = ExcelGenerator.FontFamily;
247275
cellsFormatRequest.Cell = cell;
248-
cellsFormatRequest.Fields = "userEnteredFormat(horizontalAlignment,verticalAlignment,textFormat)";
276+
cellsFormatRequest.Fields = "userEnteredFormat(horizontalAlignment,verticalAlignment,textFormat.fontSize,textFormat.fontFamily)";
249277

250278
var request = new Request();
251279
request.RepeatCell = cellsFormatRequest;
@@ -263,7 +291,7 @@ private static void AddMergeRequests(
263291
var mergedCellsAddress = mergeCellsCollection[i];
264292
var gridRange = FillGridRange(worksheet, mergedCellsAddress, sheetId);
265293
var mergeCellsRequest = new MergeCellsRequest();
266-
mergeCellsRequest.MergeType = "MERGE_COLUMNS";
294+
mergeCellsRequest.MergeType = "MERGE_ALL";
267295
mergeCellsRequest.Range = gridRange;
268296

269297
var request = new Request();
@@ -272,19 +300,49 @@ private static void AddMergeRequests(
272300
}
273301
}
274302

303+
private static void AddUpdateCellsWidthRequest(
304+
BatchUpdateSpreadsheetRequest batchUpdateRequest,
305+
ExcelWorksheet worksheet,
306+
int sheetId,
307+
List<string> cellsAddresses,
308+
int cellsPixelWidth)
309+
{
310+
for (var i = 0; i < cellsAddresses.Count; ++i)
311+
{
312+
var cellAddress = cellsAddresses[i];
313+
var rangeInfo = worksheet.Cells[cellAddress];
314+
var updateWidthRequest = new UpdateDimensionPropertiesRequest();
315+
updateWidthRequest.Range = new DimensionRange()
316+
{
317+
SheetId = sheetId,
318+
Dimension = "COLUMNS",
319+
StartIndex = rangeInfo.Start.Column - 1,
320+
EndIndex = rangeInfo.End.Column,
321+
};
322+
updateWidthRequest.Fields = "*";
323+
updateWidthRequest.Properties = new DimensionProperties();
324+
updateWidthRequest.Properties.PixelSize = cellsPixelWidth;
325+
326+
var request = new Request();
327+
request.UpdateDimensionProperties = updateWidthRequest;
328+
batchUpdateRequest.Requests.Add(request);
329+
}
330+
}
331+
275332
private static void AddColouredCellsRequests(
276333
BatchUpdateSpreadsheetRequest batchUpdateRequest,
277334
ExcelWorksheet worksheet,
278335
int sheetId,
279336
List<string> colouredCellsAddresses,
280-
(int Alpha, int Red, int Green, int Blue) color)
337+
(float Alpha, float Red, float Green, float Blue) color)
281338
{
282339
for (var i = 0; i < colouredCellsAddresses.Count; ++i)
283340
{
284341
var cellAddress = colouredCellsAddresses[i];
285342
var colorInRedRequest = new RepeatCellRequest();
286343
colorInRedRequest.Range = FillGridRange(worksheet, cellAddress, sheetId);
287344
var cell = new CellData();
345+
cell.UserEnteredFormat = new CellFormat();
288346
cell.UserEnteredFormat.BackgroundColor = new Color()
289347
{
290348
Alpha = color.Alpha,
@@ -293,13 +351,14 @@ private static void AddColouredCellsRequests(
293351
Blue = color.Blue,
294352
};
295353
colorInRedRequest.Fields = "userEnteredFormat(backgroundColor)";
354+
colorInRedRequest.Cell = cell;
296355

297356
var request = new Request();
298357
request.RepeatCell = colorInRedRequest;
299358
batchUpdateRequest.Requests.Add(request);
300359
}
301360
}
302-
361+
303362
private async Task<int?> GetSheetId(string spreadsheetId, string sheetName)
304363
{
305364
var spreadsheetGetRequest = _sheetsService.Spreadsheets.Get(spreadsheetId);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace HwProj.APIGateway.API.Models
2+
{
3+
public class GetSheetTitlesModel
4+
{
5+
public string SheetUrl;
6+
}
7+
}

HwProj.APIGateway/HwProj.APIGateway.API/Models/GoogleSheetsRequest.cs renamed to HwProj.APIGateway/HwProj.APIGateway.API/Models/GoogleSheetsRequestModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace HwProj.APIGateway.API.Models
22
{
3-
public class GoogleSheetsRequest
3+
public class GoogleSheetsRequestModel
44
{
55
public string? SheetUrl { get; set; }
66

HwProj.APIGateway/HwProj.APIGateway.API/Startup.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Google.Apis.Auth.OAuth2;
33
using Google.Apis.Services;
44
using Google.Apis.Sheets.v4;
5+
using HwProj.APIGateway.API.ExportServices;
56
using HwProj.AuthService.Client;
67
using HwProj.CoursesService.Client;
78
using HwProj.NotificationsService.Client;
@@ -48,7 +49,8 @@ public void ConfigureServices(IServiceCollection services)
4849

4950
services.AddHttpClient();
5051
services.AddHttpContextAccessor();
51-
services.AddScoped(_ => ConfigureGoogleSheets());
52+
services.AddSingleton(_ => ConfigureGoogleSheets());
53+
services.AddSingleton<GoogleService>();
5254

5355
services.AddAuthServiceClient();
5456
services.AddCoursesServiceClient();

HwProj.APIGateway/HwProj.APIGateway.API/TableGenerators/ExcelGenerator.cs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,27 @@ public class ExcelGenerator
2626
/// <summary>
2727
/// Shade of red used in the reports.
2828
/// </summary>
29-
public static (int Alpha, int Red, int Green, int Blue) RedColor { get; set; } = (255, 255, 0, 0);
30-
public static string RedColorRgb { get; set; } = "FFFF0000";
29+
private static (int Alpha, int Red, int Green, int Blue) BlueIntArgbColor { get; set; } = (0, 0, 255, 255);
30+
public static (float Alpha, float Red, float Green, float Blue) BlueFloatArgbColor { get; set; } = (0, 0, 1, 1);
31+
public static string BlueArgbColor { get; set; } = "0000FFFF";
3132

3233

3334
/// <summary>
3435
/// Shade of gray used in the reports.
3536
/// </summary>
36-
public static (int Alpha, int Red, int Green, int Blue) GrayColor { get; set; } = (255, 80, 80, 80);
37-
public static string GrayColorRgb { get; set; } = "FF505050";
37+
private static (int Alpha, int Red, int Green, int Blue) GrayIntArgbColor { get; set; } = (255, 80, 80, 80);
38+
39+
public static (float Alpha, float Red, float Green, float Blue) GrayFloatArgbColor { get; set; } =
40+
(1, (float)0.3137, (float)0.3137, (float)0.3137);
41+
42+
public static string GrayArgbColor { get; set; } = "FF505050";
43+
44+
private static ExcelBorderStyle BorderStyle { get; set; } = ExcelBorderStyle.Thin;
3845

39-
public static ExcelBorderStyle BorderStyle { get; set; } = ExcelBorderStyle.Thin;
4046
public static string EquivalentBorderStyle { get; set; } = "SOLID";
4147

48+
private static int SeparationColumnWidth { get; set; } = 2;
49+
4250
/// <summary>
4351
/// Generates course statistics file based on the model from HwProj.APIGateway.Tests.Test.xlsx file.
4452
/// </summary>
@@ -62,7 +70,7 @@ public static ExcelPackage Generate(
6270
worksheet.Cells[position.Row, position.Column, position.Row + 2, position.Column].Merge = true;
6371
++position.Column;
6472

65-
AddHomeworksHeaders(worksheet, course, position, rowsNumber);
73+
AddHomeworksHeaders(worksheet, course, position, rowsNumber, SeparationColumnWidth);
6674
var columnsNumber = position.Column - 2;
6775
position.ToNextRow(2);
6876

@@ -79,7 +87,7 @@ public static ExcelPackage Generate(
7987
AddTasksMaxRatingInfo(worksheet, course, rowsNumber, maxFieldPosition);
8088

8189
AddCourseMatesInfo(worksheet, courseMatesModels, position);
82-
90+
8391
var headersRange = worksheet.Cells[1, 1, 3, columnsNumber];
8492
headersRange.Style.Font.Bold = true;
8593

@@ -90,17 +98,17 @@ public static ExcelPackage Generate(
9098
return excelPackage;
9199
}
92100

93-
private static void AddBorderedSeparationColumn(ExcelWorksheet worksheet, Position position, int heightInCells)
101+
private static void AddBorderedSeparationColumn(ExcelWorksheet worksheet, Position position, int heightInCells, int columnWidth)
94102
{
95103
var range = worksheet.Cells[1, position.Column, heightInCells, position.Column];
96104
range.Style.Fill.PatternType = ExcelFillStyle.Solid;
97-
range.Style.Fill.BackgroundColor.SetColor(GrayColor.Alpha, GrayColor.Red, GrayColor.Green, GrayColor.Blue);
98-
worksheet.Column(position.Column).Width = 2;
105+
range.Style.Fill.BackgroundColor.SetColor(GrayIntArgbColor.Alpha, GrayIntArgbColor.Red, GrayIntArgbColor.Green, GrayIntArgbColor.Blue);
106+
worksheet.Column(position.Column).Width = columnWidth;
99107
++position.Column;
100108
}
101109

102110
private static void AddHomeworksHeaders(ExcelWorksheet worksheet, CourseDTO course, Position position,
103-
int heightInCells)
111+
int heightInCells, int separationColumnWidth)
104112
{
105113
var homeworkNumber = 1;
106114
for (var i = 0; i < course.Homeworks.Length; ++i)
@@ -111,7 +119,7 @@ private static void AddHomeworksHeaders(ExcelWorksheet worksheet, CourseDTO cour
111119
worksheet.Cells[position.Row, position.Column, position.Row, position.Column + numberCellsToMerge - 1]
112120
.Merge = true;
113121
position.Column += numberCellsToMerge;
114-
AddBorderedSeparationColumn(worksheet, position, heightInCells);
122+
AddBorderedSeparationColumn(worksheet, position, heightInCells, separationColumnWidth);
115123
++homeworkNumber;
116124
}
117125
}
@@ -211,7 +219,7 @@ private static void AddCourseMatesInfo(
211219
worksheet.Cells[position.Row, position.Column + 2].Style.Fill.PatternType =
212220
ExcelFillStyle.Solid;
213221
worksheet.Cells[position.Row, position.Column + 2].Style.Fill.BackgroundColor.SetColor(
214-
RedColor.Alpha, RedColor.Red, RedColor.Green, RedColor.Blue);
222+
BlueIntArgbColor.Alpha, BlueIntArgbColor.Red, BlueIntArgbColor.Green, BlueIntArgbColor.Blue);
215223
}
216224

217225
position.Column += 3;
-5.4 KB
Binary file not shown.

0 commit comments

Comments
 (0)