我不确定我是否走上了正确的道路。因此,我得到了一项任务,我有两份关于两个不同cvs文件的报告,其中包含以下信息:
一个csv在以下标题下保存付款信息:
支付.csv:
id,employee_id,payment,code
另一个使用以下标题保存员工信息:
员工.csv:
id,first_name,last_name,email,city,ip_address
此处的主键是employee.csv文件中的id
,而外键是payments.csv中的employee_id
。这意味着employee.csv文件上的id
应该与payment.csv的employee_id
匹配。
有了这些信息,我应该创建2个类。一个是employee.csv文件中的信息创建对象的employee类。另一个是使用payments.csv创建对象的支付类。
然后,我需要比较两组对象,其中employee.csv上的id
等于payments.csv中的employee_id
。然后,我想使用这些数据创建一个新的csv,将两个csv文件上的数据合并为一个文件,employe.csv上的员工将链接到他们在payments.csv上的付款。
感谢任何帮助或指导!这就是我迄今为止所做的。我可能离这里很远,所以请不要评判。只是想学习。我遇到了一个障碍,在能够创建员工和支付对象之后,我该怎么做。
#Class that creates employee object
class Employee {
[Int]$id
[String]$first_name
[String]$last_name
[String]$email
[String]$city
[String]$ip_address
Employee ([Int]$id,[String]$first_name,[String]$last_name,[String]$email,[String]$city,[String]$ip_address){
$This.id = $id
$This.first_name = $first_name
$This.last_name = $last_name
$This.email = $email
$This.city = $city
$This.ip_address = $ip_address
}
}
#Class that creates payment object
class Payment{
[Int]$id
[Int]$employee_id
[String]$payment
[String]$code
Payment ([Int]$id,[Int]$employee_id,[String]$payment,[String]$code){
$This.id = $id
$This.employee_id = $employee_id
$This.payment = $payment
$This.code = $code
}
}
#Importing spreadsheets w/ data being used
$ImportedEmployees = Import-Csv ".Employee.csv"
$ImportedPayments = Import-Csv ".Payment.csv"
$FinalEmployeeReport = @{}
#Calling [Employee] to create new objects using the employee.csv
Foreach ($Employee in $ImportedEmployees){
$NewEmployeeEntry = [Employee]::new([Int]$Employee.id,[String]$Employee.first_name,[String]$Employee.last_name,[String]$Employee.email,[String]$Employee.city,[String]$Employee.ip_address)
#Adding object to $FinalEmployeeReport
$FinalEmployeeReport.Add([String]$NewEmployeeEntry.last_name,[Int]$NewEmployeeEntry.id)
}
Foreach ($Payment in $ImportedPayments)
{
$NewPayment = [Payment]::new([Int]$Payment.id,[Int]$Payment.employee_id,[String]$Payment.payment,[String]$Payment.code)
$FinalEmployeeReport.Add[Int]$Payment.employee_id,[String]$Payment.payment))
}
Foreach($Payment in $ImportedPayments){
$NewPayment = [Payment]::new([Int]$Payment.id,[Int]$Payment.employee_id,[String]$Payment.payment,[String]$Payment.code)
Foreach($NewEmployeeEntry in $Payment){
if($NewPayment.employee_id -eq $NewEmployeeEntry.id ){
$NewEmployeeEntry.Add($NewPayment)
}
}
}
$FinalEmployeeReport.Add($NewEmployeeEntry)
实现这一点的一种方法是使用一个简单的循环,并根据员工id在内部查找匹配的记录。
如果这些是您的输入,请确认
支付.csv
id,employee_id,payment,code
123,8765,1500,abc123
456,9007,100,xyz456
999,9007,200,def666
员工.csv
id,first_name,last_name,email,city,ip_address
9007,John,Doe,jdoe@yourcompany.com,Miami,10.10.10.10
8765,Joe,Bloggs,jbloggs@somewhere.org,Salem,10.11.12.13
然后尝试
# load both csv files
$payments = Import-Csv -Path 'D:Testpayments.csv'
$employees = Import-Csv -Path 'D:Testemployee.csv'
# loop through the employees records
$result = foreach ($emp in $employees){
# find a record in the payments.csv where the .employee_id is equal to the .id in the employee.csv
$payments | Where-Object { $_.employee_id -eq $emp.id } | ForEach-Object {
# create an object with properties from both csv files combined
$obj = $emp | Select-Object @{Name = 'employee_id'; Expression = {$_.id}}, * -ExcludeProperty id
# add the details from $payments to this
$obj | Add-Member -MemberType NoteProperty -Name 'payment_id' -Value $_.id
$obj | Add-Member -MemberType NoteProperty -Name 'payment' -Value $_.payment
$obj | Add-Member -MemberType NoteProperty -Name 'payment_code' -Value $_.code
# output the combined object to be collected in variable $result
$obj
}
}
# now you can show the results in the console
$result | Format-Table -AutoSize
# and save as new csv file
$result | Export-Csv -Path 'D:TestEmployeePayments.csv' -NoTypeInformation
屏幕输出:
employee_id first_name last_name email city ip_address payment_id payment payment_code
----------- ---------- --------- ----- ---- ---------- ---------- ------- ------------
9007 John Doe jdoe@yourcompany.com Miami 10.10.10.10 456 100 xyz456
9007 John Doe jdoe@yourcompany.com Miami 10.10.10.10 999 200 def666
8765 Joe Bloggs jbloggs@somewhere.org Salem 10.11.12.13 123 1500 abc123