【发布时间】:2020-11-26 21:48:22
【问题描述】:
我正在尝试使用我的代码创建一个文件夹,然后打开某个 Excel 文件,对其进行编辑,然后将其保存到通过函数 Dagcontrole_folders_maken 创建的位置。目前我在 ForEach 循环中使用这个 ForEach。但它不起作用。这是完整代码的简化版本。必须使用变量设置函数。
演示问题的代码:
$path_dagelijkse_controle = "C:\Users\Nick\Desktop\Test1",
"C:\Users\Nick\Desktop\Test2",
"C:\Users\Nick\Desktop\Test3"
$list_excels = 'C:\Users\nick\Desktop\Test1\kek3', #pad waar het excel bestand staat die geopend moet worden
'C:\Users\nick\Desktop\Test1\kek4',
'C:\Users\nick\Desktop\Test1\kek5'
function Dagcontrole_folders_maken ($huidige_folder) {
md -Path "$huidige_folder\2020" -Force # Makes all the neccessary folders
}
function Aanpassen_excel_dagcontrole ($path, $huidige_folder) {
$Excel = New-Object -ComObject excel.application
$Excel.visible = $True
$date_today= (get-date).DayOfWeek
$Workbook = $excel.Workbooks.open($path)
$workbook.SaveAs("$huidige_folder\$date_today")
$Excel.Quit()
Remove-Variable -Name excel
[gc]::collect()
[gc]::WaitForPendingFinalizers()
}
Foreach ($i in $path_dagelijkse_controle) {
Dagcontrole_folders_maken $i
Foreach ($a in $list_excels) {
Aanpassen_excel_dagcontrole $a $i
}
}
我在以下部分遇到错误:$workbook.SaveAs("$huidige_folder\$date_today")。告诉它无权访问该文件。
我要做的是将excel文件保存在function Dagcontrole_folders_maken刚刚创建的目录中。我在第二个循环中尝试使用来自$path_dagelijkse_controle 列表的数据
循环应该执行以下操作:
Dagcontrole_folder_maken
makes folder with location: "C:\Users\Nick\Desktop\Test1\2020"
Aanpassen_excel_dagcontrole
$Workbook = $excel.Workbooks.open('C:\Users\nick\Desktop\Test1\kek3')
$workbook.SaveAs('C:\Users\Nick\Desktop\Test1"\2020\20200806')
然后是应该做的:
Dagcontrole_folder_maken
makes folder with location: "C:\Users\Nick\Desktop\Test2\2020"
Aanpassen_excel_dagcontrole
$Workbook = $excel.Workbooks.open('C:\Users\nick\Desktop\Test2\kek4')
$workbook.SaveAs('C:\Users\Nick\Desktop\Test2"\2020\20200806')
然后是列表的其余部分
完整代码供参考:
$path_dagelijkse_controle = "C:\Users\Nick\Desktop\Test1",
"C:\Users\Nick\Desktop\Test2",
"C:\Users\Nick\Desktop\Test3"
$list_excels = 'C:\Users\nick\Desktop\Test1\kek3', #pad waar het excel bestand staat die geopend moet worden
'C:\Users\nick\Desktop\Test1\kek4',
'C:\Users\nick\Desktop\Test1\kek5'
function Dagcontrole_folders_maken ($huidige_folder) {
$Dagelijkse_controle = "Dagelijkse controle"
$datum_vandaag = $(Get-Date).toString('yyyy-MM-dd')
$jaar = $datum_vandaag.Substring(0,4)
$maand = $datum_vandaag.substring(5, 2)
$dag = (get-date).DayOfWeek
$folder_maand = Get-Date -UFormat "%m - %B"
md -Path "$huidige_folder\$jaar\$folder_maand\Dagelijks\$datum_vandaag" -Force # Makes all the neccessary folders
}
function Aanpassen_excel_dagcontrole ($path, $huidige_folder) {
#editing excel file
$Controle_mailbox_vrijdag = "Nora Remeeus"
$weekcontrole1 = "Maandag"
$weekcontrole2 = "Dinsdag"
$partimedag = "Woensdag"
$dagcontroleur_parttimedag = "Victor Wong"
$weekcontrole_persoon = "Nick Siegert"
$afwezig_mailboxcontrole = "Vrijdag"
$Excel = New-Object -ComObject excel.application
$Excel.visible = $False
$Workbook = $excel.Workbooks.open($path)
$Worksheet = $Workbook.WorkSheets.item("Uit te voeren werkzaamheden")
$worksheet.activate()
$workbook.ActiveSheet.Cells.Item(3,3) = Date
if ($dag -eq $partimedag) {
$workbook.ActiveSheet.Cells.Item(9,3) = $dagcontroleur_parttimedag
$workbook.ActiveSheet.Cells.Item(10,3) = $dagcontroleur_parttimedag
$workbook.ActiveSheet.Cells.Item(12,3) = $dagcontroleur_parttimedag
}
if (($dag -eq $weekcontrole1) -or ($dag -eq $weekcontrole2)) {
$workbook.ActiveSheet.Cells.Item(13,3) = $weekcontrole_persoon
}
if ($dag -eq $afwezig_mailboxcontrole) {
$workbook.ActiveSheet.Cells.Item(11,3) = $Controle_mailbox_vrijdag
}
$workbook.SaveAs("$huidige_folder\$jaar\$folder_maand\Dagelijks\$datum_vandaag\$Dagelijkse_controle $datum_vandaag") #Edit to save with Dagelijkse controle + datum_vandaag Hardcoded $huidige folder (eerste deel) oud: "$huidige_folder\$jaar\$folder_maand\Dagelijks\$datum_vandaag\$Dagelijkse_controle $datum_vandaag"
$Excel.Quit()
Remove-Variable -Name excel
[gc]::collect()
[gc]::WaitForPendingFinalizers()
}
Foreach ($i in $path_dagelijkse_controle) {
Dagcontrole_folders_maken $i
Foreach ($a in $list_excels) {
Aanpassen_excel_dagcontrole $a $i
}
}
【问题讨论】:
-
请说明是什么不工作。并尝试将提供的代码缩减到相关部分。我认为您正在寻找的是一个哈希表。见docs.microsoft.com/en-us/powershell/module/…
-
我刚刚编辑了帖子,感谢您的反馈
-
当你打开工作簿时,你确定是在打开工作簿吗?看起来你正在向我打开一个文件夹。
-
(顺便说一句,您可能想查看 ImportExcel。请参阅:powershellgallery.com/packages/ImportExcel)
标签: arrays list powershell foreach