-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored common google request helpers.
- Loading branch information
Showing
8 changed files
with
249 additions
and
224 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace RLE.Core.Extensions; | ||
|
||
public static class EnumerableExtensions | ||
{ | ||
public static void AddRange<T>(this IList<T> collection, IEnumerable<T> items) | ||
{ | ||
foreach (var item in items) | ||
{ | ||
collection.Add(item); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
using Google.Apis.Sheets.v4.Data; | ||
using RLE.Core.Constants; | ||
using RLE.Core.Enums; | ||
using RLE.Core.Models.Google; | ||
|
||
namespace RLE.Core.Helpers; | ||
|
||
public static class GoogleRequestHelpers | ||
{ | ||
|
||
public static Request GenerateAppendCells(SheetModel sheet) | ||
{ | ||
// Create Sheet Headers | ||
var appendCellsRequest = new AppendCellsRequest | ||
{ | ||
Fields = GoogleConfig.FieldsUpdate, | ||
Rows = SheetHelpers.HeadersToRowData(sheet!), | ||
SheetId = sheet!.Id | ||
}; | ||
|
||
return new Request { AppendCells = appendCellsRequest }; | ||
} | ||
|
||
public static List<Request> GenerateAppendDimension(SheetModel sheet) | ||
{ | ||
List<Request> requests = []; | ||
// Append more columns if the default amount isn't enough | ||
var defaultColumns = GoogleConfig.DefaultColumnCount; | ||
if (sheet!.Headers.Count > defaultColumns) | ||
{ | ||
var appendDimensionRequest = new AppendDimensionRequest | ||
{ | ||
Dimension = GoogleConfig.AppendDimensionType, | ||
Length = sheet.Headers.Count - defaultColumns, | ||
SheetId = sheet.Id | ||
}; | ||
requests.Add(new Request { AppendDimension = appendDimensionRequest }); | ||
} | ||
|
||
return requests; | ||
} | ||
|
||
public static Request GenerateBandingRequest(SheetModel sheet) | ||
{ | ||
// Add alternating colors | ||
var addBandingRequest = new AddBandingRequest | ||
{ | ||
BandedRange = new BandedRange | ||
{ | ||
BandedRangeId = sheet!.Id, | ||
Range = new GridRange { SheetId = sheet.Id }, | ||
RowProperties = new BandingProperties { HeaderColor = SheetHelpers.GetColor(sheet!.TabColor), FirstBandColor = SheetHelpers.GetColor(ColorEnum.WHITE), SecondBandColor = SheetHelpers.GetColor(sheet!.CellColor) } | ||
} | ||
}; | ||
return new Request { AddBanding = addBandingRequest }; | ||
} | ||
public static Request GenerateProtectedRangeForHeaderOrSheet(SheetModel sheet) | ||
{ | ||
// Protect sheet or header | ||
var addProtectedRangeRequest = new AddProtectedRangeRequest(); | ||
if (sheet!.ProtectSheet) | ||
{ | ||
addProtectedRangeRequest = new AddProtectedRangeRequest | ||
{ | ||
ProtectedRange = new ProtectedRange { Description = ProtectionWarnings.SheetWarning, Range = new GridRange { SheetId = sheet.Id }, WarningOnly = true } | ||
}; | ||
} | ||
else | ||
{ | ||
// Protect full header if sheet isn't protected. | ||
var range = new GridRange | ||
{ | ||
SheetId = sheet.Id, | ||
StartColumnIndex = 0, | ||
EndColumnIndex = sheet!.Headers.Count, | ||
StartRowIndex = 0, | ||
EndRowIndex = 1 | ||
}; | ||
|
||
addProtectedRangeRequest.ProtectedRange = new ProtectedRange { Description = ProtectionWarnings.HeaderWarning, Range = range, WarningOnly = true }; | ||
} | ||
|
||
return new Request { AddProtectedRange = addProtectedRangeRequest }; | ||
} | ||
|
||
public static Request GenerateSheetPropertes(SheetModel sheet) | ||
{ | ||
var sheetRequest = new AddSheetRequest | ||
{ | ||
Properties = new SheetProperties | ||
{ | ||
// Create Sheet With Properties | ||
SheetId = sheet.Id, | ||
Title = sheet!.Name, | ||
TabColor = SheetHelpers.GetColor(sheet.TabColor), | ||
GridProperties = new GridProperties { FrozenColumnCount = sheet.FreezeColumnCount, FrozenRowCount = sheet.FreezeRowCount } | ||
} | ||
}; | ||
|
||
return new Request { AddSheet = sheetRequest }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
using Google.Apis.Sheets.v4.Data; | ||
using RLE.Gig.Enums; | ||
using RLE.Gig.Mappers; | ||
using RLE.Core.Constants; | ||
using RLE.Core.Enums; | ||
using RLE.Core.Models.Google; | ||
using RLE.Core.Helpers; | ||
using RLE.Core.Extensions; | ||
|
||
namespace RLE.Gig.Helpers; | ||
|
||
public static class GenerateSheetsHelpers | ||
{ | ||
private static BatchUpdateSpreadsheetRequest? _batchUpdateSpreadsheetRequest; | ||
private static List<RepeatCellRequest>? _repeatCellRequests; | ||
|
||
public static BatchUpdateSpreadsheetRequest Generate(List<SheetEnum> sheets) | ||
{ | ||
_batchUpdateSpreadsheetRequest = new BatchUpdateSpreadsheetRequest(); | ||
_batchUpdateSpreadsheetRequest.Requests = []; | ||
_repeatCellRequests = []; | ||
|
||
sheets.ForEach(sheet => | ||
{ | ||
var sheetModel = GetSheetModel(sheet); | ||
var random = new Random(); | ||
sheetModel.Id = random.Next(); | ||
|
||
_batchUpdateSpreadsheetRequest!.Requests.Add(GoogleRequestHelpers.GenerateSheetPropertes(sheetModel)); | ||
_batchUpdateSpreadsheetRequest!.Requests.AddRange(GoogleRequestHelpers.GenerateAppendDimension(sheetModel)); | ||
_batchUpdateSpreadsheetRequest!.Requests.Add(GoogleRequestHelpers.GenerateAppendCells(sheetModel)); | ||
GenerateHeadersFormatAndProtection(sheetModel); | ||
_batchUpdateSpreadsheetRequest!.Requests.Add(GoogleRequestHelpers.GenerateBandingRequest(sheetModel)); | ||
_batchUpdateSpreadsheetRequest!.Requests.Add(GoogleRequestHelpers.GenerateProtectedRangeForHeaderOrSheet(sheetModel)); | ||
}); | ||
|
||
_repeatCellRequests.ForEach(request => | ||
{ | ||
_batchUpdateSpreadsheetRequest.Requests.Add(new Request { RepeatCell = request }); | ||
}); | ||
|
||
return _batchUpdateSpreadsheetRequest; | ||
} | ||
|
||
private static SheetModel GetSheetModel(SheetEnum sheetEnum) | ||
{ | ||
return sheetEnum switch | ||
{ | ||
SheetEnum.ADDRESSES => AddressMapper.GetSheet(), | ||
SheetEnum.DAILY => DailyMapper.GetSheet(), | ||
SheetEnum.MONTHLY => MonthlyMapper.GetSheet(), | ||
SheetEnum.NAMES => NameMapper.GetSheet(), | ||
SheetEnum.PLACES => PlaceMapper.GetSheet(), | ||
SheetEnum.REGIONS => RegionMapper.GetSheet(), | ||
SheetEnum.SERVICES => ServiceMapper.GetSheet(), | ||
SheetEnum.SHIFTS => ShiftMapper.GetSheet(), | ||
SheetEnum.TRIPS => TripMapper.GetSheet(), | ||
SheetEnum.TYPES => TypeMapper.GetSheet(), | ||
SheetEnum.WEEKDAYS => WeekdayMapper.GetSheet(), | ||
SheetEnum.WEEKLY => WeeklyMapper.GetSheet(), | ||
SheetEnum.YEARLY => YearlyMapper.GetSheet(), | ||
_ => throw new NotImplementedException(), | ||
}; | ||
} | ||
|
||
private static void GenerateHeadersFormatAndProtection(SheetModel sheet) | ||
{ | ||
// Format/Protect Column Cells | ||
sheet!.Headers.ForEach(header => | ||
{ | ||
var range = new GridRange | ||
{ | ||
SheetId = sheet.Id, | ||
StartColumnIndex = header.Index, | ||
EndColumnIndex = header.Index + 1, | ||
StartRowIndex = 1, | ||
}; | ||
|
||
// If whole sheet isn't protected then protect certain columns | ||
if (!string.IsNullOrEmpty(header.Formula) && !sheet.ProtectSheet) | ||
{ | ||
var addProtectedRangeRequest = new AddProtectedRangeRequest | ||
{ | ||
ProtectedRange = new ProtectedRange { Description = ProtectionWarnings.ColumnWarning, Range = range, WarningOnly = true } | ||
}; | ||
_batchUpdateSpreadsheetRequest!.Requests.Add(new Request { AddProtectedRange = addProtectedRangeRequest }); | ||
} | ||
|
||
// If there's no format or validation then go to next header | ||
if (header.Format == null && header.Validation == null) | ||
{ | ||
return; | ||
} | ||
|
||
// Set start/end for formatting | ||
range.StartRowIndex = 1; | ||
range.EndRowIndex = null; | ||
|
||
var repeatCellRequest = new RepeatCellRequest | ||
{ | ||
Fields = GoogleConfig.FieldsUpdate, | ||
Range = range, | ||
Cell = new CellData() | ||
}; | ||
|
||
if (header.Format != null) | ||
{ | ||
repeatCellRequest.Cell.UserEnteredFormat = SheetHelpers.GetCellFormat((FormatEnum)header.Format); | ||
} | ||
|
||
if (header.Validation != null) | ||
{ | ||
repeatCellRequest.Cell.DataValidation = GigSheetHelpers.GetDataValidation((ValidationEnum)header.Validation); | ||
} | ||
|
||
_repeatCellRequests!.Add(repeatCellRequest); | ||
}); | ||
} | ||
} |
Oops, something went wrong.