Skip to content

Commit bf92d0d

Browse files
author
Fraser Greenroyd
authored
Add ability to push multiple tables without reopening the file every time (#54)
2 parents 835a972 + 94c9132 commit bf92d0d

File tree

2 files changed

+110
-29
lines changed

2 files changed

+110
-29
lines changed

Excel_Adapter/AdapterActions/Push.cs

Lines changed: 73 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
using System;
3131
using System.Collections;
3232
using System.Collections.Generic;
33+
using System.Diagnostics.Eventing.Reader;
3334
using System.IO;
3435
using System.Linq;
3536

@@ -64,7 +65,7 @@ public override List<object> Push(IEnumerable<object> objects, string tag = "",
6465

6566
// Cast action config to ExcelPushConfig, create new if null.
6667
ExcelPushConfig config = actionConfig as ExcelPushConfig;
67-
if (config == null)
68+
if (config == null && !(objects.FirstOrDefault() is PushItem))
6869
{
6970
BH.Engine.Base.Compute.RecordNote($"{nameof(ExcelPushConfig)} has not been provided, default config is used.");
7071
config = new ExcelPushConfig();
@@ -93,6 +94,74 @@ public override List<object> Push(IEnumerable<object> objects, string tag = "",
9394
return new List<object>();
9495
}
9596

97+
// Push the objects
98+
List<object> pushedObjects = new List<object>();
99+
if (objects.FirstOrDefault() is PushItem)
100+
{
101+
foreach (PushItem item in objects.OfType<PushItem>())
102+
{
103+
if (PushObjects(workbook, item.Objects, item.Config, pushType))
104+
pushedObjects.AddRange(item.Objects);
105+
}
106+
}
107+
else
108+
{
109+
if (PushObjects(workbook, objects.ToList(), config, pushType))
110+
pushedObjects = objects.ToList();
111+
}
112+
113+
// Try to update the workbook properties and then save it.
114+
try
115+
{
116+
if (config != null)
117+
Update(workbook, config.WorkbookProperties);
118+
119+
if (m_FileSettings != null)
120+
workbook.SaveAs(m_FileSettings.GetFullFileName());
121+
else if (m_OutputStream != null)
122+
{
123+
workbook.SaveAs(m_OutputStream);
124+
m_OutputStream.Position = 0;
125+
}
126+
else
127+
{
128+
BH.Engine.Base.Compute.RecordError("Output stream has not been provided. The workbook cannot be saved.");
129+
return new List<object>();
130+
}
131+
132+
return pushedObjects;
133+
}
134+
catch (Exception e)
135+
{
136+
BH.Engine.Base.Compute.RecordError($"Finalisation and saving of the workbook failed with the following error: {e.Message}");
137+
return new List<object>();
138+
}
139+
}
140+
141+
/***************************************************/
142+
/**** Private Methods ****/
143+
/***************************************************/
144+
145+
private bool PushObjects(XLWorkbook workbook, List<object> objects, ExcelPushConfig config, PushType pushType = PushType.AdapterDefault)
146+
{
147+
// Makwe sure the config is defined
148+
if (config == null)
149+
{
150+
BH.Engine.Base.Compute.RecordNote($"{nameof(ExcelPushConfig)} has not been provided, default config is used.");
151+
config = new ExcelPushConfig();
152+
}
153+
154+
// Make sure that a single type of objects are pushed
155+
List<Type> objectTypes = objects.Select(x => x.GetType()).Distinct().ToList();
156+
if (objectTypes.Count != 1)
157+
{
158+
string message = "The Excel adapter only allows to push objects of a single type per table."
159+
+ "\nRight now you are providing objects of the following types: "
160+
+ objectTypes.Select(x => x.ToString()).Aggregate((a, b) => a + ", " + b);
161+
Engine.Base.Compute.RecordError(message);
162+
return false;
163+
}
164+
96165
// Split the tables into collections to delete, create and update.
97166
bool success = true;
98167
string sheetName = config.Worksheet;
@@ -134,39 +203,14 @@ public override List<object> Push(IEnumerable<object> objects, string tag = "",
134203
default:
135204
{
136205
BH.Engine.Base.Compute.RecordError($"Currently Excel adapter does not supports {nameof(PushType)} equal to {pushType}");
137-
return new List<object>();
206+
return false;
138207
}
139208
}
140209

141-
// Try to update the workbook properties and then save it.
142-
try
143-
{
144-
Update(workbook, config.WorkbookProperties);
145-
146-
if (m_FileSettings != null)
147-
workbook.SaveAs(m_FileSettings.GetFullFileName());
148-
else if (m_OutputStream != null)
149-
{
150-
workbook.SaveAs(m_OutputStream);
151-
m_OutputStream.Position = 0;
152-
}
153-
else
154-
{
155-
BH.Engine.Base.Compute.RecordError("Output stream has not been provided. The workbook cannot be saved.");
156-
return new List<object>();
157-
}
158-
159-
return success ? objects.ToList() : new List<object>();
160-
}
161-
catch (Exception e)
162-
{
163-
BH.Engine.Base.Compute.RecordError($"Finalisation and saving of the workbook failed with the following error: {e.Message}");
164-
return new List<object>();
165-
}
210+
return success;
166211
}
167212

168-
/***************************************************/
169-
/**** Private Methods ****/
213+
170214
/***************************************************/
171215

172216
private XLWorkbook CreateWorkbookFromFile(string fileName, PushType pushType)

Excel_oM/PushItem.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* This file is part of the Buildings and Habitats object Model (BHoM)
3+
* Copyright (c) 2015 - 2024, the respective contributors. All rights reserved.
4+
*
5+
* Each contributor holds copyright over their respective contributions.
6+
* The project versioning (Git) records all such contribution source information.
7+
*
8+
*
9+
* The BHoM is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Lesser General Public License as published by
11+
* the Free Software Foundation, either version 3.0 of the License, or
12+
* (at your option) any later version.
13+
*
14+
* The BHoM is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU Lesser General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Lesser General Public License
20+
* along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
21+
*/
22+
23+
using BH.oM.Base;
24+
using BH.oM.Base.Attributes;
25+
using System.Collections.Generic;
26+
27+
namespace BH.oM.Adapters.Excel
28+
{
29+
public class PushItem : BHoMObject
30+
{
31+
public virtual List<object> Objects { get; set; } = new List<object>();
32+
33+
public virtual ExcelPushConfig Config { get; set; } = new ExcelPushConfig();
34+
}
35+
}
36+
37+

0 commit comments

Comments
 (0)