Skip to content

Instantly share code, notes, and snippets.

@ten9miq
Last active September 12, 2024 09:37
Show Gist options
  • Save ten9miq/5af25a487b1aeb4035c8e8898f4ba6fd to your computer and use it in GitHub Desktop.
Save ten9miq/5af25a487b1aeb4035c8e8898f4ba6fd to your computer and use it in GitHub Desktop.
param (
[string]$inputFile
)
# 入力ファイルの存在を確認
if (-Not (Test-Path $inputFile)) {
Write-Host "Error: ファイル $inputFile が見つかりません。" -ForegroundColor Red
exit 1
}
# 出力ファイル名を作成
$outputFile = Join-Path -Path (Split-Path $inputFile -Parent) -ChildPath ("output_" + (Split-Path $inputFile -Leaf))
# CSVファイルの読み込み
$csvData = Import-Csv -Path $inputFile
# 結果を格納するリスト
$result = @()
# 各行を処理
foreach ($row in $csvData) {
$id_value = $row.ID
$other_info = $row.'他の情報'
# 作業時間列を処理
foreach ($col in $csvData | Get-Member -MemberType Properties | Where-Object { $_.Name -like '作業時間*' }) {
$time_data = $row.$($col.Name)
if ($time_data) {
# 作業時間の内容を分割
$split_data = $time_data -split ';'
$comment = $split_data[0]
$start_time = $split_data[1]
$user_account = $split_data[2]
$elapsed_time = $split_data[3]
# 新しいレコードを作成
$newRow = [PSCustomObject]@{
'ID' = $id_value
'コメント' = $comment
'開始日時' = $start_time
'ユーザーアカウント' = $user_account
'経過秒数' = $elapsed_time
'他の情報' = $other_info
}
# 結果に追加
$result += $newRow
}
}
}
# 新しいデータをCSVとして出力
$result | Export-Csv -Path $outputFile -NoTypeInformation
Write-Host "処理が完了しました。出力ファイル: $outputFile" -ForegroundColor Green
param (
[string]$inputFile
)
# 入力ファイルの存在を確認
if (-Not (Test-Path $inputFile)) {
Write-Host "Error: ファイル $inputFile が見つかりません。" -ForegroundColor Red
exit 1
}
# 出力ファイル名を作成
$outputFile = Join-Path -Path (Split-Path $inputFile -Parent) -ChildPath ("output_" + (Split-Path $inputFile -Leaf))
# CSVファイルの読み込み
$csvData = Import-Csv -Path $inputFile
# 結果を格納するリスト
$result = @()
# 各行を処理
foreach ($row in $csvData) {
$id_value = $row.ID
$other_info = $row.'他の情報'
# 作業時間列を処理
foreach ($col in $csvData.PSObject.Properties.Name | Where-Object { $_ -like '作業時間*' }) {
$time_data = $row.$col
if ($time_data) {
# 作業時間の内容を分割
$split_data = $time_data -split ';'
$comment = $split_data[0]
$start_time = $split_data[1]
$user_account = $split_data[2]
$elapsed_time = $split_data[3]
# 新しいレコードを作成
$newRow = [PSCustomObject]@{
'ID' = $id_value
'コメント' = $comment
'開始日時' = $start_time
'ユーザーアカウント' = $user_account
'経過秒数' = $elapsed_time
'他の情報' = $other_info
}
# 結果に追加
$result += $newRow
}
}
}
# 新しいデータをCSVとして出力
$result | Export-Csv -Path $outputFile -NoTypeInformation
Write-Host "処理が完了しました。出力ファイル: $outputFile" -ForegroundColor Green
param (
[string]$inputFile
)
# 入力ファイルの存在を確認
if (-Not (Test-Path $inputFile)) {
Write-Host "Error: ファイル $inputFile が見つかりません。" -ForegroundColor Red
exit 1
}
# 入力ファイルのフルパスを取得
$inputFileFullPath = Resolve-Path $inputFile
Write-Host "Full input file path: $inputFileFullPath" -ForegroundColor Yellow
# 出力ファイル名を作成
$outputFile = Join-Path -Path (Split-Path $inputFileFullPath -Parent) -ChildPath ("output_" + (Split-Path $inputFileFullPath -Leaf))
Write-Host "Output file path: $outputFile" -ForegroundColor Yellow
# CSVファイルの読み込み
$csvData = Import-Csv -Path $inputFileFullPath
Write-Host "CSV data loaded. Total rows: $($csvData.Count)" -ForegroundColor Yellow
# 全列名の確認
Write-Host "All columns:" -ForegroundColor Yellow
$columns = $csvData[0].PSObject.Properties.Name
foreach ($col in $columns) {
Write-Host "Column name: '$col'" -ForegroundColor Cyan
}
# 列名のフィルタリングと確認
Write-Host "Checking column names that match '作業時間*' after trimming..." -ForegroundColor Yellow
foreach ($col in $columns | Where-Object { $_.Trim() -like '作業時間*' }) {
Write-Host "Column found: '$col'" -ForegroundColor Green
}
# 結果を格納するリスト
$result = @()
# 各行を処理
foreach ($row in $csvData) {
Write-Host "Current Row: $($row | Format-Table | Out-String)" -ForegroundColor Yellow
$id_value = $row.ID
$other_info = $row.'他の情報'
Write-Host "Processing ID: $id_value" -ForegroundColor Yellow
# 作業時間列を処理
foreach ($col in $columns | Where-Object { $_.Trim() -like '作業時間*' }) {
$time_data = $row.$($col)
Write-Host "Column being processed: '$($col)'" -ForegroundColor Blue
if ($null -ne $time_data -and $time_data.Trim() -ne "") {
Write-Host "Data in '$($col)': $time_data" -ForegroundColor Cyan
# 作業時間の内容を分割
$split_data = $time_data -split ';'
Write-Host "Split data: $($split_data -join ', ')" -ForegroundColor Green
Write-Host "Split data count: $($split_data.Count)" -ForegroundColor Green
if ($split_data.Count -eq 4) {
$comment = $split_data[0]
$start_time = $split_data[1]
$user_account = $split_data[2]
$elapsed_time = $split_data[3]
# 新しいレコードを作成
$newRow = [PSCustomObject]@{
'ID' = $id_value
'コメント' = $comment
'開始日時' = $start_time
'ユーザーアカウント' = $user_account
'経過秒数' = $elapsed_time
'他の情報' = $other_info
}
# 結果に追加
$result += $newRow
Write-Host "Added new row to result: $($newRow | Out-String)" -ForegroundColor Green
} else {
Write-Host "Warning: 作業時間列のデータが正しく分割されませんでした。Split data count: $($split_data.Count)" -ForegroundColor Red
}
} else {
Write-Host "No valid data found in column: '$($col)'" -ForegroundColor Magenta
}
}
}
# 新しいデータをCSVとして出力
if ($result.Count -gt 0) {
$csvContent = $result | ConvertTo-Csv -NoTypeInformation | Out-String
[System.IO.File]::WriteAllText($outputFile, $csvContent, [System.Text.Encoding]::GetEncoding("Shift_JIS"))
Write-Host "処理が完了しました。出力ファイル: $outputFile" -ForegroundColor Green
} else {
Write-Host "Error: 出力データがありませんでした。" -ForegroundColor Red
}
@ten9miq
Copy link
Author

ten9miq commented Sep 10, 2024

1, 3, 4, 7, 8, 11, 12
1, 2, 4, 6, 9
1, 2, 3, 4, 7, 11
1, 2, 3, 5, 6, 7, 9, 10, 11, 12
2, 3, 8
3, 6, 10
1, 2, 5, 8, 10, 11
2, 3, 5, 6, 7, 11
2, 4, 5, 6, 8, 9, 10, 11, 12
2, 6, 8, 10, 12

@ten9miq
Copy link
Author

ten9miq commented Sep 10, 2024

木曜日, 火曜日, 月曜日, 金曜日, 水曜日, 土曜日, 日曜日
月曜日, 火曜日
月曜日, 金曜日, 日曜日, 火曜日, 土曜日
水曜日, 火曜日, 日曜日, 月曜日, 金曜日, 土曜日, 木曜日
月曜日
月曜日, 木曜日, 水曜日, 金曜日, 日曜日
火曜日, 木曜日, 日曜日, 土曜日
日曜日
日曜日
火曜日, 土曜日, 月曜日, 水曜日, 金曜日, 日曜日

@ten9miq
Copy link
Author

ten9miq commented Sep 12, 2024

$weekday_map = [
    '月曜' => 'Monday',
    '火曜' => 'Tuesday',
    '水曜' => 'Wednesday',
    '木曜' => 'Thursday',
    '金曜' => 'Friday',
    '土曜' => 'Saturday',
    '日曜' => 'Sunday',
];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment