Powershell Excel自动添加工作表

我试图自动化每个客户端名称在excel中添加一个工作表(包含数据)为每月报告types工作簿

我认为这应该是直接的…但我使用的方法没有工作….它甚至没有得到制表模式…你能帮我弄清楚我做错了什么?

以下是我所做的function

function Excelclientstatstemplate ($clients) { $Exl = New-Object -ComObject "Excel.Application" $Exl.Visible = $false $Exl.DisplayAlerts = $false $WB = $Exl.Workbooks.Open($excelmonthlysummary) $clientws = $WB.worksheets | where {$_.name -like "*$clients*"} #### Check if Clients worksheet exists, if no then make one with client name ### $sheetcheck = if (($clientws)) {} Else { $WS = $WB.worksheets.add $WS.name = "$clients" } $sheetcheck $WB.Save # Enter stat labels $clientws.cells.item(1,1) = "CPU Count" $clientws.cells.item(2,1) = "RAM" $clientws.cells.item(3,1) = "Reserved CPU" $clientws.cells.item(4,1) = "Reserved RAM" ### Put in Values in the next column ### $clientws.cells.item(1,2) = [int]($cstats.cpuAllocationGHz/2) $clientws.cells.item(2,2) = [decimal]$cstats.memoryLimitGB $clientws.cells.item(3,2) = [int]($cstats.rescpuAllocationGHz/2) $clientws.cells.item(4,2) = [decimal]$cstats.resmemoryLimitGB $WB.save $Exl.quit() Stop-Process -processname EXCEL Start-Sleep -Seconds 1 Echo "$clients excel sheet in monthly summary is done.." } 

然后我试着做一个Foreach的东西

  $clientxlmonthlywrite = Foreach ($client in $clientlist){ $cstats = $Combinedstats | Where {$_.Group -eq "$client"} Excelclientstatstemplate -clients $client } 

function的整个过程

  1. 以客户名称

  2. 打开一个特定的Excel工作簿

  3. 检查是否有客户名称的任何工作表

  4. 如果没有客户名称的表单,请使用客户名称

  5. 用标签填充第一列单元格

  6. 用数据填充第二列单元格(数据作品,我已经写了CSV的withem)

  7. 保存并退出

Foreachvariables只是为客户列表中的每个客户端名称执行函数(clientlist没有任何问题)


我搞砸了吗?

谢谢您的帮助。

您不正确调用.Add()方法。 你在结尾处遗漏了括号。 要解决它,你应该能够简单地修改这一行:

 $WS = $WB.worksheets.add() 

此外,单元格具有您应该引用的属性,所以我也会修改设置您的单元格值的部分,如下所示:

 # Enter stat labels $clientws.cells.item(1,1).value2 = "CPU Count" $clientws.cells.item(2,1).value2 = "RAM" $clientws.cells.item(3,1).value2 = "Reserved CPU" $clientws.cells.item(4,1).value2 = "Reserved RAM" ### Put in Values in the next column ### $clientws.cells.item(1,2).value2 = [int]($cstats.cpuAllocationGHz/2) $clientws.cells.item(2,2).value2 = [decimal]$cstats.memoryLimitGB $clientws.cells.item(3,2).value2 = [int]($cstats.rescpuAllocationGHz/2) $clientws.cells.item(4,2).value2 = [decimal]$cstats.resmemoryLimitGB 

我相当确定,定义types是毫无意义的,因为对于Excel,它们都是string,直到您将单元格的格式设置设置为其他值。 我可能是错的,但那是我观察到的行为。

现在,对于其他您没有要求的批评…不要启动Excel,打开书籍,保存书籍,并closures每个客户端的Excel。 一开始打开Excel,打开书,为每个客户做出更新,然后保存并closures。

testing客户端是否有工作表,如果需要添加,然后select客户的工作表。 现在没有什么可以设置$clientws如果你必须为该客户端添加一个。

默认添加工作表将其放在活动工作表之前。 在我看来,这在devise上是一个糟糕的select,但事实却是如此。 如果是我,我会添加指定工作簿中最后一个工作表的新工作表,这将在最后一个工作表之前添加新的工作表,使其成为第二个到最后一个工作表。 然后,我将最后一张工作表放在新的工作表的前面,有效地添加新工作表作为最后一个工作表。 是否有可能添加新的工作表作为最后一个时,你做? 是的,但这对我的口味来说太复杂了。 看到这里,如果你有兴趣这样做。

当testing一个现有的客户端工作表时,如果缺less这个工作表,就这样做,不要告诉它testing某个东西,什么都不做,并把所有你想要的东西放在一个Else语句中。 这只是使事情复杂化。 所有这一切,这里有一些build议付诸实践:

 function Excelclientstatstemplate ($clients) { #### Check if Clients worksheet exists, if no then make one with client name ### if (($clients -notin $($WB.worksheets).Name)){ #Find the current last sheet $LastSheet = $WB.Worksheets|Select -Last 1 #Make a new sheet before the current last sheet so it's near the end $WS = $WB.worksheets.add($LastSheet) #Name it $WS.name = "$clients" #Move the last sheet up one spot, making the new sheet the new effective last sheet $LastSheet.Move($WS) } #Find the current client sheet regardless of if it existed before or not $clientws = $WB.worksheets | where {$_.name -like "*$clients*"} # Enter stat labels $clientws.cells.item(1,1).value2 = "CPU Count" $clientws.cells.item(2,1).value2 = "RAM" $clientws.cells.item(3,1).value2 = "Reserved CPU" $clientws.cells.item(4,1).value2 = "Reserved RAM" ### Put in Values in the next column ### $clientws.cells.item(1,2).value2 = [int]($cstats.cpuAllocationGHz/2) $clientws.cells.item(2,2).value2 = [decimal]$cstats.memoryLimitGB $clientws.cells.item(3,2).value2 = [int]($cstats.rescpuAllocationGHz/2) $clientws.cells.item(4,2).value2 = [decimal]$cstats.resmemoryLimitGB Start-Sleep -Seconds 1 Echo "$clients excel sheet in monthly summary is done.." } $Exl = New-Object -ComObject "Excel.Application" $Exl.Visible = $false $Exl.DisplayAlerts = $false $WB = $Exl.Workbooks.Open($excelmonthlysummary) $clientxlmonthlywrite = Foreach ($client in $clientlist){ $cstats = $Combinedstats | Where {$_.Group -eq "$client"} Excelclientstatstemplate -clients $client } $WB.save $Exl.quit() Stop-Process -processname EXCEL