Adi*_*ilZ 2 com excel powershell worksheet
我正在尝试自动化为每月报告类型工作簿在 excel 中为每个客户名称添加工作表(带有数据)的过程
我认为它应该是直接的......但我使用的方法不起作用......它甚至没有进入制表模式......你能帮我弄清楚我做错了什么吗?
以下是我做的功能
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.."
}
Run Code Online (Sandbox Code Playgroud)
然后我试着为它做一个 Foreach 东西
$clientxlmonthlywrite = Foreach ($client in $clientlist){
$cstats = $Combinedstats | Where {$_.Group -eq "$client"}
Excelclientstatstemplate -clients $client
}
Run Code Online (Sandbox Code Playgroud)
函数的整个过程去
取客户名
打开特定的 Excel 工作簿
检查是否有任何带有客户名称的工作表
如果没有带有客户名称的工作表,请制作带有客户名称的工作表
用标签填充第一列单元格
用数据填充第二列单元格(数据有效,我已经用它们编写了 CSV)
保存并退出
Foreach 变量只是为来自客户端列表的每个客户端名称执行功能(客户端列表没有问题)
我搞砸了什么吗?
谢谢您的帮助。
您没有.Add()正确调用该方法。您缺少末尾的括号。要修复它,您应该能够简单地将行修改为:
$WS = $WB.worksheets.add()
Run Code Online (Sandbox Code Playgroud)
此外,单元格具有您应该参考的属性,因此我还将修改将单元格值设置为以下内容的部分:
# 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
Run Code Online (Sandbox Code Playgroud)
我相当确定定义类型是没有意义的,因为对于 Excel,它们都是字符串,直到您将单元格的格式设置设置为其他内容。我可能是错的,但这是我观察到的行为。
现在,对于您没有要求的其他批评... 不要为每个客户启动 Excel、打开书、保存书并关闭 Excel。开始时打开 Excel 一次,打开书,对每个客户端进行更新,然后保存并关闭。
测试以查看客户是否有工作表,并在需要时添加它,然后选择客户的工作表后记。现在,$clientws如果您必须为该客户端添加一个,则无需设置任何内容。
默认情况下,添加工作表会将其置于活动工作表之前。在我看来,这是一个糟糕的设计选择,但事实就是如此。如果是我,我会添加新工作表,指定工作簿中的最后一个工作表,这将在最后一个工作表之前添加新工作表,使其成为最后一个工作表的第二个工作表。然后我将最后一个工作表移到新工作表的前面,有效地将新工作表添加为列出的最后一个工作表。制作时是否可以将新工作表添加为最后一张?是的,但它对我的口味来说太复杂了。如果您对此感兴趣,请参阅此处。
当测试现有的客户工作表以使其丢失时,请执行此操作,不要告诉它测试某些内容,什么也不做,并将您想要的所有内容放在Else语句中。那只会让事情复杂化。综上所述,以下是一些已付诸实践的建议:
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
19687 次 |
| 最近记录: |