Created
August 30, 2024 04:30
-
-
Save sweshelo/7ad6566151aa0198e55bfd81348e04ff to your computer and use it in GitHub Desktop.
freee人事労務 一括打刻
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** FREEE CLOCK IN | |
* @sweshelo | |
* 人事労務freeeの勤怠レコードを1ヶ月分(平日のみ)まとめて入力します | |
* ブラウザの開発者ツールに貼り付けて動かしてください | |
*/ | |
/* 設定 ここから */ | |
// 企業ID: 開発者ツールで正当なリクエストのヘッダ `X-Company-Id` を参照 | |
const CompanyId = '0000000'; | |
// 従業員ID: URLに含まれている | |
const EmployeeId = '0000000'; | |
// 実際に勤務を行った月の初日 | |
const WorkMonth = new Date('2024/07/01'); | |
// 給料が支払われる月の初日 | |
const PayMonth = new Date("2024/08/01"); | |
// 勤怠レコード | |
const RecordTimestamps = { | |
work: { | |
clockInAt: "10:00:00+09:00", | |
clockOutAt: "19:00:00+09:00", | |
}, | |
break: { | |
clockInAt: "14:00:00+09:00", | |
clockOutAt: "15:00:00+09:00", | |
} | |
}; | |
/* 設定 ここまで */ | |
function formatDate(date) { | |
const year = date.getFullYear(); | |
const month = String(date.getMonth() + 1).padStart(2, "0"); | |
const day = String(date.getDate()).padStart(2, "0"); | |
return `${year}-${month}-${day}`; | |
} | |
function putWorkRecord(workDate) { | |
const formattedDate = formatDate(workDate); | |
fetch( | |
`https://p.secure.freee.co.jp/api/p/employees/work_records/${EmployeeId}/${PayMonth.getFullYear()}/${PayMonth.getMonth()}/${formattedDate}`, | |
{ | |
method: "PATCH", | |
credentials: "include", | |
headers: { | |
"content-type": "application/json", | |
"x-csrf-token": document.cookie | |
.split(";") | |
.find((e) => e.startsWith("XSRF-TOKEN")) | |
.split("=")[1], | |
"X-Company-Id": CompanyId, | |
}, | |
body: JSON.stringify({ | |
break_records: [ | |
{ | |
clock_in_at: `${formattedDate}T${RecordTimestamps.break.clockInAt}`, | |
clock_out_at: `${formattedDate}T${RecordTimestamps.break.clockOutAt}`, | |
mins: 0, | |
}, | |
], | |
employee_work_record_segments: [ | |
{ | |
clock_in_at: `${formattedDate}T${RecordTimestamps.work.clockInAt}`, | |
clock_out_at: `${formattedDate}T${RecordTimestamps.work.clockOutAt}`, | |
mins: 0, | |
}, | |
], | |
work_type: "normal", | |
}), | |
} | |
); | |
} | |
function getWeekdaysInMonth(startDate) { | |
const weekdays = []; | |
const currentDate = new Date(startDate); | |
const month = currentDate.getMonth(); | |
while (currentDate.getMonth() === month) { | |
const dayOfWeek = currentDate.getDay(); | |
if (dayOfWeek !== 0 && dayOfWeek !== 6) { | |
weekdays.push(new Date(currentDate)); | |
} | |
currentDate.setDate(currentDate.getDate() + 1); | |
} | |
return weekdays; | |
} | |
(() => { | |
getWeekdaysInMonth(WorkMonth).forEach(putWorkRecord) | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment