From 90ceeeea0f605e162cfd0bcf2ea76122ffdc0dc9 Mon Sep 17 00:00:00 2001 From: Thomas Edward Kingstone Date: Wed, 5 Feb 2025 11:47:44 +0000 Subject: [PATCH 01/11] added ability to update tables, untested as of now but fixes coming --- PowerPoint_Adapter/CRUD/Update/Table.cs | 177 ++++++++++++++++++++++++ PowerPoint_oM/Update/TableUpdate.cs | 20 +++ 2 files changed, 197 insertions(+) create mode 100644 PowerPoint_Adapter/CRUD/Update/Table.cs create mode 100644 PowerPoint_oM/Update/TableUpdate.cs diff --git a/PowerPoint_Adapter/CRUD/Update/Table.cs b/PowerPoint_Adapter/CRUD/Update/Table.cs new file mode 100644 index 0000000..c521bf2 --- /dev/null +++ b/PowerPoint_Adapter/CRUD/Update/Table.cs @@ -0,0 +1,177 @@ +using BH.oM.PowerPoint; +using DocumentFormat.OpenXml; +using DocumentFormat.OpenXml.Packaging; +using DocumentFormat.OpenXml.Presentation; +using D = DocumentFormat.OpenXml.Drawing; +using System; +using System.Collections.Generic; +using System.Text; +using System.Linq; +using BH.Engine.Base; + +namespace BH.Adapter.PowerPoint +{ + public partial class PowerPointAdapter + { + private void UpdateSlide(SlidePart slidePart, TableUpdate update) + { + if (update.Contents.IsNullOrEmpty()) + { + BH.Engine.Base.Compute.RecordError("The table update has no contents to update the table with."); + return; + } + + if (!update.Contents.All(x => update.Contents[0].Count == x.Count)) + { + BH.Engine.Base.Compute.RecordError("The length of all of the rows in Content must be equal."); + return; + } + + if (update.Contents[0].IsNullOrEmpty()) + { + BH.Engine.Base.Compute.RecordError("The table update has no contents to update the table with."); + return; + } + + int rowCount = update.Contents.Count; + int columnCount = update.Contents[0].Count; + + OpenXmlElement element = GetElementByName(slidePart, update.ElementName); + GraphicFrame frame; //tables are contained within graphic frames + + switch (element) //if the element is a graphic frame already then just use that, but if it's a placeholder then there is nothing contained so need to create a new table from scratch + { + case GraphicFrame graphicFrame: + frame = graphicFrame; + break; + case Shape shape: + frame = ConvertToGraphicFrame(shape, rowCount, columnCount); + slidePart.Slide.CommonSlideData.ShapeTree.ReplaceChild(frame, shape); + break; + default: + BH.Engine.Base.Compute.RecordError($"The element with name '{update.ElementName}' on slide {update.SlideNumber} must be either a Shape, a Table, or a Table placeholder to be updated with a table."); + return; + } + + D.Table table = frame.Graphic?.GraphicData.GetFirstChild(); + + if (table == null) + { + BH.Engine.Base.Compute.RecordError($"The element with the name `{update.ElementName}` on slide {update.SlideNumber} must contain a Table."); + return; + } + + IEnumerable columns = table.TableGrid.Descendants(); + IEnumerable rows = table.Descendants(); + + //compare to the column and row counts in the data to update. + if (columns.Count() != columnCount || rows.Count() != rowCount) + { + BH.Engine.Base.Compute.RecordError($"The shape of the data in Content must be the same shape as the table that is being updated.\nContent has {rowCount} rows and {columnCount} columns, whereas the table has {rows.Count()} rows and {columns.Count()} columns."); + return; + } + + //update the text in each row/column - This assumes that each table cell will have only one paragraph and run originally and does not modify any other properties. + + int r = 0; + int c = 0; + foreach (D.TableRow row in rows) + { + foreach (D.TableCell cell in row.Descendants()) + { + try + { + cell.TextBody.Elements().Single().Elements().Single().Text.Text = update.Contents[r][c]; + } + catch (InvalidOperationException ex) + { + BH.Engine.Base.Compute.RecordWarning(ex, $"An error occurred while trying to update cell in row {r} column {c}, due to there being more than one paragraph or run in the template cell. This cell has not been updated, but others might have been. Occurred in element `{update.ElementName}` on slide {update.SlideNumber}."); + } + catch (Exception ex) + { + BH.Engine.Base.Compute.RecordWarning(ex, $"An error occurred while trying to update cell in row {r} column {c}. Occurred in element `{update.ElementName}` on slide {update.SlideNumber}."); + return; + } + c++; + } + r++; + c = 0; + } + } + + private static GraphicFrame ConvertToGraphicFrame(Shape oldShape, int rowCount, int columnCount) + { + ShapeProperties shapeProperties = (ShapeProperties)oldShape.Descendants().Single().CloneNode(true); + NonVisualDrawingProperties drawingProps = (NonVisualDrawingProperties)oldShape.Descendants().Single().CloneNode(true); + + return new GraphicFrame( + new NonVisualGraphicFrameProperties( + drawingProps, + new NonVisualGraphicFrameDrawingProperties(), + new ApplicationNonVisualDrawingProperties() + ), + new D.Graphic( + new D.GraphicData( + ConstructNewTable(rowCount, columnCount) + ) { Uri = "http://schemas.openxmlformats.org/drawingml/2006/table" } + ), + shapeProperties + ); + } + + private static D.Table ConstructNewTable(int rowCount, int columnCount) + { + D.Table table = new D.Table(); + D.TableProperties tableProperties = new D.TableProperties(); + D.TableGrid tGrid = new D.TableGrid(); + + for (int i = 0; i < columnCount; i++) + { + tGrid.Append(new D.GridColumn()); + } + + table.Append(tableProperties); + table.Append(tGrid); + + for (int row = 0; row < rowCount; row++) + { + D.TableRow tRow = new D.TableRow(); + for (int col = 0; col < columnCount; col++) + { + tRow.Append(ConstructNewTableCell()); + } + table.Append(tRow); + } + + return table; + } + + private static D.TableCell ConstructNewTableCell() + { + D.TableCell cell = new D.TableCell(); + D.TableCellProperties properties = new D.TableCellProperties(); + D.TextBody textBody = new D.TextBody(); + D.BodyProperties bodyProperties = new D.BodyProperties(); + D.ListStyle listStyle = new D.ListStyle(); + + D.Paragraph par = new D.Paragraph(); + D.Run run = new D.Run(); + D.RunProperties runProps = new D.RunProperties(); + D.Text text = new D.Text(); //.Text property is what will be modified later + run.Append(runProps); + run.Append(text); + D.EndParagraphRunProperties endProps = new D.EndParagraphRunProperties(); + + par.Append(run); + par.Append(endProps); + textBody.Append(bodyProperties); + textBody.Append(listStyle); + textBody.Append(par); + + cell.Append(textBody); + cell.Append(properties); + + return cell; + } + } +} diff --git a/PowerPoint_oM/Update/TableUpdate.cs b/PowerPoint_oM/Update/TableUpdate.cs new file mode 100644 index 0000000..b9cda6d --- /dev/null +++ b/PowerPoint_oM/Update/TableUpdate.cs @@ -0,0 +1,20 @@ +using BH.oM.Base; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Text; + +namespace BH.oM.PowerPoint +{ + public class TableUpdate : BHoMObject, ISlideUpdate + { + [Description("Number of the slide where the update needs to happen.")] + public virtual int SlideNumber { get; set; } = -1; + + [Description("Name of the table that needs to be updated.")] + public virtual string ElementName { get; set; } + + [Description("Content to be placed into the table. Outer list indexes correspond to row numbers, and inner list indexes correspond to the column numbers. each row must be the same length.")] + public virtual List> Contents { get; set; } + } +} From 06641b4b1627e8b55b4ba415994ecc48c4644d88 Mon Sep 17 00:00:00 2001 From: Thomas Edward Kingstone Date: Wed, 5 Feb 2025 16:36:51 +0000 Subject: [PATCH 02/11] fix incorrect/invalid parts of tables, now working properly --- PowerPoint_Adapter/CRUD/Update/Table.cs | 35 ++++++++++++++++--------- PowerPoint_oM/Update/TableUpdate.cs | 3 +++ 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/PowerPoint_Adapter/CRUD/Update/Table.cs b/PowerPoint_Adapter/CRUD/Update/Table.cs index c521bf2..217d71f 100644 --- a/PowerPoint_Adapter/CRUD/Update/Table.cs +++ b/PowerPoint_Adapter/CRUD/Update/Table.cs @@ -101,25 +101,33 @@ private void UpdateSlide(SlidePart slidePart, TableUpdate update) private static GraphicFrame ConvertToGraphicFrame(Shape oldShape, int rowCount, int columnCount) { - ShapeProperties shapeProperties = (ShapeProperties)oldShape.Descendants().Single().CloneNode(true); + ShapeProperties shapeProperties = (ShapeProperties)oldShape.Descendants().Single(); NonVisualDrawingProperties drawingProps = (NonVisualDrawingProperties)oldShape.Descendants().Single().CloneNode(true); - return new GraphicFrame( + GraphicFrame gf = new GraphicFrame( new NonVisualGraphicFrameProperties( drawingProps, new NonVisualGraphicFrameDrawingProperties(), new ApplicationNonVisualDrawingProperties() ), - new D.Graphic( - new D.GraphicData( - ConstructNewTable(rowCount, columnCount) - ) { Uri = "http://schemas.openxmlformats.org/drawingml/2006/table" } - ), - shapeProperties + new Transform() { Offset = (D.Offset)shapeProperties.Transform2D.Offset.CloneNode(true), Extents = (D.Extents)shapeProperties.Transform2D.Extents.CloneNode(true) } ); + + (long width, long height) = GetSizeOfShape(shapeProperties); + + gf.Append(new D.Graphic(new D.GraphicData(ConstructNewTable(rowCount, columnCount, width, height)) { Uri = "http://schemas.openxmlformats.org/drawingml/2006/table" })); + + return gf; + } + + private static (long, long) GetSizeOfShape(ShapeProperties shapeProps) + { + long width = shapeProps.Transform2D.Extents.Cx; + long height = shapeProps.Transform2D.Extents.Cy; + return (width, height); } - private static D.Table ConstructNewTable(int rowCount, int columnCount) + private static D.Table ConstructNewTable(int rowCount, int columnCount, long shapeWidth, long shapeHeight) { D.Table table = new D.Table(); D.TableProperties tableProperties = new D.TableProperties(); @@ -127,7 +135,7 @@ private static D.Table ConstructNewTable(int rowCount, int columnCount) for (int i = 0; i < columnCount; i++) { - tGrid.Append(new D.GridColumn()); + tGrid.Append(new D.GridColumn() { Width = shapeWidth / columnCount }); } table.Append(tableProperties); @@ -135,7 +143,7 @@ private static D.Table ConstructNewTable(int rowCount, int columnCount) for (int row = 0; row < rowCount; row++) { - D.TableRow tRow = new D.TableRow(); + D.TableRow tRow = new D.TableRow() { Height = shapeHeight / rowCount }; for (int col = 0; col < columnCount; col++) { tRow.Append(ConstructNewTableCell()); @@ -146,7 +154,7 @@ private static D.Table ConstructNewTable(int rowCount, int columnCount) return table; } - private static D.TableCell ConstructNewTableCell() + private static D.TableCell ConstructNewTableCell(int fontSize = 20) { D.TableCell cell = new D.TableCell(); D.TableCellProperties properties = new D.TableCellProperties(); @@ -154,9 +162,10 @@ private static D.TableCell ConstructNewTableCell() D.BodyProperties bodyProperties = new D.BodyProperties(); D.ListStyle listStyle = new D.ListStyle(); + //TODO: figure out what to do about font size D.Paragraph par = new D.Paragraph(); D.Run run = new D.Run(); - D.RunProperties runProps = new D.RunProperties(); + D.RunProperties runProps = new D.RunProperties() { FontSize = fontSize }; D.Text text = new D.Text(); //.Text property is what will be modified later run.Append(runProps); run.Append(text); diff --git a/PowerPoint_oM/Update/TableUpdate.cs b/PowerPoint_oM/Update/TableUpdate.cs index b9cda6d..a7f298f 100644 --- a/PowerPoint_oM/Update/TableUpdate.cs +++ b/PowerPoint_oM/Update/TableUpdate.cs @@ -16,5 +16,8 @@ public class TableUpdate : BHoMObject, ISlideUpdate [Description("Content to be placed into the table. Outer list indexes correspond to row numbers, and inner list indexes correspond to the column numbers. each row must be the same length.")] public virtual List> Contents { get; set; } + + [Description("The font size of any text in the table.")] + public virtual int UpdatedTextFontSize { get; set; } = 20; } } From 1b2246be620383ddeebd553cd10d5dcf5a3aedad Mon Sep 17 00:00:00 2001 From: Thomas Edward Kingstone Date: Thu, 6 Feb 2025 10:03:40 +0000 Subject: [PATCH 03/11] move font size modification to better location --- PowerPoint_Adapter/CRUD/Update/Table.cs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/PowerPoint_Adapter/CRUD/Update/Table.cs b/PowerPoint_Adapter/CRUD/Update/Table.cs index 217d71f..2b65fee 100644 --- a/PowerPoint_Adapter/CRUD/Update/Table.cs +++ b/PowerPoint_Adapter/CRUD/Update/Table.cs @@ -81,7 +81,9 @@ private void UpdateSlide(SlidePart slidePart, TableUpdate update) { try { - cell.TextBody.Elements().Single().Elements().Single().Text.Text = update.Contents[r][c]; + D.Run run = cell.TextBody.Elements().Single().Elements().Single(); + run.Text.Text = update.Contents[r][c]; + run.RunProperties.FontSize = update.UpdatedTextFontSize; } catch (InvalidOperationException ex) { @@ -154,7 +156,7 @@ private static D.Table ConstructNewTable(int rowCount, int columnCount, long sha return table; } - private static D.TableCell ConstructNewTableCell(int fontSize = 20) + private static D.TableCell ConstructNewTableCell() { D.TableCell cell = new D.TableCell(); D.TableCellProperties properties = new D.TableCellProperties(); @@ -165,7 +167,7 @@ private static D.TableCell ConstructNewTableCell(int fontSize = 20) //TODO: figure out what to do about font size D.Paragraph par = new D.Paragraph(); D.Run run = new D.Run(); - D.RunProperties runProps = new D.RunProperties() { FontSize = fontSize }; + D.RunProperties runProps = new D.RunProperties(); D.Text text = new D.Text(); //.Text property is what will be modified later run.Append(runProps); run.Append(text); From 789ba33a4af2377fd625a65de806b108a1899c7a Mon Sep 17 00:00:00 2001 From: Thomas Edward Kingstone Date: Thu, 6 Feb 2025 14:50:18 +0000 Subject: [PATCH 04/11] font size needs to be 100x the visible number in powerpoint (20pt becomes 2000) --- PowerPoint_Adapter/CRUD/Update/Table.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PowerPoint_Adapter/CRUD/Update/Table.cs b/PowerPoint_Adapter/CRUD/Update/Table.cs index 2b65fee..c12a4f4 100644 --- a/PowerPoint_Adapter/CRUD/Update/Table.cs +++ b/PowerPoint_Adapter/CRUD/Update/Table.cs @@ -83,7 +83,7 @@ private void UpdateSlide(SlidePart slidePart, TableUpdate update) { D.Run run = cell.TextBody.Elements().Single().Elements().Single(); run.Text.Text = update.Contents[r][c]; - run.RunProperties.FontSize = update.UpdatedTextFontSize; + run.RunProperties.FontSize = update.UpdatedTextFontSize * 100; } catch (InvalidOperationException ex) { From 748db92e29ab308b97eb91a857476a65977949e3 Mon Sep 17 00:00:00 2001 From: Thomas Edward Kingstone Date: Mon, 10 Feb 2025 10:07:06 +0000 Subject: [PATCH 05/11] record error instead of warning when returning before completion --- PowerPoint_Adapter/CRUD/Update/Table.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PowerPoint_Adapter/CRUD/Update/Table.cs b/PowerPoint_Adapter/CRUD/Update/Table.cs index c12a4f4..9f677df 100644 --- a/PowerPoint_Adapter/CRUD/Update/Table.cs +++ b/PowerPoint_Adapter/CRUD/Update/Table.cs @@ -91,7 +91,7 @@ private void UpdateSlide(SlidePart slidePart, TableUpdate update) } catch (Exception ex) { - BH.Engine.Base.Compute.RecordWarning(ex, $"An error occurred while trying to update cell in row {r} column {c}. Occurred in element `{update.ElementName}` on slide {update.SlideNumber}."); + BH.Engine.Base.Compute.RecordError(ex, $"An error occurred while trying to update cell in row {r} column {c}. Occurred in element `{update.ElementName}` on slide {update.SlideNumber}."); return; } c++; From 14b3872365604c392fa9b5c146196a35f3a7fca2 Mon Sep 17 00:00:00 2001 From: Thomas Edward Kingstone Date: Thu, 13 Feb 2025 14:19:30 +0000 Subject: [PATCH 06/11] fix missing paragraphs etc. --- PowerPoint_Adapter/CRUD/Update/Table.cs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/PowerPoint_Adapter/CRUD/Update/Table.cs b/PowerPoint_Adapter/CRUD/Update/Table.cs index 9f677df..11bca1d 100644 --- a/PowerPoint_Adapter/CRUD/Update/Table.cs +++ b/PowerPoint_Adapter/CRUD/Update/Table.cs @@ -81,7 +81,21 @@ private void UpdateSlide(SlidePart slidePart, TableUpdate update) { try { - D.Run run = cell.TextBody.Elements().Single().Elements().Single(); + D.Paragraph par = cell.TextBody.Elements().SingleOrDefault(); + if (par == null) + { + par = new D.Paragraph(); + par.AddChild(new D.Run(new D.RunProperties(), new D.Text())); + cell.TextBody.AddChild(par); + } + + D.Run run = par.Elements().SingleOrDefault(); + if (run == null) + { + run = new D.Run(new D.RunProperties(), new D.Text()); + par.AddChild(run); + } + run.Text.Text = update.Contents[r][c]; run.RunProperties.FontSize = update.UpdatedTextFontSize * 100; } From 083fd8937306fed0a38d6e04b1aa64dc6ac3848f Mon Sep 17 00:00:00 2001 From: BHoMBot Date: Fri, 14 Feb 2025 12:16:25 +0000 Subject: [PATCH 07/11] Resolve copyright compliance --- PowerPoint_Adapter/CRUD/Update/Table.cs | 24 +++++++++++++++++++++++- PowerPoint_oM/Update/TableUpdate.cs | 24 +++++++++++++++++++++++- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/PowerPoint_Adapter/CRUD/Update/Table.cs b/PowerPoint_Adapter/CRUD/Update/Table.cs index 11bca1d..57182b2 100644 --- a/PowerPoint_Adapter/CRUD/Update/Table.cs +++ b/PowerPoint_Adapter/CRUD/Update/Table.cs @@ -1,4 +1,26 @@ -using BH.oM.PowerPoint; +/* + * This file is part of the Buildings and Habitats object Model (BHoM) + * Copyright (c) 2015 - 2024, 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 . + */ + +using BH.oM.PowerPoint; using DocumentFormat.OpenXml; using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Presentation; diff --git a/PowerPoint_oM/Update/TableUpdate.cs b/PowerPoint_oM/Update/TableUpdate.cs index a7f298f..aa74504 100644 --- a/PowerPoint_oM/Update/TableUpdate.cs +++ b/PowerPoint_oM/Update/TableUpdate.cs @@ -1,4 +1,26 @@ -using BH.oM.Base; +/* + * This file is part of the Buildings and Habitats object Model (BHoM) + * Copyright (c) 2015 - 2024, 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 . + */ + +using BH.oM.Base; using System; using System.Collections.Generic; using System.ComponentModel; From d7d1748d7a761a84d1da9453a908d6c5dff4494c Mon Sep 17 00:00:00 2001 From: Thomas Edward Kingstone Date: Mon, 17 Feb 2025 08:58:59 +0000 Subject: [PATCH 08/11] fix copyright date --- PowerPoint_Adapter/CRUD/Update/Table.cs | 2 +- PowerPoint_oM/Update/TableUpdate.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/PowerPoint_Adapter/CRUD/Update/Table.cs b/PowerPoint_Adapter/CRUD/Update/Table.cs index 57182b2..f7f2441 100644 --- a/PowerPoint_Adapter/CRUD/Update/Table.cs +++ b/PowerPoint_Adapter/CRUD/Update/Table.cs @@ -1,6 +1,6 @@ /* * This file is part of the Buildings and Habitats object Model (BHoM) - * Copyright (c) 2015 - 2024, the respective contributors. All rights reserved. + * 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. diff --git a/PowerPoint_oM/Update/TableUpdate.cs b/PowerPoint_oM/Update/TableUpdate.cs index aa74504..d4d459f 100644 --- a/PowerPoint_oM/Update/TableUpdate.cs +++ b/PowerPoint_oM/Update/TableUpdate.cs @@ -1,6 +1,6 @@ /* * This file is part of the Buildings and Habitats object Model (BHoM) - * Copyright (c) 2015 - 2024, the respective contributors. All rights reserved. + * 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. From 94b4f35fd92743f3269b801e157c9b9a1ab56363 Mon Sep 17 00:00:00 2001 From: Thomas Edward Kingstone Date: Tue, 8 Apr 2025 11:34:14 +0100 Subject: [PATCH 09/11] slightly better null handling and added defaults to TableUpdate object --- PowerPoint_Adapter/CRUD/Update/Table.cs | 17 ++++++++--------- PowerPoint_oM/Update/TableUpdate.cs | 4 ++-- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/PowerPoint_Adapter/CRUD/Update/Table.cs b/PowerPoint_Adapter/CRUD/Update/Table.cs index f7f2441..d75bfcd 100644 --- a/PowerPoint_Adapter/CRUD/Update/Table.cs +++ b/PowerPoint_Adapter/CRUD/Update/Table.cs @@ -37,18 +37,12 @@ public partial class PowerPointAdapter { private void UpdateSlide(SlidePart slidePart, TableUpdate update) { - if (update.Contents.IsNullOrEmpty()) + if (update.Contents == null || update.Contents.IsNullOrEmpty()) { BH.Engine.Base.Compute.RecordError("The table update has no contents to update the table with."); return; } - if (!update.Contents.All(x => update.Contents[0].Count == x.Count)) - { - BH.Engine.Base.Compute.RecordError("The length of all of the rows in Content must be equal."); - return; - } - if (update.Contents[0].IsNullOrEmpty()) { BH.Engine.Base.Compute.RecordError("The table update has no contents to update the table with."); @@ -58,6 +52,12 @@ private void UpdateSlide(SlidePart slidePart, TableUpdate update) int rowCount = update.Contents.Count; int columnCount = update.Contents[0].Count; + if (!update.Contents.All(x => columnCount == x.Count)) + { + BH.Engine.Base.Compute.RecordError("The length of all of the rows in Content must be equal."); + return; + } + OpenXmlElement element = GetElementByName(slidePart, update.ElementName); GraphicFrame frame; //tables are contained within graphic frames @@ -75,7 +75,7 @@ private void UpdateSlide(SlidePart slidePart, TableUpdate update) return; } - D.Table table = frame.Graphic?.GraphicData.GetFirstChild(); + D.Table table = frame.Graphic?.GraphicData?.GetFirstChild(); if (table == null) { @@ -200,7 +200,6 @@ private static D.TableCell ConstructNewTableCell() D.BodyProperties bodyProperties = new D.BodyProperties(); D.ListStyle listStyle = new D.ListStyle(); - //TODO: figure out what to do about font size D.Paragraph par = new D.Paragraph(); D.Run run = new D.Run(); D.RunProperties runProps = new D.RunProperties(); diff --git a/PowerPoint_oM/Update/TableUpdate.cs b/PowerPoint_oM/Update/TableUpdate.cs index d4d459f..eb9ec5d 100644 --- a/PowerPoint_oM/Update/TableUpdate.cs +++ b/PowerPoint_oM/Update/TableUpdate.cs @@ -34,10 +34,10 @@ public class TableUpdate : BHoMObject, ISlideUpdate public virtual int SlideNumber { get; set; } = -1; [Description("Name of the table that needs to be updated.")] - public virtual string ElementName { get; set; } + public virtual string ElementName { get; set; } = ""; [Description("Content to be placed into the table. Outer list indexes correspond to row numbers, and inner list indexes correspond to the column numbers. each row must be the same length.")] - public virtual List> Contents { get; set; } + public virtual List> Contents { get; set; } = new List>(); [Description("The font size of any text in the table.")] public virtual int UpdatedTextFontSize { get; set; } = 20; From 000ae2a256759d7677cda2cac5855448a953fe01 Mon Sep 17 00:00:00 2001 From: Thomas Edward Kingstone Date: Thu, 1 May 2025 09:46:50 +0100 Subject: [PATCH 10/11] add header row to table update --- PowerPoint_Adapter/CRUD/Update/Table.cs | 28 ++++++++++++++++++++++--- PowerPoint_oM/Update/TableUpdate.cs | 7 +++++-- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/PowerPoint_Adapter/CRUD/Update/Table.cs b/PowerPoint_Adapter/CRUD/Update/Table.cs index d75bfcd..c17ec75 100644 --- a/PowerPoint_Adapter/CRUD/Update/Table.cs +++ b/PowerPoint_Adapter/CRUD/Update/Table.cs @@ -52,12 +52,21 @@ private void UpdateSlide(SlidePart slidePart, TableUpdate update) int rowCount = update.Contents.Count; int columnCount = update.Contents[0].Count; - if (!update.Contents.All(x => columnCount == x.Count)) + if (update.Contents.Any(x => columnCount != x.Count)) { BH.Engine.Base.Compute.RecordError("The length of all of the rows in Content must be equal."); return; } + if (update.HeaderRow.Count > 0 && update.HeaderRow.Count != columnCount) + { + BH.Engine.Base.Compute.RecordError($"The length of the header row ({update.HeaderRow.Count}) must be equal to the length of the content rows ({columnCount})."); + return; + } + + if (update.HeaderRow.Count > 0) + rowCount += 1; + OpenXmlElement element = GetElementByName(slidePart, update.ElementName); GraphicFrame frame; //tables are contained within graphic frames @@ -97,6 +106,7 @@ private void UpdateSlide(SlidePart slidePart, TableUpdate update) int r = 0; int c = 0; + bool headerRow = update.HeaderRow.Count != 0; foreach (D.TableRow row in rows) { foreach (D.TableCell cell in row.Descendants()) @@ -112,14 +122,26 @@ private void UpdateSlide(SlidePart slidePart, TableUpdate update) } D.Run run = par.Elements().SingleOrDefault(); + int? fontSize = null; if (run == null) { run = new D.Run(new D.RunProperties(), new D.Text()); par.AddChild(run); } + else + fontSize = run.RunProperties.FontSize / 100; + + if (update.UpdatedTextFontSize <= 0) + fontSize = fontSize ?? 20; + else + fontSize = update.UpdatedTextFontSize; + + if (headerRow) + run.Text.Text = update.HeaderRow[c]; + else + run.Text.Text = update.Contents[r][c]; - run.Text.Text = update.Contents[r][c]; - run.RunProperties.FontSize = update.UpdatedTextFontSize * 100; + run.RunProperties.FontSize = fontSize * 100; } catch (InvalidOperationException ex) { diff --git a/PowerPoint_oM/Update/TableUpdate.cs b/PowerPoint_oM/Update/TableUpdate.cs index eb9ec5d..73d28b0 100644 --- a/PowerPoint_oM/Update/TableUpdate.cs +++ b/PowerPoint_oM/Update/TableUpdate.cs @@ -36,10 +36,13 @@ public class TableUpdate : BHoMObject, ISlideUpdate [Description("Name of the table that needs to be updated.")] public virtual string ElementName { get; set; } = ""; + [Description("Header row for the table. Leave empty to ignore and just make a table using the content. If not empty this list must be the same length as the rows in content.")] + public virtual List HeaderRow { get; set; } = new List(); + [Description("Content to be placed into the table. Outer list indexes correspond to row numbers, and inner list indexes correspond to the column numbers. each row must be the same length.")] public virtual List> Contents { get; set; } = new List>(); - [Description("The font size of any text in the table.")] - public virtual int UpdatedTextFontSize { get; set; } = 20; + [Description("The font size of any text in the table. If set to 0, does not change the font size of elements in a previous table, or if the table is a placeholder, defaults to font size of 20.")] + public virtual int UpdatedTextFontSize { get; set; } = 0; } } From 761d27d22f34a9dcc31ac6cf47ab878460f7fb04 Mon Sep 17 00:00:00 2001 From: Thomas Edward Kingstone Date: Thu, 1 May 2025 09:49:10 +0100 Subject: [PATCH 11/11] don't make all rows header rows --- PowerPoint_Adapter/CRUD/Update/Table.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/PowerPoint_Adapter/CRUD/Update/Table.cs b/PowerPoint_Adapter/CRUD/Update/Table.cs index c17ec75..4f4d186 100644 --- a/PowerPoint_Adapter/CRUD/Update/Table.cs +++ b/PowerPoint_Adapter/CRUD/Update/Table.cs @@ -137,8 +137,11 @@ private void UpdateSlide(SlidePart slidePart, TableUpdate update) fontSize = update.UpdatedTextFontSize; if (headerRow) + { run.Text.Text = update.HeaderRow[c]; - else + headerRow = false; + } + else run.Text.Text = update.Contents[r][c]; run.RunProperties.FontSize = fontSize * 100;