|
30 | 30 | using System;
|
31 | 31 | using System.Collections;
|
32 | 32 | using System.Collections.Generic;
|
| 33 | +using System.Diagnostics.Eventing.Reader; |
33 | 34 | using System.IO;
|
34 | 35 | using System.Linq;
|
35 | 36 |
|
@@ -64,7 +65,7 @@ public override List<object> Push(IEnumerable<object> objects, string tag = "",
|
64 | 65 |
|
65 | 66 | // Cast action config to ExcelPushConfig, create new if null.
|
66 | 67 | ExcelPushConfig config = actionConfig as ExcelPushConfig;
|
67 |
| - if (config == null) |
| 68 | + if (config == null && !(objects.FirstOrDefault() is PushItem)) |
68 | 69 | {
|
69 | 70 | BH.Engine.Base.Compute.RecordNote($"{nameof(ExcelPushConfig)} has not been provided, default config is used.");
|
70 | 71 | config = new ExcelPushConfig();
|
@@ -93,6 +94,74 @@ public override List<object> Push(IEnumerable<object> objects, string tag = "",
|
93 | 94 | return new List<object>();
|
94 | 95 | }
|
95 | 96 |
|
| 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 | + |
96 | 165 | // Split the tables into collections to delete, create and update.
|
97 | 166 | bool success = true;
|
98 | 167 | string sheetName = config.Worksheet;
|
@@ -134,39 +203,14 @@ public override List<object> Push(IEnumerable<object> objects, string tag = "",
|
134 | 203 | default:
|
135 | 204 | {
|
136 | 205 | BH.Engine.Base.Compute.RecordError($"Currently Excel adapter does not supports {nameof(PushType)} equal to {pushType}");
|
137 |
| - return new List<object>(); |
| 206 | + return false; |
138 | 207 | }
|
139 | 208 | }
|
140 | 209 |
|
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; |
166 | 211 | }
|
167 | 212 |
|
168 |
| - /***************************************************/ |
169 |
| - /**** Private Methods ****/ |
| 213 | + |
170 | 214 | /***************************************************/
|
171 | 215 |
|
172 | 216 | private XLWorkbook CreateWorkbookFromFile(string fileName, PushType pushType)
|
|
0 commit comments