function listEmployee() { //SmartHRのAPIキー(機密情報なのでプロパティで事前設定しておく) const TOKEN = PropertiesService.getScriptProperties().getProperty("SMARTHR_APIKEY"); //組織名(SmartHRで登録されているサブドメイン名) const ORG = PropertiesService.getScriptProperties().getProperty("ORG"); //一覧に表示するドメイン名(Gmailなどの個人アドレスを表示させないため) const DOMAIN = PropertiesService.getScriptProperties().getProperty("DOMAIN"); //シート名 const SHEET = PropertiesService.getScriptProperties().getProperty("SHEET"); //何ページ分取得するか(登録されている社員数に依存する) const LAST_PAGE = 7; //1ページ最大100件まで const PER_PAGE = 100; //入社後何日後に一覧表示するか(入力未完で表示させないために) const DATE_GAP = 1; // 現在アクティブなスプレッドシートを取得 const ss = SpreadsheetApp.openById(SpreadsheetApp.getActiveSpreadsheet().getId()); // シート名の指定 const sheet = ss.getSheetByName(SHEET); //既存データの最終行の取得 const lastRowNum = sheet.getLastRow(); //空でも消せるように1件追加 sheet.appendRow([""]); //スプレッドシートに記載されている既存データの全件行削除 sheet.deleteRows(1, lastRowNum); //1行目のヘッダ挿入 sheet.appendRow(["社員番号","姓","名","セイ","メイ","入社日","Eメール","部署1","部署2","部署3","役職","業務"]); //読み込んだ社員情報全件をフィルタや加工して一覧に追記する。 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){ let last_name = json[i]["last_name"];; let first_name = json[i]["first_name"]; let last_name_yomi = json[i]["last_name_yomi"]; let first_name_yomi = json[i]["first_name_yomi"]; const emp_code = json[i]["emp_code"]; const business_last_name = json[i]["business_last_name"]; const position = json[i]["position"]; const occupation = json[i]["occupation"]; const entered_at = json[i]["entered_at"]; const resigned_at = json[i]["resigned_at"]; const department = json[i]["department"]; //ビジネスネームがあればそれを優先する if(business_last_name != ""){ last_name = json[i]["business_last_name"]; first_name = json[i]["business_first_name"]; last_name_yomi = json[i]["business_last_name_yomi"]; first_name_yomi = json[i]["business_first_name_yomi"]; } //部署名を分割して配列にする let departmentList = null; departmentList = getDepartmentList(department); //会社ドメインのメールのときのみ表示する。 let email = json[i]["email"]; if(!email.includes("@"+DOMAIN)){ email = null; } //社員コードが発行されていないときは-にする。 if(emp_code == null){ emp_code = "-"; } //入社直後は必要情報が十分に記入されていない(ビジネスネームなど)ことが多いため、本日+バッファ日数分を過ぎた人のみを表示する。 const date0 = new Date(entered_at+"T00:00:00.000"); const today = new Date(); //入社済みかつ指定日数過ぎていたら表示する if(isBefore(date0,today,DATE_GAP)){ if(resigned_at == null){ //各種条件を満たした社員情報が一覧に追加される sheet.appendRow([emp_code,last_name,first_name,last_name_yomi,first_name_yomi, entered_at,email,departmentList[0],departmentList[1],departmentList[2],position,occupation]); } } } } } //日付のカラムで昇順ソートする const lastRow = sheet.getLastRow(); const lastCol = sheet.getLastColumn(); sheet.appendRow([""]); const data = sheet.getRange(2,1,lastRow,lastCol); data.sort({column: 1, ascending: true}); //一覧作成の履歴をシートに記入する const updateSheet = ss.getSheetByName("更新履歴"); const date = Utilities.formatDate(new Date(), 'Asia/Tokyo', 'yyyy-MM-dd HH:mm:ss'); updateSheet.appendRow([date]); //日付の比較 //a+c<b a+cとbの日付を比較してbのほうが大きいときにtrueを返す function isBefore(a,b,c){ a.setDate(a.getDate()+c); const a1 = a.getTime(); const b1 = b.getTime(); if(a1 < b1){ return true; }else{ return false; } } //部署名をスラッシュやスペース区切りで分割する(SmartHRでどのような部署文字列で登録をするかに依存する) function getDepartmentList(department){ let departmentList = ["","",""]; if(department!=null){ departmentList = department.split("/"); if(departmentList!=null){ if(departmentList.length>1){ if(departmentList[0]==null)departmentList[0]=""; if(departmentList[1]==null)departmentList[1]=""; if(departmentList[2]==null)departmentList[2]=""; }else{ departmentList = department.split(" "); if(departmentList!=null){ if(departmentList[0]==null)departmentList[0]=""; if(departmentList[1]!=null)departmentList[1]=department.substring(departmentList[0].length+1,department.length); } } } } return departmentList; } }