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

More than 5 years have passed since last update.

kintone のドロップダウンフィールドの項目をCSVとして取り出す方法

Last updated at Posted at 2020-01-24

ドロップダウンの項目がだんだんと増えてきて、いっそのことマスタアプリにした方がいいんじゃない?と感じた時に試してみましょう。

方法

  • フォーム情報取得系の kintone REST API を叩いて JSON で保存します。
  • 保存したJSONを jq コマンドで成形して、ファイルに出力します。
  • エクセルで読み込んでkintoneに読み込むレコードに成形します。
  • アプリを作成してCSV読み込みします。

手順

やってみます。

form.json を使う場合

フォーム設計情報取得APIを使う例です。

アプリの詳細画面を開いて form.json API を実行

  1. アプリの詳細画面を開きます。
  2. ChromeのDevToolsを開きます。
  3. Consoleにform.json API を入力してEnterします。
  4. 結果のJSONをコピーしてファイルに保存します。

3.の実行例

var body = {
    "app": kintone.app.getId()
};
kintone.api(kintone.api.url('/k/v1/form', true), 'GET', body, function(resp) {
    // success
    console.log(JSON.stringify(resp, null, 2));
}, function(error) {
    // error
    console.log(error);
});

JSONを jqコマンドで成形

  1. OSのコマンドラインで JSONファイルを読み込みます。
  2. jqコマンドで必要なフィールドに絞り込みます。
  3. さらにドロップダウンの"options"だけを取り出します。
$ cat form.json | jq '.["properties"] | map(select(.["label"] == "取り出したいフィールド名")) | .[].options | .[]'
"sample1"
"sample2"

jqコマンド cheat sheet

フォームのラベルの取り出し
cat form.json | jq '.["properties"] | .[].label'

上と同じことをパイプで
cat form.json | jq '.["properties"] | .[] | .label'

配列に入れて
cat form.json | jq '.["properties"] | .[] | [.type, .label]'

CSVにするとか
cat form.json | jq '.["properties"] | .[] | [.type, .label] | @csv'

-r つけて
cat form.json | jq -r '.["properties"] | .[] | [.type, .label] | @csv'

nkf で文字コードを変換
cat form.json | jq -r '.["properties"] | .[] | [.type, .label] | @csv' | nkf -Ws

ドロップダウンフィールドだけ抽出するとか
cat form.json | jq -r '.["properties"] | map(select(.["type"] == "DROP_DOWN")) | .[] | [ .label, .options ]

labelとoptionsを並べて
cat form.json | jq -r '.["properties"] | map(select(.["type"] == "DROP_DOWN")) | .[] | [ .label, .options[] ]'

csvで出力する
cat form-anken.json | jq -r '.["properties"] | map(select(.["type"] == "DROP_DOWN")) | .[] | [ .label, .options[] ] | @csv'


form/fields.json を使う場合

フォームの設定の取得APIを使う例です。

アプリの詳細画面を開いて form/fields.json API を実行

API実行例

var body = {
    "app": kintone.app.getId()
};
kintone.api(kintone.api.url('/k/v1/app/form/fields', true), 'GET', body, function(resp) {
    // success
    console.log(JSON.stringify(resp, null, 2));
}, function(error) {
    // error
    console.log(error);
});

JSONを jqコマンドで成形

$ $ cat form-fields.json | jq '[.["properties"]["<フィールドコード>"]["options"][]] | sort_by(.index) | .[]["label"]'

indexのソートが上手くいってないですが、ひとまずこちらで取り出すことはできました。

参考

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