elidorcz1

pw7 - Office-komentáře mazani času

Aug 25th, 2025 (edited)
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PowerShell 21.26 KB | Software | 0 0
  1. #
  2. # cistis.ps1 - Odstranuje casove udaje z Office dokumentu, zachovava autory
  3. #
  4.  
  5. param(
  6.     [Parameter(Mandatory=$true, Position=0)]
  7.     [string]$InputFile,
  8.    
  9.     [Parameter(Mandatory=$false)]
  10.     [string]$BackupDir = "",
  11.    
  12.     [Parameter(Mandatory=$false)]
  13.     [switch]$Test
  14. )
  15.  
  16. # Konfigurace
  17. $CleanExcelRevisions = $true
  18. if ($Test) {
  19.     $VerbosePreference = "Continue"
  20. } else {
  21.     $VerbosePreference = "SilentlyContinue"
  22. }
  23. $DefaultBackupFolder = "zaloha"
  24. $CompressionLevel = [System.IO.Compression.CompressionLevel]::Optimal
  25. $XmlEncoding = [System.Text.Encoding]::UTF8
  26.  
  27. # Regex vzory pro Word dokumenty (vcetne revizi a komentaru)
  28. $WordAttrPatterns = @(
  29.     "\s+w:date\s*=\s*("".*?""|'.*?')",          
  30.     "\s+w14:date\s*=\s*("".*?""|'.*?')",        
  31.     "\s+w15:dateUtc\s*=\s*("".*?""|'.*?')",      
  32.     "\s+mso:date\s*=\s*("".*?""|'.*?')",        
  33.     "\s+w:modified\s*=\s*("".*?""|'.*?')",      
  34.     "\s+w:created\s*=\s*("".*?""|'.*?')",        
  35.     "\s+created\s*=\s*("".*?""|'.*?')",          
  36.     "\s+modified\s*=\s*("".*?""|'.*?')",        
  37.     "\s+lastModifiedTime\s*=\s*("".*?""|'.*?')",
  38.     'date\s*=\s*"[^"]*\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[^"]*"',  
  39.     'w:date\s*=\s*"[^"]*"',                                          
  40.     'date\s*=\s*"[^"]*"',                                          
  41.     '\s+author\s*=\s*"[^"]*"\s+date\s*=\s*"[^"]*"',                
  42.     'date\s*=\s*"[^"]*\d{4}-\d{2}-\d{2}[^"]*"'                    
  43. )
  44.  
  45. # Regex vzory pro Excel dokumenty
  46. $ExcelAttrPatterns = @(
  47.     "\s+(\w+:)?(date|dateUtc|created|creationDate|modified|modifiedTime|createdTime|time|dttm|lastModifiedTime)\s*=\s*("".*?""|'.*?')",
  48.     "\s+dt\s*=\s*("".*?""|'.*?')",              
  49.     "\s+uid\s*=\s*(""[^""]*\d{4}-\d{2}-\d{2}[^""]*""|'[^']*\d{4}-\d{2}-\d{2}[^']*')"
  50. )
  51.  
  52. # Vzory pro metadata dokumentu
  53. $MetadataPatterns = @(
  54.     "<dcterms:created[^>]*>.*?</dcterms:created>",
  55.     "<dcterms:modified[^>]*>.*?</dcterms:modified>",
  56.     "<dcterms:created\s+xsi:type=""dcterms:W3CDTF"">.*?</dcterms:created>",
  57.     "<dcterms:modified\s+xsi:type=""dcterms:W3CDTF"">.*?</dcterms:modified>",
  58.     "<dc:created>.*?</dc:created>",
  59.     "<dc:modified>.*?</dc:modified>"
  60. )
  61.  
  62. $SupportedExtensions = @(".docx",".docm",".xlsx",".xlsm")
  63.  
  64. if ($Test) {
  65.     Write-Host "=== DEBUG START ===" -ForegroundColor Magenta
  66.     Write-Host "Input file: $InputFile" -ForegroundColor Yellow
  67.     Write-Host "Test mode: $Test" -ForegroundColor Yellow
  68.     Write-Host "Verbose: $($VerbosePreference)" -ForegroundColor Yellow
  69. }
  70.  
  71. # Nacita UTF-8 kodovany textovy soubor
  72. function Get-Utf8Text {
  73.     param([string]$FilePath)
  74.     return [System.IO.File]::ReadAllText($FilePath, $XmlEncoding)
  75. }
  76.  
  77. # Zapise UTF-8 kodovany textovy soubor bez BOM
  78. function Set-Utf8Text {
  79.     param([string]$FilePath, [string]$Content)
  80.     [System.IO.File]::WriteAllText($FilePath, $Content, (New-Object System.Text.UTF8Encoding($false)))
  81. }
  82.  
  83. # Vytvori verzovanou zalohu vstupniho souboru
  84. function New-VersionedBackup {
  85.     param([string]$FullPath, [string]$BackupDirectory = "")
  86.    
  87.     $dir   = Split-Path $FullPath -Parent
  88.     $name  = Split-Path $FullPath -Leaf
  89.     $base  = [System.IO.Path]::GetFileNameWithoutExtension($name)
  90.     $ext   = [System.IO.Path]::GetExtension($name)
  91.    
  92.     if ($BackupDirectory -and (Test-Path $BackupDirectory)) {
  93.         $bdir = $BackupDirectory
  94.     } else {
  95.         $bdir = Join-Path $dir $DefaultBackupFolder
  96.     }
  97.    
  98.     if (-not (Test-Path $bdir)) {
  99.         New-Item -ItemType Directory -Path $bdir | Out-Null
  100.     }
  101.    
  102.     $stamp = (Get-Date).ToString("yyyyMMdd_HHmmss")
  103.     $bfile = Join-Path $bdir ("{0}_{1}{2}" -f $base,$stamp,$ext)
  104.    
  105.     if (-not $Test) {
  106.         Copy-Item -LiteralPath $FullPath -Destination $bfile -Force
  107.     }
  108.    
  109.     if (-not $Test) {
  110.         Write-Host "Vytvarim zalohu: $bfile" -ForegroundColor Green
  111.     } else {
  112.         Write-Host "Vytvarim zalohu: $bfile" -ForegroundColor Green
  113.     }
  114.     return $bfile
  115. }
  116.  
  117. # Zpracuje jednotlivy XML soubor a odstrani casove atributy
  118. function Process-XmlFile {
  119.     param(
  120.         [System.IO.FileInfo]$XmlFile,
  121.         [string[]]$Patterns,
  122.         [string]$FilePrefix,
  123.         [string]$UnzippedPath
  124.     )
  125.    
  126.     $txt = Get-Utf8Text $XmlFile.FullName
  127.     $fileRemoved = 0
  128.     $originalTxt = $txt
  129.    
  130.     foreach ($pat in $Patterns) {
  131.         $matches = [System.Text.RegularExpressions.Regex]::Matches($txt, $pat,
  132.             [System.Text.RegularExpressions.RegexOptions]::IgnoreCase -bor
  133.             [System.Text.RegularExpressions.RegexOptions]::Singleline)
  134.        
  135.         if ($matches.Count -gt 0) {
  136.             Write-Verbose ("  Nalezeno {0} vyskytov vzorem: {1}" -f $matches.Count, $pat)
  137.             foreach ($match in $matches) {
  138.                 Write-Verbose ("    Odstranuji: {0}" -f $match.Value.Trim())
  139.             }
  140.            
  141.             $txt = [System.Text.RegularExpressions.Regex]::Replace($txt, $pat, "",
  142.                 [System.Text.RegularExpressions.RegexOptions]::IgnoreCase -bor
  143.                 [System.Text.RegularExpressions.RegexOptions]::Singleline)
  144.            
  145.             $fileRemoved += $matches.Count
  146.         }
  147.     }
  148.    
  149.     if ($fileRemoved -gt 0) {
  150.         $relativePath = $XmlFile.FullName.Replace($UnzippedPath, '')
  151.        
  152.         if (-not $Test) {
  153.             Set-Utf8Text $XmlFile.FullName $txt
  154.         }
  155.        
  156.         Write-Verbose ("{0} {1} removed={2}" -f $FilePrefix, $relativePath, $fileRemoved)
  157.     }
  158.    
  159.     return @{ RemovedCount = $fileRemoved }
  160. }
  161.  
  162. # Zpracuje soubory komentaru specificke pro odstraneni casovych udaju
  163. function Process-CommentFiles {
  164.     param([string]$UnzippedPath, [string]$FileType)
  165.    
  166.     if ($Test) {
  167.         Write-Host "DEBUG: Zpracovavam komentare pro $FileType" -ForegroundColor Cyan
  168.     }
  169.    
  170.     $changed = 0
  171.     $totalRemoved = 0
  172.    
  173.     if ($FileType -eq "Word") {
  174.         $wordFolder = Join-Path $UnzippedPath "word"
  175.         $commentFiles = @()
  176.        
  177.         if ($Test) {
  178.             Write-Host "DEBUG: Hledam komentare ve slozce: $wordFolder" -ForegroundColor Cyan
  179.         }
  180.        
  181.         # Hlavni soubor komentaru
  182.         $mainComments = Join-Path $wordFolder "comments.xml"
  183.         if (Test-Path $mainComments) {
  184.             $commentFiles += Get-Item $mainComments
  185.             if ($Test) {
  186.                 Write-Host "DEBUG: Nalezen hlavni soubor komentaru: comments.xml" -ForegroundColor Green
  187.             }
  188.         }
  189.        
  190.         # Komentare v dalsich souborech
  191.         $additionalComments = Get-ChildItem -Path $wordFolder -Filter "*comment*.xml" -Recurse -File -ErrorAction SilentlyContinue
  192.         if ($additionalComments) {
  193.             $commentFiles += $additionalComments
  194.             if ($Test) {
  195.                 Write-Host "DEBUG: Nalezeno $($additionalComments.Count) dalsich souboru s komentari" -ForegroundColor Green
  196.             }
  197.         }
  198.        
  199.         # Zkontroluj take document.xml kde mohou byt komentare
  200.         $docXml = Join-Path $wordFolder "document.xml"
  201.         if (Test-Path $docXml) {
  202.             $commentFiles += Get-Item $docXml
  203.             if ($Test) {
  204.                 Write-Host "DEBUG: Pridavam document.xml pro kontrolu komentaru" -ForegroundColor Green
  205.             }
  206.         }
  207.        
  208.     } else {
  209.         # Excel komentare
  210.         $xl = Join-Path $UnzippedPath "xl"
  211.         $commentFiles = Get-ChildItem -Path $xl -Filter "*comment*.xml" -Recurse -File -ErrorAction SilentlyContinue
  212.     }
  213.    
  214.     if ($Test) {
  215.         Write-Host "DEBUG: Celkem souboru ke kontrole: $($commentFiles.Count)" -ForegroundColor Cyan
  216.     }
  217.    
  218.     foreach ($commentFile in $commentFiles) {
  219.         if ($Test) {
  220.             Write-Host "DEBUG: Zpracovavam komentare v: $($commentFile.Name)" -ForegroundColor Yellow
  221.         }
  222.        
  223.         $txt = Get-Utf8Text $commentFile.FullName
  224.         $originalTxt = $txt
  225.         $fileRemoved = 0
  226.        
  227.         # Ukaz cast obsahu pro debug (pouze v testu)
  228.         if ($Test) {
  229.             Write-Host "DEBUG: Prvnich 200 znaku souboru:" -ForegroundColor Gray
  230.             Write-Host ($txt.Substring(0, [Math]::Min(200, $txt.Length))) -ForegroundColor Gray
  231.         }
  232.        
  233.         # Specialni vzory pro komentare
  234.         $commentPatterns = @(
  235.             'date\s*=\s*"[^"]*\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[^"]*"',  # date="2025-08-25T11:39:00Z"
  236.             'w:date\s*=\s*"[^"]*"',                                          # w:date="..."
  237.             'date\s*=\s*"[^"]*"',                                           # obecny date="..."
  238.             'author\s*=\s*"[^"]*"\s+date\s*=\s*"[^"]*"',                   # author="..." date="..."
  239.             'date\s*=\s*"[^"]*\d{4}-\d{2}-\d{2}[^"]*"'                     # jakykoli date s datem
  240.         )
  241.        
  242.         foreach ($pattern in $commentPatterns) {
  243.             $matches = [System.Text.RegularExpressions.Regex]::Matches($txt, $pattern,
  244.                 [System.Text.RegularExpressions.RegexOptions]::IgnoreCase)
  245.            
  246.             if ($matches.Count -gt 0) {
  247.                 if ($Test) {
  248.                     Write-Host "  DEBUG: Nalezeno $($matches.Count) casovych udaju vzorem: $pattern" -ForegroundColor Green
  249.                     foreach ($match in $matches) {
  250.                         Write-Host "    DEBUG: Odstranuji: $($match.Value)" -ForegroundColor Red
  251.                     }
  252.                 }
  253.                
  254.                 # Uplne odstran casove atributy
  255.                 $txt = [System.Text.RegularExpressions.Regex]::Replace($txt, $pattern, "",
  256.                     [System.Text.RegularExpressions.RegexOptions]::IgnoreCase)
  257.                
  258.                 $fileRemoved += $matches.Count
  259.             }
  260.         }
  261.        
  262.         if ($fileRemoved -gt 0) {
  263.             if (-not $Test) {
  264.                 Set-Utf8Text $commentFile.FullName $txt
  265.             }
  266.            
  267.             $changed++
  268.             $totalRemoved += $fileRemoved
  269.             if ($Test) {
  270.                 Write-Host "[C] $($commentFile.Name) removed=$fileRemoved" -ForegroundColor Green
  271.             }
  272.         } else {
  273.             if ($Test) {
  274.                 Write-Host "DEBUG: V souboru $($commentFile.Name) nebyly nalezeny casove udaje" -ForegroundColor Gray
  275.             }
  276.         }
  277.     }
  278.    
  279.     return @{ Changed = $changed; Removed = $totalRemoved }
  280. }
  281.  
  282. # Zpracuje metadata dokumentu a odstrani casove udaje
  283. function Process-DocumentMetadata {
  284.     param([string]$UnzippedPath)
  285.    
  286.     $changed = 0
  287.     $totalRemoved = 0
  288.    
  289.     # Zpracuj core.xml (zakladni metadata)
  290.     $coreXml = Join-Path $UnzippedPath "docProps\core.xml"
  291.     if (Test-Path $coreXml) {
  292.         $txt = Get-Utf8Text $coreXml
  293.         $originalTxt = $txt
  294.        
  295.         foreach ($pat in $MetadataPatterns) {
  296.             $matches = [System.Text.RegularExpressions.Regex]::Matches($txt, $pat,
  297.                 [System.Text.RegularExpressions.RegexOptions]::IgnoreCase -bor
  298.                 [System.Text.RegularExpressions.RegexOptions]::Singleline)
  299.            
  300.             if ($matches.Count -gt 0) {
  301.                 $txt = [System.Text.RegularExpressions.Regex]::Replace($txt, $pat, "",
  302.                     [System.Text.RegularExpressions.RegexOptions]::IgnoreCase -bor
  303.                     [System.Text.RegularExpressions.RegexOptions]::Singleline)
  304.                
  305.                 $totalRemoved += $matches.Count
  306.             }
  307.         }
  308.        
  309.         if ($txt -ne $originalTxt) {
  310.             if (-not $Test) {
  311.                 Set-Utf8Text $coreXml $txt
  312.             }
  313.             $changed++
  314.             Write-Verbose "[M] core.xml removed=$totalRemoved"
  315.         }
  316.     }
  317.    
  318.     return @{ Changed = $changed; Removed = $totalRemoved }
  319. }
  320.  
  321. # Zpracuje Word dokument - najde a zpracuje vsechny XML soubory
  322. function Process-WordDocument {
  323.     param([string]$UnzippedPath)
  324.    
  325.     $wordFolder = Join-Path $UnzippedPath "word"
  326.     if (-not (Test-Path $wordFolder)) {
  327.         throw "Neplatna DOCX struktura (chybi /word slozka)"
  328.     }
  329.    
  330.     $targets = Get-ChildItem -Path $wordFolder -Filter *.xml -Recurse -File -ErrorAction SilentlyContinue
  331.     $changed = 0
  332.     $totalRemoved = 0
  333.    
  334.     if ($Test) {
  335.         Write-Host "DEBUG: Nalezeno $($targets.Count) XML souboru ve Word slozce" -ForegroundColor Cyan
  336.     }
  337.    
  338.     foreach ($xmlFile in $targets) {
  339.         if ($Test) {
  340.             Write-Host "DEBUG: Zpracovavam: $($xmlFile.Name)" -ForegroundColor Gray
  341.         }
  342.         $result = Process-XmlFile -XmlFile $xmlFile -Patterns $WordAttrPatterns -FilePrefix "[W]" -UnzippedPath $UnzippedPath
  343.        
  344.         if ($result.RemovedCount -gt 0) {
  345.             $changed++
  346.             $totalRemoved += $result.RemovedCount
  347.         }
  348.     }
  349.    
  350.     return @{ Changed = $changed; Removed = $totalRemoved }
  351. }
  352.  
  353. # Zpracuje Excel sešit - najde a zpracuje komentare a revize
  354. function Process-ExcelWorkbook {
  355.     param([string]$UnzippedPath)
  356.    
  357.     $xl = Join-Path $UnzippedPath "xl"
  358.     if (-not (Test-Path $xl)) {
  359.         throw "Neplatna XLSX struktura (chybi /xl slozka)"
  360.     }
  361.    
  362.     $changed = 0
  363.     $totalRemoved = 0
  364.    
  365.     # Komentare (legacy) a threaded komentare (moderni)
  366.     $commentFiles = Get-ChildItem -Path $xl -Filter "comments*.xml" -Recurse -File -ErrorAction SilentlyContinue
  367.     $threadedFiles = Get-ChildItem -Path $xl -Filter "threadedComments*.xml" -Recurse -File -ErrorAction SilentlyContinue
  368.    
  369.     $targets = @()
  370.     if ($commentFiles)  { $targets += $commentFiles }
  371.     if ($threadedFiles) { $targets += $threadedFiles }
  372.    
  373.     foreach ($xmlFile in $targets) {
  374.         $result = Process-XmlFile -XmlFile $xmlFile -Patterns $ExcelAttrPatterns -FilePrefix "[X]" -UnzippedPath $UnzippedPath
  375.        
  376.         if ($result.RemovedCount -gt 0) {
  377.             $changed++
  378.             $totalRemoved += $result.RemovedCount
  379.         }
  380.     }
  381.    
  382.     # Volitelne: legacy revize
  383.     if ($CleanExcelRevisions) {
  384.         $revDir = Join-Path $xl "revisions"
  385.         if (Test-Path $revDir) {
  386.             $revFiles = Get-ChildItem -Path $revDir -Filter "*.xml" -File -ErrorAction SilentlyContinue
  387.             foreach ($xmlFile in $revFiles) {
  388.                 $result = Process-XmlFile -XmlFile $xmlFile -Patterns $ExcelAttrPatterns -FilePrefix "[XR]" -UnzippedPath $UnzippedPath
  389.                
  390.                 if ($result.RemovedCount -gt 0) {
  391.                     $changed++
  392.                     $totalRemoved += $result.RemovedCount
  393.                 }
  394.             }
  395.         }
  396.     }
  397.    
  398.     return @{ Changed = $changed; Removed = $totalRemoved }
  399. }
  400.  
  401. $scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
  402. $tryPath   = Join-Path $scriptDir $InputFile
  403.  
  404. if ($Test) {
  405.     Write-Host "DEBUG: Script dir: $scriptDir" -ForegroundColor Yellow
  406.     Write-Host "DEBUG: Try path: $tryPath" -ForegroundColor Yellow
  407. }
  408.  
  409. if (Test-Path -LiteralPath $tryPath -PathType Leaf) {
  410.     $FullPath = [System.IO.Path]::GetFullPath($tryPath)
  411.     if ($Test) {
  412.         Write-Host "DEBUG: Soubor nalezen v try path: $FullPath" -ForegroundColor Green
  413.     }
  414. } elseif (Test-Path -LiteralPath $InputFile -PathType Leaf) {
  415.     $FullPath = [System.IO.Path]::GetFullPath($InputFile)
  416.     if ($Test) {
  417.         Write-Host "DEBUG: Soubor nalezen v input path: $FullPath" -ForegroundColor Green
  418.     }
  419. } else {
  420.     Write-Host "CHYBA: Soubor nebyl nalezen: $InputFile" -ForegroundColor Red
  421.     if ($Test) {
  422.         Write-Host "DEBUG: Zkusil jsem: $tryPath" -ForegroundColor Red
  423.         Write-Host "DEBUG: A take: $InputFile" -ForegroundColor Red
  424.     }
  425.     exit 1
  426. }
  427.  
  428. $Extension = [System.IO.Path]::GetExtension($FullPath).ToLowerInvariant()
  429. if ($Test) {
  430.     Write-Host "DEBUG: Extension: $Extension" -ForegroundColor Yellow
  431. }
  432.  
  433. if ($Extension -notin $SupportedExtensions) {
  434.     Write-Host "CHYBA: Podporovane jsou pouze: $($SupportedExtensions -join ', ')" -ForegroundColor Red
  435.     exit 1
  436. }
  437.  
  438. if ($Test -or $VerbosePreference -eq "Continue") {
  439.     Write-Host "POZOR: Modifikace znevazni digitalni podpisy (pokud existuji)" -ForegroundColor Yellow
  440. }
  441.  
  442. if ($Test) {
  443.     Write-Host "TEST REZIM: Zadne zmeny nebudou provedeny" -ForegroundColor Cyan
  444. }
  445.  
  446. $backupFile = New-VersionedBackup -FullPath $FullPath -BackupDirectory $BackupDir
  447.  
  448. Add-Type -AssemblyName System.IO.Compression.FileSystem -ErrorAction SilentlyContinue | Out-Null
  449. Add-Type -AssemblyName System.IO.Compression -ErrorAction SilentlyContinue | Out-Null
  450.  
  451. $guid     = [Guid]::NewGuid().ToString("N")
  452. $workRoot = Join-Path $env:TEMP ("CleanTimestamps_" + $guid)
  453. $unzipped = Join-Path $workRoot "unzipped"
  454.  
  455. if ($Test) {
  456.     Write-Host "DEBUG: Work root: $workRoot" -ForegroundColor Yellow
  457.     Write-Host "DEBUG: Unzipped path: $unzipped" -ForegroundColor Yellow
  458. }
  459.  
  460. # Rozbal soubor i pro test (potrebujeme videt obsah)
  461. New-Item -ItemType Directory -Path $workRoot,$unzipped -Force | Out-Null
  462. if ($Test) {
  463.     Write-Host "DEBUG: Rozbaluji soubor..." -ForegroundColor Yellow
  464. }
  465. [System.IO.Compression.ZipFile]::ExtractToDirectory($FullPath, $unzipped)
  466. if ($Test) {
  467.     Write-Host "DEBUG: Soubor rozbalen" -ForegroundColor Green
  468. }
  469.  
  470. if (-not $Test) {
  471.     $fileName = Split-Path $FullPath -Leaf
  472.     if ($Extension -in @(".docx",".docm")) {
  473.         Write-Host "Zpracovavam dokument: $fileName" -ForegroundColor Yellow
  474.     } else {
  475.         Write-Host "Zpracovavam dokument: $fileName" -ForegroundColor Yellow
  476.         if ($CleanExcelRevisions) {
  477.             Write-Host "Vcetne revizi (historie bude ztracena)" -ForegroundColor Magenta
  478.         }
  479.     }
  480. }
  481.  
  482. # Zpracuj podle typu souboru
  483. $results = @{ Changed = 0; Removed = 0 }
  484. $metadataResults = @{ Changed = 0; Removed = 0 }
  485. $commentResults = @{ Changed = 0; Removed = 0 }
  486.  
  487. if ($Extension -in @(".docx",".docm")) {
  488.     # Standardni zpracovani XML souboru
  489.     if ($Test) {
  490.         Write-Host "DEBUG: Spoustim standardni zpracovani Word dokumentu" -ForegroundColor Cyan
  491.     }
  492.     $results = Process-WordDocument -UnzippedPath $unzipped
  493.    
  494.     # Specificke zpracovani komentaru
  495.     if ($Test) {
  496.         Write-Host "DEBUG: Spoustim specificke zpracovani komentaru" -ForegroundColor Cyan
  497.     }
  498.     $commentResults = Process-CommentFiles -UnzippedPath $unzipped -FileType "Word"
  499.    
  500.     # Metadata dokumentu
  501.     if ($Test) {
  502.         Write-Host "DEBUG: Spoustim zpracovani metadat" -ForegroundColor Cyan
  503.     }
  504.     $metadataResults = Process-DocumentMetadata -UnzippedPath $unzipped
  505. } elseif ($Extension -in @(".xlsx",".xlsm")) {
  506.     $results = Process-ExcelWorkbook -UnzippedPath $unzipped
  507.     $metadataResults = Process-DocumentMetadata -UnzippedPath $unzipped
  508. }
  509.  
  510. # Zabal zpatky (pouze pokud to neni test)
  511. if (-not $Test) {
  512.     $tmpZip = Join-Path $workRoot "packed.zip"
  513.    
  514.     if (Test-Path $FullPath) {
  515.         Remove-Item $FullPath -Force
  516.     }
  517.    
  518.     [System.IO.Compression.ZipFile]::CreateFromDirectory($unzipped, $tmpZip, $CompressionLevel, $false)
  519.     Copy-Item $tmpZip $FullPath -Force
  520.    
  521.     Remove-Item $workRoot -Recurse -Force -ErrorAction SilentlyContinue
  522. } else {
  523.     Remove-Item $workRoot -Recurse -Force -ErrorAction SilentlyContinue
  524. }
  525.  
  526. # Finalni vypis
  527. Write-Host ""
  528. if (-not $Test) {
  529.     Write-Host "=== VYSLEDKY ===" -ForegroundColor Green
  530. } else {
  531.     Write-Host "=== VYSLEDKY ===" -ForegroundColor Magenta
  532. }
  533.  
  534. if ($Test) {
  535.     Write-Host ""
  536.     Write-Host "TEST DOKONCEN: $FullPath" -ForegroundColor Cyan
  537.     Write-Host "Pro skutecne provedeni spustte bez parametru -Test" -ForegroundColor Yellow
  538.    
  539.     # Ukaz co by se stalo
  540.     $totalChanged = $results.Changed + $metadataResults.Changed + $commentResults.Changed
  541.     $totalRemoved = $results.Removed + $metadataResults.Removed + $commentResults.Removed
  542.    
  543.     if ($totalRemoved -gt 0) {
  544.         Write-Host ""
  545.         Write-Host "TEST - Co by bylo odstraneno:" -ForegroundColor Yellow
  546.         Write-Host "  Zmenene XML soubory: $totalChanged" -ForegroundColor White
  547.         Write-Host "  Odstranenych casovych atributu: $totalRemoved" -ForegroundColor White
  548.         Write-Host "    - z obsahu: $($results.Removed)" -ForegroundColor Gray
  549.         Write-Host "    - z komentaru: $($commentResults.Removed)" -ForegroundColor Gray
  550.         Write-Host "    - z metadat: $($metadataResults.Removed)" -ForegroundColor Gray
  551.     } else {
  552.         Write-Host ""
  553.         Write-Host "TEST - Zadne casove atributy nebyly nalezeny" -ForegroundColor Gray
  554.     }
  555. } else {
  556.     try {
  557.         $totalChanged = $results.Changed + $metadataResults.Changed + $commentResults.Changed
  558.         $totalRemoved = $results.Removed + $metadataResults.Removed + $commentResults.Removed
  559.        
  560.         Write-Host "Zmenene XML soubory: $totalChanged" -ForegroundColor White
  561.         Write-Host "Odstranenych casovych atributu: $totalRemoved" -ForegroundColor White
  562.         Write-Host "  - z obsahu: $($results.Removed)" -ForegroundColor Gray
  563.         Write-Host "  - z komentaru: $($commentResults.Removed)" -ForegroundColor Gray
  564.         Write-Host "  - z metadat: $($metadataResults.Removed)" -ForegroundColor Gray
  565.        
  566.         if ($totalRemoved -eq 0) {
  567.             Write-Host "Vysledek: OK (zadne casove atributy nenalezeny)" -ForegroundColor Gray
  568.         } else {
  569.             Write-Host "Vysledek: OK" -ForegroundColor Green
  570.         }
  571.     } catch {
  572.         Write-Host "Vysledek: CHYBA - $($_.Exception.Message)" -ForegroundColor Red
  573.         exit 1
  574.     }
  575. }
  576.  
  577. if ($Test) {
  578.     Write-Host "=== DEBUG END ===" -ForegroundColor Magenta
  579. }
Advertisement
Add Comment
Please, Sign In to add comment