Sub ImportDataToKintone()
Dim xmlHttpRequest As New MSXML2.ServerXMLHTTP60
Dim url As String
Dim payload As String
Dim lastRow As Long
Dim i As Long
Dim deliveryDate As String
Dim destinationCode As String
Dim weight As String
Dim filePath As String
Dim wb As Workbook
Dim ws As Worksheet
’ Set your kintone subdomain, API token and App ID
Const subDomain As String = “ドメイン”
Const apiToken As String = “トークン”
Const appId As String = “ID”
’ Set kintone API URL
url = “https://” & subDomain & “.cybozu.com/k/v1/records.json”
’ Open the file selection dialog
With Application.FileDialog(msoFileDialogFilePicker)
.Title = “Select an Excel file”
.Filters.Clear
.Filters.Add “Excel Files”, “*.xls; *.xlsx; *.xlsm”
If .Show = -1 Then
filePath = .SelectedItems(1)
Else
MsgBox “No file was selected. Exiting…”
Exit Sub
End If
End With
’ Open the selected Excel file
Set wb = Workbooks.Open(filePath)
Set ws = wb.Worksheets(1)
’ Find the last row
lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
’ Loop through each row of data
For i = 2 To lastRow
’ Read data from the worksheet
deliveryDate = Format(ws.Cells(i, 1).Value, “yyyy-MM-dd”)
destinationCode = ws.Cells(i, 2).Value
weight = ws.Cells(i, 3).Value
’ Create the JSON payload
payload = “{”“app”“:”“” & appId & “”“,”“record”“:{”“納品日”“:{”“value”“:”“” & deliveryDate & “”“},”“配送先番号”“:{”“value”“:”“” & destinationCode & “”“},”“重量”“:{”“value”“:”“” & weight & “”“}}}”
’ Set up the HTTP request
With xmlHttpRequest
.Open “POST”, url, False
.setRequestHeader “Content-Type”, “application/json”
.setRequestHeader “X-Cybozu-API-Token”, apiToken
.send payload
’ Check if the API request was successful
If .Status <> 200 Then
MsgBox "Error: " & .Status & " " & .statusText & vbCrLf & .responseText
End If
End With
Next i
’ Close the workbook
wb.Close SaveChanges:=False
MsgBox “Data has been imported to kintone successfully.”
End Sub
| 納品日 | 配送先番号 | 重量 |
| 2023/4/22 | あんず | 500 |
| 2023/4/22 | ポン酢 | 400 |
上記の形式のエクセルファイルを自動でkintoneにインポートしたいと考えております。
配送先番号のフィールドはルックアップフィールです。
このコードを実行すると、
Error: 400 Bad Request
fcode” "CB_VAOT "id"jo2qdZ1edmuZAsIxpbs7,"message”"入力内容が正しくありません。”“errors” 'records’messages”:[必須です。“
(OK)をクリック
Error: 400 Bad Request
fcode””“CB_VAOT”,id " ”RUM8BMYTiVZPJStYEAlP”,"message””入力内容が正しくありません。,「errors”:「records”f’messages”[必須です。"119
(OK)をクリック
Data has been imported to kintone successfully.
と最後に表示されます。
しかし、kintoneの方へはデータが入っていません。
私はVBAもプログラミングもわかりません。
ChatGPTで作ったコードです。
GPTに確認してもエラーは解消されません。
原因がわかる方おられましたら教えていただきたいです。
よろしくお願いします。