0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

エクセルを開いてセル位置、倍率を合わせる

Posted at
xxx.ps1
# 処理対象のファイル名を取得(複数可) 
$targetFiles = Get-ChildItem -Recurse -Path *.xlsx

# Excelオブジェクト作成
$excel = New-Object -ComObject Excel.Application

# 非表示のまま処理する
$excel.Visible = $false

# xlsx ファイルのループ
foreach ($file in $targetFiles) {

    # エクセルを開く
    $book = $excel.Workbooks.Open($file.FullName)

    # シートのループ
    foreach ($WorkSheet in $excel.Worksheets) {
        # 非表示シートは対象外
        if ($WorkSheet.Visible -ne $false) {
            # 処理対象のシートを選択状態にする
            $WorkSheet.Activate()

            # セル「A1」を選択状態にする
            $WorkSheet.Cells.Item(1,1).Select() | Out-Null

            # 表示倍率を100%にする
            $excel.ActiveWindow.Zoom = 100
        }

    }

    # 一番左のシートを選択状態にする(おまけ)
    $excel.Worksheets.Item(1).Activate()

    # 上書き保存する
    $book.save()
    $book.close()
}

# 閉じる
$excel.Quit()
xxx.bat
@echo on

set ps_file=.\xxx.ps1
powershell -NoProfile -ExecutionPolicy Unrestricted %ps_file%

pause
0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?