From 35fffcc33282b6c2111b05443ca2a3760317887f Mon Sep 17 00:00:00 2001 From: swapnilsekhande <85052150+swapnilsekhande@users.noreply.github.com> Date: Fri, 6 Dec 2024 11:35:33 +0530 Subject: [PATCH] Added Delete Table function Below Function will add the delete Table function in template Processor which will help to remove the table from Word File can be usefu for condition based approach and dynamic behaviour of Word DOC --- src/PhpWord/TemplateProcessor.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/PhpWord/TemplateProcessor.php b/src/PhpWord/TemplateProcessor.php index f6fe1d8887..8e57757b91 100644 --- a/src/PhpWord/TemplateProcessor.php +++ b/src/PhpWord/TemplateProcessor.php @@ -1499,4 +1499,25 @@ public function getTempDocumentFilename(): string { return $this->tempDocumentFilename; } + + /** + * Delete Table + * + * Below function will take $search Parameter as an Input and remove Respective table from format + * + * @param string $search + * return void + */ + public function deleteTable(string $search): void + { + $search = self::ensureMacroCompleted($search); + $tagPos = strpos($this->tempDocumentMainPart, $search); + if ($tagPos) { + $tableStart = $this->findTableStart($tagPos); + $tableEnd = $this->findTableEnd($tagPos); + + // Delete the entire table + $this->tempDocumentMainPart = $this->getSlice(0, $tableStart) . $this->getSlice($tableEnd); + } + } }