Skip to content

Instantly share code, notes, and snippets.

@staticWagomU
Last active July 15, 2025 05:43
Show Gist options
  • Select an option

  • Save staticWagomU/d82a53c560c549f892a2a2f3b273434e to your computer and use it in GitHub Desktop.

Select an option

Save staticWagomU/d82a53c560c549f892a2a2f3b273434e to your computer and use it in GitHub Desktop.
vbaファイルをバージョン管理するやつ
param (
[string]$xlsmRelativePath
)
# 相対パスを絶対パスに変換
$xlsmPath = [System.IO.Path]::GetFullPath([System.IO.Path]::Combine($PSScriptRoot, $xlsmRelativePath))
if ([System.IO.Path]::GetExtension($xlsmPath).ToLower() -ne ".xlsm") {
Write-Host "エラー: 指定されたファイルは .xlsm ファイルではありません。"
exit
}
function Get-VBComponentTypeExtension {
param(
[int]$Type
)
switch ($Type) {
1 { return "bas" }
2 { return "cls" }
3 { return "frm" }
default { return "unk" }
}
}
try {
$xl = New-Object -ComObject Excel.Application
$xl.Visible = $false
$xl.DisplayAlerts = $false
$wb = $xl.Workbooks.Open($xlsmPath)
$wb.VBProject.VBComponents | ForEach-Object {
$extension = Get-VBComponentTypeExtension $_.Type
if ($extension -ne "unk") {
$path = Join-Path (Split-Path $xlsmPath) "src" "$($_.Name).$extension"
Write-Host "エクスポート中: $path"
$_.Export($path)
}
}
}
catch {
Write-Host "エラーが発生しました: $_"
}
finally {
if ($wb) {
$wb.Close($false)
}
if ($xl) {
$xl.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($xl) | Out-Null
}
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
}
# EXAMPLE USAGE:
#
# Refer for explanation to following link:
# https://github.com/evilmartians/lefthook/blob/master/docs/configuration.md
#
pre-commit:
commands:
export-vba:
glob: "*.xlsm"
run: pwsh export.ps1 -xlsmRelativePath .\foo.xlsm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment