From efcb8aea3e6fecd8f9d6d2170c7bedfb4fa5d178 Mon Sep 17 00:00:00 2001 From: freerer2 Date: Tue, 25 Feb 2025 14:12:05 +0900 Subject: [PATCH] Add weekbogo.ps1 --- weekbogo.ps1 | 213 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 213 insertions(+) create mode 100644 weekbogo.ps1 diff --git a/weekbogo.ps1 b/weekbogo.ps1 new file mode 100644 index 0000000..2714e98 --- /dev/null +++ b/weekbogo.ps1 @@ -0,0 +1,213 @@ +# 실행시 권한 및 파일 서명관련 문제 생기면 실행 정책 변경(수동실행) +# Set-ExecutionPolicy Unrestricted + +# 필요한 어셈블리 로드 +Add-Type -AssemblyName System.Drawing + +# 날짜 가져오기 +$today = Get-Date + +# 이번 주 토요일 +$this_week_saturday = $today.AddDays((6 - $today.DayOfWeek.value__) % 7) + +# 이번 주 월요일 +$this_week_monday = $this_week_saturday.AddDays(-5) + +# 저번 주 토요일 +$last_week_saturday = $this_week_saturday.AddDays(-7) + +# 파일 경로 및 시트명 +$file_path = 'C:\Users\LHK\OneDrive - ks.ac.kr\주간보고\' +$file_writer = '이호경' +$file_prefix = '_주간보고_' +$file_ext = '.xlsx' + +$this_week_file_path = Join-Path $file_path ($file_writer + $file_prefix + $this_week_saturday.ToString("yyyyMMdd") + $file_ext) +$last_week_file_path = Join-Path $file_path ($file_writer + $file_prefix + $last_week_saturday.ToString("yyyyMMdd") + $file_ext) + +$plan_sheet_name = $file_writer +$commute_sheet_name = $file_writer + '_출퇴근' + +# 달력 현재 1달전 2달전 +$calendar_this_month = 'B1:E33' +$calendar_month_1_ago = 'G1:J33' +$calendar_month_2_ago = 'L1:O33' + +# 저번달 달력 이동 거리(1당 1셀) +$calendar_move_right = 5 +$calendar_move_bottom = 0 + +# 1일이 시작 되는 row +$calendar_start_row = 3 + +# 셀 채우기 색상 +$white = 16777215 # RGB(255, 255, 255) +$grey = 15132390 # RGB(230, 230, 230) + +# 금주, 차주계획 내용범위 +$this_week_plan = 'B7:B18' +$progress = 'C7:C18' +$next_week_plan = 'E7:E18' + +function Cell-Move-Range { + param ( + $worksheet, + $cell_range, + $rows, + $cols + ) + + $range = $worksheet.Range($cell_range) + $range.Copy() | Out-Null + $destination = $worksheet.Cells($range.Row + $rows, $range.Column + $cols) + $worksheet.Paste($destination) +} + +function Update-Calendar { + $excel = $null + try { + $excel = New-Object -ComObject Excel.Application + $excel.Visible = $false + $workbook = $excel.Workbooks.Open($this_week_file_path) + $worksheet = $workbook.Worksheets.Item($commute_sheet_name) + + $this_month = ' ' + $this_week_saturday.Year + '/' + $this_week_saturday.Month + + # 월 변경시 기본 달력 처리 + $month = $worksheet.Range($calendar_this_month).Cells(1,1).Text + if ($month -ne $this_month) { + # 전월 달력 이동 + Cell-Move-Range $worksheet $calendar_month_1_ago $calendar_move_bottom $calendar_move_right + Cell-Move-Range $worksheet $calendar_this_month $calendar_move_bottom $calendar_move_right + $worksheet.Range($calendar_this_month).Cells(1,1).Value = $this_month + + # 당월 날짜 채움 + $first_day = Get-Date -Year $this_week_saturday.Year -Month $this_week_saturday.Month -Day 1 + $last_day = $first_day.AddMonths(1).AddDays(-1) + + $insert_range = $worksheet.Range($calendar_this_month) + for ($row = $calendar_start_row; $row -le $insert_range.Rows.Count; $row++) { + for ($col = 1; $col -le $insert_range.Columns.Count; $col++) { + $cell = $insert_range.Cells($row, $col) + $cell.Value = $null + $cell.Interior.Color = $white + } + } + + for ($i = 0; $i -lt $last_day.Day; $i++) { + $insert_day = $first_day.AddDays($i) + + $insert_row = $insert_range.Rows($i + $calendar_start_row) + $insert_row.Cells(1, 1).Value = $insert_day.ToString("yyyy-MM-dd") + if ($insert_day.DayOfWeek -eq 'Sunday') { + $insert_row.Interior.Color = $grey + } else { + $insert_row.Interior.Color = $white + } + } + } + + $workbook.Save() + } + catch { + Write-Host "Update-Calendar 함수에서 오류 발생: $_" + } + finally { + if ($workbook) { $workbook.Close() } + if ($excel) { + $excel.Quit() + [System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel) | Out-Null + } + } +} + +function Ready-To-Write { + $excel = $null + try { + $excel = New-Object -ComObject Excel.Application + $excel.Visible = $false + $workbook = $excel.Workbooks.Open($this_week_file_path) + $worksheet = $workbook.Worksheets.Item($plan_sheet_name) + + $worksheet.Range("B4").Value2 = "업무기간 : $($this_week_monday.ToString('yyyy.MM.dd')) ~ $($this_week_saturday.ToString('yyyy.MM.dd'))" + $worksheet.Range("C4").Value2 = "작 성 일 : $($this_week_saturday.ToString('yyyy.MM.dd'))" + + $value_arr = @() + $next_week_range = $worksheet.Range($next_week_plan) + foreach ($cell in $next_week_range.Cells) { + if ($cell.Value2 -ne $null) { + $value_arr += $cell.Value2 + } + $cell.Value2 = $null + } + + $progress_range = $worksheet.Range($progress) + foreach ($cell in $progress_range.Cells) { + $cell.Value2 = $null + } + + if ($value_arr.Count -gt 0) { + $this_week_range = $worksheet.Range($this_week_plan) + $i = 0 + foreach ($cell in $this_week_range.Cells) { + if ($i -lt $value_arr.Count) { + if ($value_arr[$i] -ne $null) { + $value = $value_arr[$i] + Write-Host "Value to assign: $value" + + # 값을 문자열로 명시적 변환 + $stringValue = [string]$value + Write-Host "String value to assign: $stringValue" + + # 값 할당 + $cell.Value2 = $stringValue + + # 할당 직후 값 확인 + $assignedValue = $cell.Value2 + Write-Host "Assigned value in cell: $assignedValue" + + if ($assignedValue -eq $null -or $assignedValue -eq '') { + Write-Host "Assignment failed, trying alternative method" + $worksheet.Cells.Item($cell.Row, $cell.Column) = $stringValue + $assignedValue = $worksheet.Cells.Item($cell.Row, $cell.Column).Value2 + Write-Host "Value after alternative assignment: $assignedValue" + } + + $i++ + } + } else { + $cell.Value2 = $null # 남은 셀을 비웁니다 + } + } + } + + $workbook.Save() + } + catch { + Write-Host "Ready-To-Write 함수에서 오류 발생: $_" + } + finally { + if ($workbook) { $workbook.Close() } + if ($excel) { + $excel.Quit() + [System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel) | Out-Null + } + } +} + +# Main script +try { + if (Test-Path $this_week_file_path) { + Remove-Item $this_week_file_path -Force + } + + Copy-Item $last_week_file_path $this_week_file_path + + Update-Calendar + Ready-To-Write + + Write-Host "작업이 완료되었습니다." +} +catch { + Write-Host "메인 스크립트에서 오류 발생: $_" +} \ No newline at end of file