Powershell Excel自动添加工作表

时间:2017-02-28 21:26:00

标签: excel powershell com 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.."
}

然后我试图为它制作一个Foreach的东西

  $clientxlmonthlywrite = Foreach ($client in $clientlist){

  $cstats = $Combinedstats | Where {$_.Group -eq "$client"}

  Excelclientstatstemplate -clients $client

} 

该功能的整个过程

  1. 取客户名称

  2. 打开特定的Excel工作簿

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

  4. 如果没有包含客户名称的工作表,请使用客户名称

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

  6. 用数据填充第二列单元格(数据工作我已经写了CSVs)

  7. 保存并退出

  8. Foreach变量只为客户列表中的每个客户端名称执行此功能(客户列表没有错误)

    我搞砸了什么?

    感谢您的帮助。

1 个答案:

答案 0 :(得分:4)

您没有正确调用.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 

我非常确定定义类型是没有意义的,因为对Excel来说,除非您将单元格的格式设置设置为其他内容,否则它们都是所有字符串。我可能错了,但这是我观察到的行为。

现在,对于您没有要求的其他批评......不要启动Excel,打开书籍,保存书籍,并为每个客户端关闭Excel。首先打开Excel一次,打开书籍,为每个客户端进行更新,然后保存并关闭。

测试客户端是否有工作表,并在需要时添加,然后选择客户端的工作表。现在,如果您必须为该客户端添加一个$clientws,则无法设置Else

默认情况下添加工作表会将其放在活动工作表之前。在我看来,这是一个糟糕的设计选择,但它就是它的本质。如果是我,我将添加新工作表,指定工作簿中的最后一个工作表,这将在最后一个工作表之前添加新工作表,使其成为最后一个工作表的第二个工作表。然后,我将最后一张工作表移到新工作表的前面,有效地添加新工作表作为列出的最后一张工作表。您可以将新工作表添加为最后一个工作表吗?是的,但这对我来说太复杂了。如果您对此感兴趣,请参阅here

在测试现有客户端工作表时如果缺少一个,请执行此操作,不要告诉它测试某些内容,什么都不做,并将所需内容放在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 语句中。这只会让事情复杂化。尽管如此,其中一些建议付诸实践:

old = 1
new = 1

while new < target:
   new, old = old+new, new