Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow using BatchRequest for Pulls #90

Merged
merged 2 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 21 additions & 14 deletions Excel_Adapter/CRUD/Read/Read.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,10 @@
using BH.oM.Adapter;
using BH.oM.Adapters.Excel;
using BH.oM.Base;
using BH.oM.Data.Collections;
using BH.oM.Data.Requests;
using ClosedXML.Excel;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;

Expand Down Expand Up @@ -68,6 +66,16 @@
return new List<IBHoMObject>();
}

return ReadExcel(workbook, request, actionConfig);
}


/***************************************************/
/**** Private Methods ****/
/***************************************************/

private List<IBHoMObject> ReadExcel(XLWorkbook workbook, IRequest request, ActionConfig actionConfig)
{
if (request is ObjectRequest)
{
List<TableRow> result = ReadExcel(workbook, ((ObjectRequest)request).Worksheet, ((ObjectRequest)request).Range, true).OfType<TableRow>().ToList();
Expand All @@ -81,25 +89,24 @@
return ReadExcel(workbook, ((CellContentsRequest)request).Worksheet, ((CellContentsRequest)request).Range, false);
else if (request is WorksheetsRequest)
return ReadExcel(workbook, ((WorksheetsRequest)request));
else if (request is BatchRequest batchRequest)
return batchRequest.Requests.Select(x => new ResultItem { Objects = ReadExcel(workbook, x, actionConfig), OriginalRequest = x }).ToList<IBHoMObject>();
else
{
BH.Engine.Base.Compute.RecordError($"Requests of type {request?.GetType()} are not supported by the Excel adapter.");
return new List<IBHoMObject>();
}
}


/***************************************************/
/**** Private Methods ****/
/***************************************************/

private List<IBHoMObject> ReadExcel(XLWorkbook workbook, WorksheetsRequest request)
{
var worksheets = Worksheets(workbook, null);
IEnumerable<IXLWorksheet> worksheets = Worksheets(workbook, null);

List<BH.oM.Adapters.Excel.Worksheet> sheets = worksheets.Select(x =>
{
var sheet = new BH.oM.Adapters.Excel.Worksheet();
Worksheet sheet = new BH.oM.Adapters.Excel.Worksheet();
sheet.Name = x.Name;
return sheet;
}).ToList();
Expand All @@ -120,13 +127,13 @@
if (string.IsNullOrEmpty(range.From.Column))
range.From.Column = "A";

if(range.From.Row == -1)
if (range.From.Row == -1)
range.From.Row = 1;

if (string.IsNullOrEmpty(range.To.Column))
range.To.Column = MaximumColumnName(workbook, worksheet);

if(range.To.Row == -1)
if (range.To.Row == -1)
range.To.Row = MaximumRowNumber(workbook, worksheet);

rangeString = range.ToExcel();
Expand Down Expand Up @@ -250,42 +257,42 @@
List<string> customProperties = typeof(CustomObject).GetProperties().Select(x => x.Name).ToList();
List<string> keys = rows.First().Content.Select(x => x.ToString()).ToList();

return rows.Skip(1).Select(row =>
{
CustomObject result = new CustomObject();

Dictionary<string, object> item = new Dictionary<string, object>();
for (int i = 0; i < Math.Min((int)keys.Count(), (int)row.Content?.Count()); i ++)
for (int i = 0; i < Math.Min((int)keys.Count(), (int)row.Content?.Count()); i++)
{
if (customProperties.Contains(keys[i]))
result.SetPropertyValue(keys[i], row.Content[i]);
else
item[keys[i]] = row.Content[i];
}

result.CustomData = item;
return result;

}).ToList<IBHoMObject>();

Check warning on line 276 in Excel_Adapter/CRUD/Read/Read.cs

View check run for this annotation

BHoMBot-CI / code-compliance

Excel_Adapter/CRUD/Read/Read.cs#L260-L276

The use of CustomData within the code is discouraged except in circumstances where volatile data is being used. - For more information see https://bhom.xyz/documentation/DevOps/Code%20Compliance%20and%20CI/Compliance%20Checks/IsUsingCustomData
}

/***************************************************/

private string MaximumColumnName(IXLWorkbook workbook, string worksheet)
{
var sheet = Worksheets(workbook, worksheet).FirstOrDefault();
IXLWorksheet sheet = Worksheets(workbook, worksheet).FirstOrDefault();
if (sheet == null)
return "XFD"; //Maximum Excel Column name

var columnNumber = sheet.LastColumnUsed().ColumnNumber();
return ConvertToColumnName(columnNumber);
int columnNumber = sheet.LastColumnUsed().ColumnNumber();
return ConvertToColumnName(columnNumber);
}

/***************************************************/

private int MaximumRowNumber(IXLWorkbook workbook, string worksheet)
{
var sheet = Worksheets(workbook, worksheet).FirstOrDefault();
IXLWorksheet sheet = Worksheets(workbook, worksheet).FirstOrDefault();
if (sheet == null)
return 1048576; //Maximum Excel Row number

Expand Down
39 changes: 39 additions & 0 deletions Excel_oM/ResultItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* This file is part of the Buildings and Habitats object Model (BHoM)
* Copyright (c) 2015 - 2025, the respective contributors. All rights reserved.
*
* Each contributor holds copyright over their respective contributions.
* The project versioning (Git) records all such contribution source information.
*
*
* The BHoM is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3.0 of the License, or
* (at your option) any later version.
*
* The BHoM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
*/

using BH.oM.Base;
using BH.oM.Base.Attributes;
using BH.oM.Data.Requests;
using System.Collections.Generic;

namespace BH.oM.Adapters.Excel
{
public class ResultItem : BHoMObject
{
public virtual List<IBHoMObject> Objects { get; set; } = new List<IBHoMObject>();

public virtual IRequest OriginalRequest { get; set; } = null;
}
}