Skip to content

Commit 7ec9247

Browse files
authored
Merge pull request #2088 from bcgov/feature/AB#32008-AddOfficeExtractionSupport-SonarFixes
AB#32008 Reduce Office extraction Sonar complexity
2 parents 632eb7b + 96097ac commit 7ec9247

1 file changed

Lines changed: 62 additions & 38 deletions

File tree

applications/Unity.GrantManager/src/Unity.GrantManager.Application/AI/TextExtractionService.cs

Lines changed: 62 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using NPOI.SS.UserModel;
33
using NPOI.XWPF.UserModel;
44
using System;
5-
using System.Collections.Generic;
65
using System.IO;
76
using System.Linq;
87
using System.Text;
@@ -220,52 +219,19 @@ private string ExtractTextFromExcelFile(string fileName, byte[] fileContent)
220219
using var workbook = WorkbookFactory.Create(stream);
221220
var builder = new StringBuilder();
222221
var sheetCount = Math.Min(workbook.NumberOfSheets, MaxExcelSheets);
223-
var limitReached = false;
224222

225223
for (var sheetIndex = 0; sheetIndex < sheetCount; sheetIndex++)
226224
{
227-
if (limitReached || builder.Length >= MaxExtractedTextLength)
225+
if (builder.Length >= MaxExtractedTextLength)
228226
{
229227
break;
230228
}
231229

232230
var sheet = workbook.GetSheetAt(sheetIndex);
233-
if (sheet == null)
234-
{
235-
continue;
236-
}
237-
238-
var processedRows = 0;
239-
foreach (IRow row in sheet)
231+
var limitReached = TryAppendExcelSheet(sheet, builder);
232+
if (limitReached)
240233
{
241-
if (processedRows >= MaxExcelRowsPerSheet || builder.Length >= MaxExtractedTextLength)
242-
{
243-
break;
244-
}
245-
246-
var rowHasValue = false;
247-
foreach (var cell in row.Cells.Take(MaxExcelCellsPerRow))
248-
{
249-
var value = GetCellText(cell);
250-
if (string.IsNullOrWhiteSpace(value))
251-
{
252-
continue;
253-
}
254-
255-
var separator = rowHasValue ? " | " : (builder.Length > 0 ? Environment.NewLine : null);
256-
limitReached = AppendWithLimit(builder, value, MaxExtractedTextLength, separator);
257-
rowHasValue = true;
258-
if (limitReached)
259-
{
260-
break;
261-
}
262-
}
263-
264-
processedRows++;
265-
if (limitReached)
266-
{
267-
break;
268-
}
234+
break;
269235
}
270236
}
271237

@@ -278,6 +244,64 @@ private string ExtractTextFromExcelFile(string fileName, byte[] fileContent)
278244
}
279245
}
280246

247+
private static bool TryAppendExcelSheet(ISheet? sheet, StringBuilder builder)
248+
{
249+
if (sheet == null)
250+
{
251+
return false;
252+
}
253+
254+
var processedRows = 0;
255+
foreach (IRow row in sheet)
256+
{
257+
if (processedRows >= MaxExcelRowsPerSheet || builder.Length >= MaxExtractedTextLength)
258+
{
259+
break;
260+
}
261+
262+
var limitReached = TryAppendExcelRow(row, builder);
263+
processedRows++;
264+
if (limitReached)
265+
{
266+
return true;
267+
}
268+
}
269+
270+
return builder.Length >= MaxExtractedTextLength;
271+
}
272+
273+
private static bool TryAppendExcelRow(IRow row, StringBuilder builder)
274+
{
275+
var rowHasValue = false;
276+
foreach (var cell in row.Cells.Take(MaxExcelCellsPerRow))
277+
{
278+
var value = GetCellText(cell);
279+
if (string.IsNullOrWhiteSpace(value))
280+
{
281+
continue;
282+
}
283+
284+
string? separator = null;
285+
if (rowHasValue)
286+
{
287+
separator = " | ";
288+
}
289+
else if (builder.Length > 0)
290+
{
291+
separator = Environment.NewLine;
292+
}
293+
294+
var limitReached = AppendWithLimit(builder, value, MaxExtractedTextLength, separator);
295+
rowHasValue = true;
296+
if (limitReached)
297+
{
298+
return true;
299+
}
300+
}
301+
302+
return false;
303+
}
304+
281305
private static bool AppendWithLimit(StringBuilder builder, string? value, int maxLength, string? separator = null)
282306
{
283307
if (string.IsNullOrWhiteSpace(value))

0 commit comments

Comments
 (0)