チャネルトークとほかのツールと連携したい(スニペット・asana編) その6
どうもこんばんはtmdです。
出来上がったと思ったんですよね。
追加要望をもらいました。
ボス「チャネルトーク側からasana作るときに事前に単一選択式で用意してあるカスタムフィールドも選びたい」
私「はい」
ってことで続きです。
下準備
カスタムフィールドを書き込むときは以下のように入れろって公式のBODY PARAMSのところのDATA OBJECTのカスタムフィールドを押したところに書いてあるわけですが、そのnewKey(カスタムフィールドのgid)とNew Value(カスタムフィールドの選択肢のgid)がわからないので探します。
以下公式よりコピペです。
"data": {
"custom_fields": {
"newKey": "New Value"
}
}
ちなみにこのカスタムフィールドが選択タイプではなく値を直接入力するタイプならそこは値を直接送るだけでOKなのですが、選択式で事前に値を設定している場合はその項目のgidが必要です。
gidを探す1:カスタムフィールドのgid
カスタムフィールドの一覧を取得するのがよさそうですが、正直タスクから目視で追っても何とかなりそう。値はいずれにせよ、console.logとかで表示させてメモでいいかな。
カスタムフィールドからカスタムフィールドの一覧を取得
var response = UrlFetchApp.fetch('https://app.asana.com/api/1.0/workspaces/【ワークスペースID】/custom_fields', options);
タスクからカスタムフィールドを取得
var response = UrlFetchApp.fetch('https://app.asana.com/api/1.0/projects/【プロジェクトID】/tasks?opt_fields=custom_fields&limit=1', options);
余談カスタムフィールドの選択肢のgid
こっちはコード上でやってしまうので必要ないのですが、興味がある人は上で対象のカスタムフィールドのgidの欄にある値をこっちに入れて動かすと各項目の値が出てきます。
https://app.asana.com/api/1.0/custom_fields/【カスタムフィールドのgid】
既存のコードの修正
その1:スニペットにカスタムフィールドの値をプルダウンで出す
■カスタムフィールドの取得(doPost)
メインになるdoPost(e)の中にデータを取得するためにこんな感じの文章を入れました。
var custamFieldData = asanaCustomfieldGet(【カスタムフィールドのgid】)
■カスタムフィールドの取得(asanaCustomfieldGet)
これを入れることで、カスタムフィールドの選択肢が配列に入ります。
function asanaCustomfieldGet(customfieldId) {
var options = {
'method': 'get',
'contentType': 'application/json',
'headers': {
'Authorization': 'Bearer ' + 【アクセストークン】
},
};
var responseCustom = UrlFetchApp.fetch('https://app.asana.com/api/1.0/custom_fields/'+customfieldId, options);
var resultCustom = JSON.parse(responseCustom);
var list = [];
if(resultCustom.data){
for(var i = 0; i < resultCustom.data['enum_options'].length; i++){
if(resultCustom.data['enum_options'][i]['enabled']){
list.push(resultCustom.data['enum_options'][i]['gid']);
list.push(resultCustom.data['enum_options'][i]['name'])
};
};
};
return list;
}
■カスタムフィールドの取得(doPostのスニペット部分)
配列にはいっているカスタムフィールドの選択肢をプルダウンにします。
dropdownのvalueの値は中身がないときに届くっぽいので大事です。なお、これは挙動で判断してます。違ってたら調べてください。
var data3 = [];
count1 = 0
for (i=0;i<(custamFieldData.length)/2;i++){
data3.push({
"id": custamFieldData[count1],
"label": custamFieldData[count1+1]
});
count1 = count1+2
};
var data2 = [{
"id": "title",
"type": "text",
"text": snippetData["user"]["name"],
"style": "h1"
},
{
"type": "dropdown",
"id": "chat-mood-dropdown",
"label": "text above the option",
"value": "noData",
"disabled": false,
"items":data3
},
{
"id": "submit-button",
"type": "button",
"label": "asanaを作成",
"action": {
"type": "submit"
}
},
{
"id": "message",
"type": "text",
"text": createMessage,
"style": "h1"
}];
for (i=0;i<asanaData.length;i++){
data2.push({
"id": "asanaData"+i,
"type": "text",
"text": asanaData[i],
"style": "h3"
});
};
var data ={
"snippet": {
"version": "v0",
"layout":data2,
"params": {
"customerKey": "channelIO"
}
}
};
その2:選択した値をasanaのカスタムフィールドに書き込む
■カスタムフィールドの情報を足す(doPost)
前に書いてあったコードの引数にカスタムフィールドに関連する情報を増やしています。
create(snippetData["user"]["id"],【カスタムフィールドのgid】,snippetData["submit"]["chat-mood-dropdown"])
■カスタムフィールドの情報を足す(create)
functionの行も引数をちゃんと受け取れるように増やします。
function create(userID1,asanaCustam1,dropdown) {
■カスタムフィールドの情報がないときの対応を足す(create)
スニペットでカスタムフィールドの項目を選択したかどうかの分岐をつけます。
このとき、dropdownがnoDataなのは上のほうにある「■カスタムフィールドの取得(doPostのスニペット部分)」にてValueがnoDataだからです。
あと、カスタムフィールドのgidって書いてある部分は単純に変数を当てはめても動かず、どうやったら動くか調べる手間を惜しんだからです、直接入力したください。方法がわかり次第更新します。たぶん。
if(dropdown=="noData"){
var objTask = {
"data": {
"workspace": workspaceId,
"projects": [projectId],
"name": name,
"notes": planText
}
};
}else{
var objTask = {
"data": {
"workspace": workspaceId,
"projects": [projectId],
"name": name,
"notes": planText,
"custom_fields": {
【カスタムフィールドのgid】 : dropdown,
}
}
};
};
たぶんこんな感じで動くようになるかなって思います。
それでは、次のオーダーがきたらまた会いましょう!!
この記事が気に入ったらサポートをしてみませんか?