LoginSignup
1
1

More than 3 years have passed since last update.

kintoneで都道府県フィールドを作る方法

Last updated at Posted at 2020-04-10

はじめに

kintoneで都道府県をドロップダウンで作るのはダルい大変ですよね。
私もそういう地道な作業は大嫌いです。

もちろん別アプリで都道府県マスタ作ってルックアップする方法もありますが、たかが都道府県にルックアップというのは私的にはあまり好きではありません。

かといって、そのためだけにプラグイン使うのもどうかと思います。
他のカスタマイズと競合してもイヤですし、アプリに入れるプラグインは少ないに越したことはありません。

という訳で、特定アプリのドロップダウンフィールドの選択候補として47都道府県を設定する方法をメモしておくことにします。

必要なもの

  • Node.js
  • @kintone/rest-api-client

これらのインストールの仕方が分からない人は・・・、適当にググってください。
そんなの余裕という人は次のセクションへどうぞ。

未来のバカになった私のためどうしてもやり方が分からない人のために、ここもメモに残します。

Node.jsは問題ないでしょう。
インストーラーからインストールするだけです。

@kintone/rest-api-clientは、手っ取り早くインストールするなら以下のコマンドですかね。

npm install -g @kintone/rest-api-client

見ての通りグローバルインストールしちゃってますが、これが気になる人はここを読んでいないと思うのでキニシナイ。

コード

事前にアプリとフィールドを作っておいてください。

設定に必要な項目はコードの冒頭に集めているので適宜変更してください。

prefectures.js
(async function() {
    "use strict";

    const username = "xxxxxx";                  // cybozu.comのユーザー名
    const password = "xxxxx";                   // cybozu.comのパスワード
    const subdomain = "xxxxxx";                 // cybozu.comのサブドメイン
    const useProxy = false;                     // プロキシサーバーを使うかどうか
    const host = "proxy.example.com";           // プロキシサーバーのホスト名
    const port = "8080";                        // プロキシサーバーのポート番号
    const app = 0;                              // 対象アプリのID
    const fieldCode = "都道府県";                // 対象フィールドのフィールドコード

    const properties = {
        [fieldCode]: {
            type: "DROP_DOWN",
            code: fieldCode,
            options: {
                "北海道": {label: "北海道", index: "0"},
                "青森県": {label: "青森県", index: "1"},
                "岩手県": {label: "岩手県", index: "2"},
                "宮城県": {label: "宮城県", index: "3"},
                "秋田県": {label: "秋田県", index: "4"},
                "山形県": {label: "山形県", index: "5"},
                "福島県": {label: "福島県", index: "6"},
                "茨城県": {label: "茨城県", index: "7"},
                "栃木県": {label: "栃木県", index: "8"},
                "群馬県": {label: "群馬県", index: "9"},
                "埼玉県": {label: "埼玉県", index: "10"},
                "千葉県": {label: "千葉県", index: "11"},
                "東京都": {label: "東京都", index: "12"},
                "神奈川県": {label: "神奈川県", index: "13"},
                "新潟県": {label: "新潟県", index: "14"},
                "富山県": {label: "富山県", index: "15"},
                "石川県": {label: "石川県", index: "16"},
                "福井県": {label: "福井県", index: "17"},
                "山梨県": {label: "山梨県", index: "18"},
                "長野県": {label: "長野県", index: "19"},
                "岐阜県": {label: "岐阜県", index: "20"},
                "静岡県": {label: "静岡県", index: "21"},
                "愛知県": {label: "愛知県", index: "22"},
                "三重県": {label: "三重県", index: "23"},
                "滋賀県": {label: "滋賀県", index: "24"},
                "京都府": {label: "京都府", index: "25"},
                "大阪府": {label: "大阪府", index: "26"},
                "兵庫県": {label: "兵庫県", index: "27"},
                "奈良県": {label: "奈良県", index: "28"},
                "和歌山県": {label: "和歌山県", index: "29"},
                "鳥取県": {label: "鳥取県", index: "30"},
                "島根県": {label: "島根県", index: "31"},
                "岡山県": {label: "岡山県", index: "32"},
                "広島県": {label: "広島県", index: "33"},
                "山口県": {label: "山口県", index: "34"},
                "徳島県": {label: "徳島県", index: "35"},
                "香川県": {label: "香川県", index: "36"},
                "愛媛県": {label: "愛媛県", index: "37"},
                "高知県": {label: "高知県", index: "38"},
                "福岡県": {label: "福岡県", index: "39"},
                "佐賀県": {label: "佐賀県", index: "40"},
                "長崎県": {label: "長崎県", index: "41"},
                "熊本県": {label: "熊本県", index: "42"},
                "大分県": {label: "大分県", index: "43"},
                "宮崎県": {label: "宮崎県", index: "44"},
                "鹿児島県": {label: "鹿児島県", index: "45"},
                "沖縄県": {label: "沖縄県", index: "46"},
            }
        }
    };

    const {KintoneRestAPIClient} = require("@kintone/rest-api-client");
    const clientParam = {
        baseUrl: "https://" + subdomain + ".cybozu.com",
        auth: {username, password}
    };
    if (useProxy) clientParam.proxy = {host, port};

    const client = new KintoneRestAPIClient(clientParam);

    const updateResponse = await client.app.updateFormFields({app, properties}).catch(err => err);
    console.log("updateResponse", updateResponse);

    if (updateResponse.revision) {
        const deployResponse = await client.app.deployApp({apps: [{app}]}).catch(err => err);
        console.log("deployResponse", deployResponse);
    }
})();

実行

さすがに未来の私はこれが分からないほどバカではないこれが分からない人はいないと思いますが、念のため。

node prefectures.js

おわりに

都道府県以外にも、選択候補をたくさん登録したい場合などに活用できると思います。

あと当然ですが、スタンダードコースでないと使えません。

1
1
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
1
1