From 9f82f4f4fe550f3b5535b0f5fe4d782d36bc8773 Mon Sep 17 00:00:00 2001 From: BHoMBot Date: Mon, 25 Sep 2023 14:31:54 +0100 Subject: [PATCH 1/6] Upgrade AssemblyFileVersion to 7.0.0.0 --- Excel_Adapter/Excel_Adapter.csproj | 4 ++-- Excel_Engine/Excel_Engine.csproj | 4 ++-- Excel_oM/Excel_oM.csproj | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Excel_Adapter/Excel_Adapter.csproj b/Excel_Adapter/Excel_Adapter.csproj index 14563a5..a1ed175 100644 --- a/Excel_Adapter/Excel_Adapter.csproj +++ b/Excel_Adapter/Excel_Adapter.csproj @@ -1,13 +1,13 @@ - 6.0.0.0 + 7.0.0.0 https://github.com/BHoM/Excel_Toolkit 5.0.0 BHoM Copyright © https://github.com/BHoM BH.Adapter.Excel - 6.3.0.0 + 7.0.0.0 Debug;Release;ZeroCodeTool ..\Build\ true diff --git a/Excel_Engine/Excel_Engine.csproj b/Excel_Engine/Excel_Engine.csproj index 0befd04..a2e5557 100644 --- a/Excel_Engine/Excel_Engine.csproj +++ b/Excel_Engine/Excel_Engine.csproj @@ -1,13 +1,13 @@ - 6.0.0.0 + 7.0.0.0 https://github.com/BHoM/Excel_Toolkit 5.0.0 BHoM Copyright © https://github.com/BHoM BH.Engine.Excel - 6.3.0.0 + 7.0.0.0 Debug;Release;ZeroCodeTool ..\Build\ true diff --git a/Excel_oM/Excel_oM.csproj b/Excel_oM/Excel_oM.csproj index 5819458..a8700b5 100644 --- a/Excel_oM/Excel_oM.csproj +++ b/Excel_oM/Excel_oM.csproj @@ -1,13 +1,13 @@ - 6.0.0.0 + 7.0.0.0 https://github.com/BHoM/Excel_Toolkit 5.0.0 BHoM Copyright © https://github.com/BHoM BH.oM.Excel - 6.3.0.0 + 7.0.0.0 Debug;Release;ZeroCodeTool ..\Build\ true From 4f115a8e677d0b3b37c5c74277184e5da95bced4 Mon Sep 17 00:00:00 2001 From: adecler Date: Sun, 5 Nov 2023 17:58:14 +0000 Subject: [PATCH 2/6] Add support for stream in the adapter --- Excel_Adapter/AdapterActions/Pull.cs | 2 +- Excel_Adapter/AdapterActions/Push.cs | 98 +++++++++++++++++++++------- Excel_Adapter/CRUD/Read/Read.cs | 11 +++- Excel_Adapter/ExcelAdapter.cs | 21 ++++++ Excel_Adapter/Excel_Adapter.csproj | 12 +--- 5 files changed, 108 insertions(+), 36 deletions(-) diff --git a/Excel_Adapter/AdapterActions/Pull.cs b/Excel_Adapter/AdapterActions/Pull.cs index ee93142..07159ee 100644 --- a/Excel_Adapter/AdapterActions/Pull.cs +++ b/Excel_Adapter/AdapterActions/Pull.cs @@ -42,7 +42,7 @@ public override IEnumerable Pull(IRequest request = null, PullType pullO if (request == null) request = new CellValuesRequest(); - if (!File.Exists(m_FileSettings.GetFullFileName())) + if (m_FileSettings != null && !File.Exists(m_FileSettings.GetFullFileName())) { BH.Engine.Base.Compute.RecordError("No file exists under the location specified in the settings."); return new List(); diff --git a/Excel_Adapter/AdapterActions/Push.cs b/Excel_Adapter/AdapterActions/Push.cs index 8143de6..ec090d5 100644 --- a/Excel_Adapter/AdapterActions/Push.cs +++ b/Excel_Adapter/AdapterActions/Push.cs @@ -43,6 +43,7 @@ public partial class ExcelAdapter : BHoMAdapter public override List Push(IEnumerable objects, string tag = "", PushType pushType = PushType.AdapterDefault, ActionConfig actionConfig = null) { + // Make sure there are objects to push if (objects == null || !objects.Any()) { BH.Engine.Base.Compute.RecordError("No objects were provided for Push action."); @@ -50,6 +51,13 @@ public override List Push(IEnumerable objects, string tag = "", } objects = objects.Where(x => x != null).ToList(); + // Make sure an output stream has been provided if the input is read through a stream + if (m_InputStream != null && m_OutputStream == null) + { + BH.Engine.Base.Compute.RecordError("Please set the Stream for the output to enable the push to work correctly."); + return new List(); + } + // If unset, set the pushType to AdapterSettings' value (base AdapterSettings default is FullCRUD). if (pushType == PushType.AdapterDefault) pushType = PushType.DeleteThenCreate; @@ -74,29 +82,15 @@ public override List Push(IEnumerable objects, string tag = "", } // Check if the workbook exists and create it if not. - string fileName = m_FileSettings.GetFullFileName(); - XLWorkbook workbook; - if (!File.Exists(fileName)) - { - if (pushType == PushType.UpdateOnly) - { - BH.Engine.Base.Compute.RecordError($"There is no workbook to update under {fileName}"); - return new List(); - } - - workbook = new XLWorkbook(); - } + XLWorkbook workbook = null; + if (m_FileSettings != null) + workbook = CreateWorkbookFromFile(m_FileSettings.GetFullFileName(), pushType); + else if (m_InputStream != null) + workbook = CreateWorkbookFromStream(m_InputStream); else { - try - { - workbook = new XLWorkbook(fileName); - } - catch (Exception e) - { - BH.Engine.Base.Compute.RecordError($"The existing workbook could not be accessed due to the following error: {e.Message}"); - return new List(); - } + BH.Engine.Base.Compute.RecordError("File settings or template stream have not been provided."); + return new List(); } // Split the tables into collections to delete, create and update. @@ -148,7 +142,20 @@ public override List Push(IEnumerable objects, string tag = "", try { Update(workbook, config.WorkbookProperties); - workbook.SaveAs(fileName); + + if (m_FileSettings != null) + workbook.SaveAs(m_FileSettings.GetFullFileName()); + else if (m_OutputStream != null) + { + workbook.SaveAs(m_OutputStream); + m_OutputStream.Position = 0; + } + else + { + BH.Engine.Base.Compute.RecordError("Output stream has not been provided. The workbook cannot be saved."); + return new List(); + } + return success ? objects.ToList() : new List(); } catch (Exception e) @@ -162,6 +169,53 @@ public override List Push(IEnumerable objects, string tag = "", /**** Private Methods ****/ /***************************************************/ + private XLWorkbook CreateWorkbookFromFile(string fileName, PushType pushType) + { + XLWorkbook workbook = null; + if (!File.Exists(fileName)) + { + if (pushType == PushType.UpdateOnly) + { + BH.Engine.Base.Compute.RecordError($"There is no workbook to update under {fileName}"); + return null; + } + + workbook = new XLWorkbook(); + } + else + { + try + { + workbook = new XLWorkbook(fileName); + } + catch (Exception e) + { + BH.Engine.Base.Compute.RecordError($"The existing workbook could not be accessed due to the following error: {e.Message}"); + return null; + } + } + + return workbook; + } + + /***************************************************/ + + private XLWorkbook CreateWorkbookFromStream(Stream inputStream) + { + try + { + return new XLWorkbook(inputStream); + } + catch (Exception e) + { + BH.Engine.Base.Compute.RecordError($"The existing workbook could not be accessed due to the following error: {e.Message}"); + return null; + } + } + + + /***************************************************/ + private List ToTableRows(List objects, List properties, List propertiesToIgnore, bool goDeep = false, bool transposeTable = false, bool showPropertyNames = true) { // Get the property dictionary for the object diff --git a/Excel_Adapter/CRUD/Read/Read.cs b/Excel_Adapter/CRUD/Read/Read.cs index 84f4649..0b4638b 100644 --- a/Excel_Adapter/CRUD/Read/Read.cs +++ b/Excel_Adapter/CRUD/Read/Read.cs @@ -48,9 +48,14 @@ protected override IEnumerable Read(IRequest request, ActionConfig XLWorkbook workbook = null; try { - FileStream fileStream = new FileStream(m_FileSettings.GetFullFileName(), FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete); - workbook = new XLWorkbook(fileStream); - fileStream.Close(); + if (m_FileSettings != null) + { + FileStream fileStream = new FileStream(m_FileSettings.GetFullFileName(), FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete); + workbook = new XLWorkbook(fileStream); + fileStream.Close(); + } + else + workbook = new XLWorkbook(m_InputStream); } catch { diff --git a/Excel_Adapter/ExcelAdapter.cs b/Excel_Adapter/ExcelAdapter.cs index 9e169fc..fa216fb 100644 --- a/Excel_Adapter/ExcelAdapter.cs +++ b/Excel_Adapter/ExcelAdapter.cs @@ -63,6 +63,23 @@ public ExcelAdapter(BH.oM.Adapter.FileSettings fileSettings = null) VerifySecurityEvidenceForIsolatedStorage(this.GetType().Assembly); } + /***************************************************/ + + [Description("Adapter to create a new Excel file based on an existing template.")] + [Input("templateStream", "Defines the content of the iput Excel file. This content will not be modifed")] + [Output("outputStream", "Defines the content of the new Excel file. This will be generated on a push and is not required for a pull.")] + public ExcelAdapter(Stream inputStream, Stream outputStream = null) + { + if (inputStream == null) + { + BH.Engine.Base.Compute.RecordError("Please set the Stream for the template to enable the Excel Adapter to work correctly."); + return; + } + + m_InputStream = inputStream; + m_OutputStream = outputStream; + } + /***************************************************/ /**** Override Methods ****/ /***************************************************/ @@ -124,6 +141,10 @@ private void VerifySecurityEvidenceForIsolatedStorage(Assembly assembly) private BH.oM.Adapter.FileSettings m_FileSettings = null; + private Stream m_InputStream = null; + + private Stream m_OutputStream = null; + /***************************************************/ } } diff --git a/Excel_Adapter/Excel_Adapter.csproj b/Excel_Adapter/Excel_Adapter.csproj index a1ed175..d79380c 100644 --- a/Excel_Adapter/Excel_Adapter.csproj +++ b/Excel_Adapter/Excel_Adapter.csproj @@ -23,7 +23,7 @@ - + @@ -83,22 +83,14 @@ - - - - - + - - - - From f1f88acbb118a96076f1a1ceff2ddc3df669bf5a Mon Sep 17 00:00:00 2001 From: Fraser Greenroyd Date: Tue, 7 Nov 2023 10:39:32 +0000 Subject: [PATCH 3/6] Update Excel_Adapter/ExcelAdapter.cs --- Excel_Adapter/ExcelAdapter.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Excel_Adapter/ExcelAdapter.cs b/Excel_Adapter/ExcelAdapter.cs index fa216fb..740775b 100644 --- a/Excel_Adapter/ExcelAdapter.cs +++ b/Excel_Adapter/ExcelAdapter.cs @@ -66,7 +66,7 @@ public ExcelAdapter(BH.oM.Adapter.FileSettings fileSettings = null) /***************************************************/ [Description("Adapter to create a new Excel file based on an existing template.")] - [Input("templateStream", "Defines the content of the iput Excel file. This content will not be modifed")] + [Input("inputStream", "Defines the content of the input Excel file. This content will not be modified.")] [Output("outputStream", "Defines the content of the new Excel file. This will be generated on a push and is not required for a pull.")] public ExcelAdapter(Stream inputStream, Stream outputStream = null) { From b94cf06dc4297f8d5ab99b82641ddbd0ad893a52 Mon Sep 17 00:00:00 2001 From: Fraser Greenroyd Date: Tue, 7 Nov 2023 16:54:15 +0000 Subject: [PATCH 4/6] Add closedXML dependency to nuspec --- .ci/BHoMBot/Nuget/BHoM.Interop.Excel.nuspec | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.ci/BHoMBot/Nuget/BHoM.Interop.Excel.nuspec b/.ci/BHoMBot/Nuget/BHoM.Interop.Excel.nuspec index 43577c8..450487b 100644 --- a/.ci/BHoMBot/Nuget/BHoM.Interop.Excel.nuspec +++ b/.ci/BHoMBot/Nuget/BHoM.Interop.Excel.nuspec @@ -18,6 +18,8 @@ + + From 4560ad7bd326f2d435f53f48b5873cabcea61e53 Mon Sep 17 00:00:00 2001 From: adecler Date: Tue, 21 Nov 2023 09:57:38 +0000 Subject: [PATCH 5/6] Adding copy of OpenXML in post build events --- Excel_Adapter/Excel_Adapter.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Excel_Adapter/Excel_Adapter.csproj b/Excel_Adapter/Excel_Adapter.csproj index d79380c..8038959 100644 --- a/Excel_Adapter/Excel_Adapter.csproj +++ b/Excel_Adapter/Excel_Adapter.csproj @@ -23,7 +23,7 @@ - + From 3fb863020247588d3e70fc79084daecc05a2cdd1 Mon Sep 17 00:00:00 2001 From: Fraser Greenroyd Date: Mon, 20 Nov 2023 17:57:07 +0000 Subject: [PATCH 6/6] Update nuget package to match PowerPoint Toolkit --- Excel_Adapter/Excel_Adapter.csproj | 1 + 1 file changed, 1 insertion(+) diff --git a/Excel_Adapter/Excel_Adapter.csproj b/Excel_Adapter/Excel_Adapter.csproj index 8038959..2540858 100644 --- a/Excel_Adapter/Excel_Adapter.csproj +++ b/Excel_Adapter/Excel_Adapter.csproj @@ -83,6 +83,7 @@ +