Why not login to Qiita and try out its useful features?

We'll deliver articles that match you.

You can read useful information later.

1
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?

vbaマクロでDTOを作成する

Posted at
Sub GenerateJavaDTO()
    ' 変数の定義
    Dim className As String
    Dim row As Integer
    Dim dataType As String
    Dim variableName As String
    Dim javaCode As String
    Dim annotations As String
    Dim pathSeparator As String
    Dim outputFolder As String
    Dim filePath As String
    
    ' パスセパレータを取得
    pathSeparator = Application.pathSeparator
    
    ' クラス名をセルA1から取得
    className = Range("A1").Value
    
    ' アノテーションを設定(必要に応じて変更してください)
    annotations = "@Getter" & vbCrLf & "@Setter" & vbCrLf
    
    ' クラス宣言の開始
    javaCode = annotations & "public class " & className & " {" & vbCrLf
    
    ' 行の初期化
    row = 2
    
    ' 型と変数名をセルから読み取ってフィールドを生成
    Do While Not IsEmpty(Cells(row, 1))
        dataType = Cells(row, 1).Value
        variableName = Cells(row, 2).Value
        
        ' フィールドの宣言を追加
        javaCode = javaCode & vbTab & "private " & dataType & " " & variableName & ";" & vbCrLf
        
        row = row + 1
    Loop
    
    ' クラスの終了
    javaCode = javaCode & "}"
    
    ' 出力先のフォルダを取得
    If ThisWorkbook.Path <> "" Then
        ' ブックが保存されている場合、そのパスを使用
        outputFolder = ThisWorkbook.Path
        filePath = outputFolder & pathSeparator & className & ".java"
    Else
        ' ブックが保存されていない場合、保存先を選択
        #If Mac Then
            ' Mac環境ではGetSaveAsFilenameを使用
            filePath = Application.GetSaveAsFilename(InitialFileName:=className & ".java", _
                                                     FileFilter:="Java Files (*.java), *.java")
            ' キャンセルされた場合
            If filePath = "False" Then
                MsgBox "操作がキャンセルされました。"
                Exit Sub
            End If
        #Else
            ' Windows環境ではFileDialogを使用
            With Application.FileDialog(msoFileDialogFolderPicker)
                .Title = "出力先フォルダを選択してください"
                .AllowMultiSelect = False
                If .Show = -1 Then ' フォルダが選択された場合
                    outputFolder = .SelectedItems(1)
                    filePath = outputFolder & pathSeparator & className & ".java"
                Else
                    MsgBox "操作がキャンセルされました。"
                    Exit Sub
                End If
            End With
        #End If
    End If
    
    ' ファイルに書き込み
    Dim fileNum As Integer
    fileNum = FreeFile
    
    Open filePath For Output As #fileNum
    Print #fileNum, javaCode
    Close #fileNum
    
    ' ユーザーに完了を通知
    MsgBox "Java DTOクラスファイルが作成されました: " & filePath
End Sub

A1セル クラス名
A2セル 型
B2セル 変数名

1
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
1
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?