アプリ版:「スタンプのみでお礼する」機能のリリースについて

c:\testフォルダ内のサブフォルダ名を昇順にしてエクセルファイルに出力するスクリプトで、サブフォルダ名を罫線で囲みたいのですがうまくいきません。

$folderPath = "c:\test"
$excelFilePath = "c:\test\music_cd.xls"

# Get all subfolders in the folder
$subFolders = Get-ChildItem -Path $folderPath -Directory

# Sort the subfolders by name in ascending order
$subFolders = $subFolders | Sort-Object -Property Name

# Create a new Excel workbook
$excel = New-Object -ComObject Excel.Application
$workbook = $excel.Workbooks.Add()

# Add a new worksheet to the workbook
$worksheet = $workbook.Worksheets.Add()

# Add the subfolder names to the worksheet
$row = 1
foreach ($subFolder in $subFolders) {
$worksheet.Cells.Item($row, 1) = $subFolder.Name
$row++
}

# Add borders to the cells
$range = $worksheet.Range("A1:A$subFolders.Count")
$range.Borders.LineStyle = 1

# Save the workbook as music_cd.xls
$workbook.SaveAs($excelFilePath)

# Close the workbook and Excel application
$workbook.Close()
$excel.Quit()

実行すると、

HRESULT からの例外:0x800A03EC
発生場所 C:\test\music_cd.ps1:25 文字:1
+ $range = $worksheet.Range("A1:A$subFolders.Count")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException

このオブジェクトにプロパティ 'LineStyle' が見つかりません。プロパティが存在し、設定可能であることを確認してください。
発生場所 C:\test\music_cd.ps1:26 文字:1
+ $range.Borders.LineStyle = 1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) []、RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound

とエラーが出ます。サブフォルダ名だけの罫線のないエクセルファイルが出力されます。
どうすればうまくいくでしょうか?

A 回答 (1件)

powershellを現環境で触りたくないので試していませんが


エラー文などから
Countが取得できていないのではないかと想像します

$n=$subFolders.Count
$range = $worksheet.Range("A1:A$n")
でどうかな・・・無駄な時間でしたらごめんなさい
xls 現行だとxlsxですかね
    • good
    • 1
この回答へのお礼

上手くいきました!!ありがとうございました。

お礼日時:2023/10/13 18:28

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!

このQ&Aを見た人はこんなQ&Aも見ています


このQ&Aを見た人がよく見るQ&A