SmartHR API と Google Apps Script で住所情報を Google シートへ同期する

SmartHR API と Google Apps Script で住所情報を Google シートへ同期する方法をご紹介します。

背景 SmartHR の住所情報だけ共有したい

SmartHR の管理者権限を付与できない方 (例えば、総務の職種) に、従業員の住所情報のみを共有したいという課題があり、SmartHR API と Google Apps Script で住所情報を Google シートへ同期することで解決しました。

サンプルコード SmartHR API と Google Apps Script で住所情報を Google シートへ同期

SmartHR APIとGoogle Apps Scriptを使って社内向け社員一覧の取得を自動化する | DevelopersIO を参考にして、作成したのが以下のスクリプトです。

function listEmployee() {
    //SmartHRのAPIキー(機密情報なのでプロパティで事前設定しておく)
    const TOKEN = PropertiesService.getScriptProperties().getProperty("SMARTHR_APIKEY");
    //組織名(SmartHRで登録されているサブドメイン名)
    const ORG = PropertiesService.getScriptProperties().getProperty("ORG");
    //何ページ分取得するか(登録されている社員数に依存する)
    const LAST_PAGE = 1;
    //1ページ最大100件まで
    const PER_PAGE = 100;

    // 現在アクティブなスプレッドシートを取得
    const spreadsheet = SpreadsheetApp.openById(SpreadsheetApp.getActiveSpreadsheet().getId());
    // シート名の指定
    const sheet = spreadsheet.getActiveSheet();;
    //既存データの最終行の取得
    const lastRowNum = sheet.getLastRow();
    //空でも消せるように1件追加
//    sheet.appendRow([""]);
    //スプレッドシートに記載されている既存データの全件行削除
//    sheet.deleteRows(1, lastRowNum);

    //1行目のヘッダ挿入
    sheet.appendRow([
        "社員番号",
        "姓",
        "名",
        "セイ",
        "メイ",
        "姓(ビジネスネーム)",
        "名(ビジネスネーム)",
        "セイ(ビジネスネーム)",
        "メイ(ビジネスネーム)",
        "郵便番号",
        "都道府県",
        "市区町村",
        "番地",
        "建物名・部屋番号",
        "電話番号"
    ]);

    //読み込んだ社員情報全件をフィルタや加工して一覧に追記する。
    for (var page = 1; page <= LAST_PAGE; page++) {
        const endpointUrl = "https://" + ORG + ".smarthr.jp/api/v1/crews?page=" + page + "&per_page=" + PER_PAGE + "&access_token=" + TOKEN;
        const response = UrlFetchApp.fetch(endpointUrl);
        const json = JSON.parse(response.getContentText());

        for (var i = 0; i < PER_PAGE; i++) {
            //Logger.log(json[i]);
            if (json[i] != null) {
                const last_name = json[i]["last_name"];;
                const first_name = json[i]["first_name"];
                const last_name_yomi = json[i]["last_name_yomi"];
                const first_name_yomi = json[i]["first_name_yomi"];
                const business_last_name = json[i]["business_last_name"];
                const business_first_name = json[i]["business_first_name"];
                const business_last_name_yomi = json[i]["business_last_name_yomi"];
                const business_first_name_yomi = json[i]["business_first_name_yomi"];
                const emp_code = json[i]["emp_code"];
                const address = json[i]["address"];
                const tel_number = json[i]["tel_number"];


                //各種条件を満たした社員情報が一覧に追加される
                sheet.appendRow([
                    emp_code,
                    last_name,
                    first_name,
                    last_name_yomi,
                    first_name_yomi,
                    business_last_name,
                    business_first_name,
                    business_last_name_yomi,
                    business_first_name_yomi,
                    address && address.zip_code,
                    address && address.pref,
                    address && address.city,
                    address && address.street,
                    address && address.building,
                    tel_number
                ]);
            }
        }
    }
}

以上、SmartHR API と Google Apps Script で住所情報を Google シートへ同期させた、現場からお送りしました。