LoginSignup
0

More than 1 year has passed since last update.

マクロでjsonデータを作ってみる

Last updated at Posted at 2023-03-04
Sub Macro1()

    '型定義
    Dim wb As Workbook
    Dim ws As Worksheet
    Dim startDt As Date
    Dim endDt As Date
    Dim countd As Integer
    Dim timestamp As String
    Dim A As String
    Dim B As String
    Dim C As String
    Dim K As String
    Dim jsonData As String
    Dim jsonOutput As String
    Dim jsonList As String
    
    Set wb = ThisWorkbook
    Set ws = wb.Sheets("Sheet1")
    
    '日付範囲の設定 CDate = 文字列2023/2/2をDate型に変換
    startDt = CDate(ws.Range("B1").Value)
    endDt = CDate(ws.Range("C1").Value)
    
    'JSONデータの生成
    For D = startDt To endDt '(例: 2023/2/2~2023/2/5)分ループ
        'データの取得
        timestamp = Format(D, "yyyymmdd")
        A = ws.Range("B2").Value
        B = ws.Range("B3").Value
        C = ws.Range("B4").Value
        K = ws.Range("B6").Value
        countd = ws.Range("B5").Value 'ネストJSON数
        
        'JSONデータの生成
        jsonList = ""
        If countd > 0 Then
            For i = 1 To countd
                jsonList = jsonList & """count" & i & """:{""あいう"": " & K & "}"
                If i < countd Then
                    jsonList = jsonList & "," 'jsonList生成した"count1": {"K": 4000}を「,」で繋ぐ
                End If
            Next i
            jsonList = """list"": {" & jsonList & "}" '"count1": {"あいう": 4000},"count2": {"あいう": 4000}
        Else
            jsonList = """list"": {""number"": 0, ""D"": " & D & "}"  '←無くても良い。
        End If
        
        '取得したデータまとめる
        jsonData = "{""timeStamp"": " & timestamp & ", ""A"": " & A & ", ""B"": " & B & ", ""C"": " & C & ", " & jsonList & "}"
        
        'JSONデータを配列に追加
        jsonOutput = jsonOutput & jsonData
        If D < endDt Then
            jsonOutput = jsonOutput & ","
        End If
    Next D 'ループ終了
    
    'JSONデータの出力
    Open "C:\Users\socia\OneDrive\デスクトップ\output.json" For Output As #1
    Print #1, "[" & jsonOutput & "]"
    Close #1
    MsgBox "JSONデータ生成完了"
End Sub



スクリーンショット 2023-03-04 095732.png

意味

■ For Next
For カウンタ名 = 初期値 To 到達値
    処理
Next カウンタ名
→
JavaScript:
 var startDate = new Date("2023-02-01");
 var endDate = new Date("2023-02-03");
 for (var d = startDate; d < endDate; d.setDate(d.getDate() + 1)) {
   console.log(d.getFullYear() + '/' + (d.getMonth() + 1) + '/' + d.getDate());
 }

■ if For
→B5セルの入力値が0より大きければ、ループ(For)する。
 →1~カウント数分ループ。
 If countd > 0 Then
  For i = 1 To countd
    ~~~~~~
  Next i
 End If

■ jsonData = 
→ネストjson作成のため、親objとその中で生成される子objを1つのjsonに連結するため。
{"timeStamp":20230202,
 "A":1000,
 "B":2000,
 "C":3000,
 "list":{
   "count1":{"あいう":4000},
      "count2":{"あいう":4000}
     }
}

■ jsonOutput = jsonOutput & jsonData
→jsonDataと同様に、jsonDataは各日付毎に親と子objを連結して1日単位で1つのjsonを生成していました。
 jsonOutputでは、jsonDataで1日単位のjsonを連結して範囲内の日付を連結した1つのjsonを生成します。
→例:
    {
        "timeStamp": 20230201,
    ~~~~~~
    },
    {
        "timeStamp": 20230202,
       ~~~~~~~~
    },

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