0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

増殖したスタイルをまとめて削除したい

0
Last updated at Posted at 2024-08-17

見つけた情報が自分の環境では動かなかったので、同等のスクリプトを PowerShell で動くようにしました。

例によって batファイルを経由して動く 想定です。

試運転では問題なく動いているように見えますが、実際に処理したいファイルでは試していません。試して駄目だったらまた考えます。

動きました。

$excel = new-object -com excel.application
$excel.visible = $True

foreach ( $xlsx in $Args) {
    Write-Host $("Opening...: " + $xlsx)
    $book = $excel.Workbooks.Open($xlsx)
    $changeflag = $False
    $count = 0
    $book.Styles | Where-Object Builtin -eq $False | ForEach-Object {
        Write-Host $("Style Deleting...: " + $_.Name)
        $_.Delete() > $null
        $changeflag = $True
        
        if($count++ -gt 500){
            Write-Host "Saving...: $xlsx"
            $book.Save()
            $count = 0
        }
    }
    if ($changeflag) {
        Write-Host "Saving...: $xlsx"
        $book.Save()
    }
    $book.Close()
}

$excel.quit()

一部消えないスタイルがありましたが、style.xmlを修正 したら消せるようになりました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?