前提
GoogleAppsScriptを使用して某サービスとAPI連携し、GoogleスプレッドシートにAPIで取得した値を出力するアプリを作っています。
こちらでご教授いただいたコードを用いて、色々試行しております。
実現したいこと
取得したJSON配列から、、
- project_id
- project_name
- custom内のdef_id":1のdef_value
- custom内のdef_id":5のdef_value
を取得し、スプレッドシートのセルに出力したいです。
"data": [
{
"project_id": 1,
"project_name": "aaaaaaa",
"custom": [
{
"def_id": 1,
"def_value": "XXXXXX"
},
{
"def_id": 5,
"def_value": "ZZZZZZZ"
},
]
}
]
発生している問題・エラーメッセージ
ソースコードではproject_id、project_name、custom内のdef_id": 1のdef_valueの値しか出力されません。
該当のソースコード
GoogleAppsScript
1{ 2 const sheet_id = 'xxx'; 3 const sheet_name = 'xxx'; 4 const sheet = SpreadsheetApp.openById(sheet_id).getSheetByName(sheet_name); 5 const response = UrlFetchApp.fetch("xxx", { 6 "method": "GET", 7 "headers": { 8 "content-type": "application/json", 9 "Api-Key": "xxx" 10 }, 11 }) 12 const json = JSON.parse(response); 13 const rows = json.data.map(({ 14 project_id: A, 15 project_name: B, 16 custom: [{ def_value: C}], 17}) => [A, B, C]); 18 console.log(rows); 19 const startRow = sheet.getLastRow() + 1; 20 const rowLength = rows.length; 21 const colLength = rows[0].length; 22 var date = new Date(); 23 sheet.getRange(1, 1).setValue(date); 24 sheet.getRange(startRow, 2, rowLength, colLength).setValues(rows); 25}
試したこと
custom: [{ def_value: C}],の部分を、、
custom: [{ def_value.0: C, def_value.1: D}],
custom: [{ def_value(0): C, def_value(1): D}],
など試してみましたが、そもそもこのような書き方では取得できないのでしょうか?
ご教授いただけますと幸いです。。。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。