Black Rock Reporting Azure Function
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

ReportGenerator.cs 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. using GemBox.Spreadsheet;
  2. using BlackRockReportFunction.Models;
  3. using BlackRockReportFunction.Helpers;
  4. namespace BlackRockReportFunction.Bussines
  5. {
  6. internal class ReportGenerator
  7. {
  8. public static async Task<ExcelFile> GenerateReportContent()
  9. {
  10. string licenseKey = Environment.GetEnvironmentVariable("GemBoxLicenseKey");
  11. SpreadsheetInfo.SetLicense(licenseKey);
  12. var testObject = new ClockifyReport
  13. {
  14. reportName = "BlackRockReport_20220615",
  15. reportDescription = "Total (13/06/2022 - 15/06/2022)",
  16. reportPeople = new List<Person>
  17. {
  18. new Person
  19. {
  20. fullName = "Nikola Jovanovic",
  21. records = new List<ClockifyRecord>
  22. {
  23. new ClockifyRecord
  24. {
  25. recordDescription = "Description1",
  26. recordTime = new TimeOnly(3,15,44),
  27. amount = 200
  28. },
  29. new ClockifyRecord
  30. {
  31. recordDescription = "Description1",
  32. recordTime = new TimeOnly(3,15,44),
  33. amount = 150
  34. }
  35. }
  36. },
  37. new Person
  38. {
  39. fullName = "Boris Stevanovic",
  40. records = new List<ClockifyRecord>
  41. {
  42. new ClockifyRecord
  43. {
  44. recordDescription = "Description1",
  45. recordTime = new TimeOnly(3,15,44),
  46. amount = 300
  47. },
  48. new ClockifyRecord
  49. {
  50. recordDescription = "Description1",
  51. recordTime = new TimeOnly(3,15,44),
  52. amount = 100
  53. },
  54. new ClockifyRecord
  55. {
  56. recordDescription = "Description1",
  57. recordTime = new TimeOnly(3,15,44),
  58. amount = 120
  59. }
  60. }
  61. },
  62. new Person
  63. {
  64. fullName = "Dunja Stevanovic",
  65. records = new List<ClockifyRecord>
  66. {
  67. new ClockifyRecord
  68. {
  69. recordDescription = "Description1",
  70. recordTime = new TimeOnly(3,15,44),
  71. amount = 50
  72. },
  73. new ClockifyRecord
  74. {
  75. recordDescription = "Description1",
  76. recordTime = new TimeOnly(3,15,44),
  77. amount = 500
  78. }
  79. }
  80. }
  81. }
  82. };
  83. var excelFile = new ExcelFile();
  84. var ws = excelFile.Worksheets.Add("Content");
  85. var sectionStyle = new CellStyle
  86. {
  87. Font =
  88. {
  89. Weight = ExcelFont.BoldWeight,
  90. Size = 250,
  91. },
  92. VerticalAlignment = VerticalAlignmentStyle.Center,
  93. HorizontalAlignment = HorizontalAlignmentStyle.Center,
  94. };
  95. var mainDetailsStyle = new CellStyle
  96. {
  97. Font =
  98. {
  99. Weight = ExcelFont.BoldWeight,
  100. Size = 200,
  101. }
  102. };
  103. await AddReportItems(testObject, ws, sectionStyle, mainDetailsStyle);
  104. // Autofit
  105. excelFile = await AutoFitReport(excelFile);
  106. return excelFile;
  107. }
  108. public static async Task AddReportItems(ClockifyReport reportObject, ExcelWorksheet ws, CellStyle sectionStyle, CellStyle mainDetailsStyle)
  109. {
  110. int row = 0;
  111. string[] sectionNames = { "User", "Description", "Time (h)", "Time (decimal)", "Amount (USD)" };
  112. decimal totalAmountSum = 0;
  113. for (int i = 0; i < sectionNames.Length; i++)
  114. {
  115. ws.Cells[row, i].Style = sectionStyle;
  116. ws.Cells[row, i].Value = sectionNames[i];
  117. ws.Cells[row, i].Style.Borders[IndividualBorder.Right].LineStyle = LineStyle.Medium;
  118. ws.Cells[row, i].Style.Borders[IndividualBorder.Bottom].LineStyle = LineStyle.Thick;
  119. }
  120. row++;
  121. foreach (var reportPerson in reportObject.reportPeople)
  122. {
  123. ws.Cells[row, 0].Style = mainDetailsStyle;
  124. ws.Cells[row, 0].Value = reportPerson.fullName;
  125. var sumOfRecordHours = Formaters.getSumOfRecordTimes(reportPerson.records.Select(record => record.recordTime).ToList());
  126. ws.Cells[row, 2].Style = mainDetailsStyle;
  127. ws.Cells[row, 2].Style.HorizontalAlignment = HorizontalAlignmentStyle.Right;
  128. ws.Cells[row, 2].Value = sumOfRecordHours;
  129. ws.Cells[row, 3].Style = mainDetailsStyle;
  130. ws.Cells[row, 3].Style.HorizontalAlignment = HorizontalAlignmentStyle.Right;
  131. ws.Cells[row, 3].Value = Formaters.getDecimalHours(sumOfRecordHours);
  132. ws.Cells[row, 4].Style = mainDetailsStyle;
  133. ws.Cells[row, 4].Style.HorizontalAlignment = HorizontalAlignmentStyle.Right;
  134. ws.Cells[row, 4].Value = string.Format("{0} USD", reportPerson.records.Sum(record => record.amount).ToString("0.00"));
  135. totalAmountSum += reportPerson.records.Sum(record => record.amount);
  136. row++;
  137. foreach (var personRecord in reportPerson.records)
  138. {
  139. ws.Cells[row, 1].Value = personRecord.recordDescription;
  140. ws.Cells[row, 2].Value = Formaters.getRecordTime(personRecord.recordTime);
  141. ws.Cells[row, 2].Style.HorizontalAlignment = HorizontalAlignmentStyle.Right;
  142. ws.Cells[row, 3].Value = Formaters.getDecimalHours(personRecord.recordTime);
  143. ws.Cells[row, 3].Style.HorizontalAlignment = HorizontalAlignmentStyle.Right;
  144. ws.Cells[row, 4].Value = string.Format("{0} USD", personRecord.amount.ToString("0.00"));
  145. ws.Cells[row, 4].Style.HorizontalAlignment = HorizontalAlignmentStyle.Right;
  146. row++;
  147. }
  148. }
  149. ws.Cells.GetSubrangeAbsolute(row, 0, row, 1).Merged = true;
  150. ws.Cells.GetSubrangeAbsolute(row, 0, row, 1).Style = mainDetailsStyle;
  151. ws.Cells.GetSubrangeAbsolute(row, 0, row, 1).Value = reportObject.reportDescription;
  152. var totalSum = Formaters.getTotalSum(
  153. reportObject.reportPeople.Select(
  154. person => Formaters.getSumOfRecordTimes(
  155. person.records.Select(record => record.recordTime).ToList()
  156. )
  157. ).ToList()
  158. );
  159. ws.Cells[row, 2].Style = mainDetailsStyle;
  160. ws.Cells[row, 2].Style.HorizontalAlignment = HorizontalAlignmentStyle.Right;
  161. ws.Cells[row, 2].Value = totalSum;
  162. ws.Cells[row, 3].Style = mainDetailsStyle;
  163. ws.Cells[row, 3].Style.HorizontalAlignment = HorizontalAlignmentStyle.Right;
  164. ws.Cells[row, 3].Value = Formaters.getDecimalHours(totalSum);
  165. ws.Cells[row, 4].Style = mainDetailsStyle;
  166. ws.Cells[row, 4].Style.HorizontalAlignment = HorizontalAlignmentStyle.Right;
  167. ws.Cells[row, 4].Value = string.Format("{0} USD", totalAmountSum.ToString("0.00"));
  168. }
  169. public static async Task<ExcelFile> AutoFitReport(ExcelFile excelFile)
  170. {
  171. var localFile = excelFile;
  172. foreach (var sheet in localFile.Worksheets)
  173. {
  174. var columnCount = sheet.CalculateMaxUsedColumns();
  175. for (int i = 0; i < columnCount; i++)
  176. {
  177. sheet.Columns[i].AutoFit(1, sheet.Rows[0], sheet.Rows[sheet.Rows.Count - 1]);
  178. }
  179. }
  180. return localFile;
  181. }
  182. }
  183. }