我在使用PowerShell删除excel中的某些行时遇到了一些麻烦,目前我删除了某些列中包含某个单词的标题(由于某种原因,这些列完全没有像我预期的那样完成,但是我做了许可它工作)现在我必须删除excel文件中的所有行超过某一点,我已经尝试了很多方法,但它们似乎都没有工作,几乎所有被注释掉的东西都是以前没有用过的尝试,如果有人有任何可能的提示或解决方案,我真的很乐意帮助。 以下是我的代码。
#list of columns wanted
$want_list = ("CustomerCompanyName","InvoiceNumber","ChargeStartDate","ChargeEndDate",
"SubscriptionId","ServiceName","ServiceType","ResourceName","Region","ConsumedQuantity",
"IncludedQuantity","OverageQuantity","ListPrice","PretaxCharges","ChargeType","DomainName")
$bad_list = ("PartnerId","PartnerName","PartnerBillableAccountId","MpnId","SubscriptionName",
"SubscriptionDescription","OrderId","ResourceGuid","Sku","DetailLineItemId","TaxAmount",
"PostTaxTotal","Currency","PretaxEffectiveRate","PostTaxEffectiveRate","CustomerId")
$file = Read-Host "Enter Billing Info Location: example C:\Users\tyarn\Desktop\Billing_Folder\Billing_Info.csv"
#create new object
$excel = New-Object -ComObject Excel.Application
$excel.DisplayAlerts = $false
$excel.visible = $false
#open csv file
$workbook = $excel.WorkBooks.open($file)
$sheet = $workbook.Sheets.Item(1)
#get num columns
$num_cols = $sheet.UsedRange.Columns.Count
#for loop with deleting entire column if name of column isnt in list
For($i=1;$i -lt $num_cols;$i++){
if($sheet.Cells.Item(2,$i).Text -notin $want_list){[void]$sheet.Cells.Item(2,$i).EntireColumn.Delete()}
if($sheet.Cells.Item(2,$i).Text -in $bad_list){[void]$sheet.Cells.Item(2,$i).EntireColumn.Delete()}
if($sheet.Cells.Item(2,$i).Text -eq "OrderId"){[void]$sheet.Cells.Item(2,$i).EntireColumn.Delete()}
if($sheet.Cells.Item(2,$i).Text -eq "Currency"){[void]$sheet.Cells.Item(2,$i).EntireColumn.Delete()}
if($sheet.Cells.Item(2,$i).Text -eq "PretaxEffectiveRate"){[void]$sheet.Cells.Item(2,$i).EntireColumn.Delete()}
if($sheet.Cells.Item(2,$i).Text -eq "PartnerId"){[void]$sheet.Cells.Item(2,$i).EntireColumn.Delete()}
}
##Regular for loop going through and deleteing every row after a certain point doesnt work
#get number of rows
$num_rows = $sheet.UsedRange.Rows.Count
#doesnt work yet
#loop deleting unneeded rows
#$truth_value = 0
#For($i = 1; $i -lt $num_rows+1;$i++){
#if($sheet.Cells.Item($i,1).Text -eq ""){
#$truth_value = 1
#For($j = $i;$j-lt $num_rows; $j++){
#[void]$sheet.Cells.Item($j,1).EntireRow.Delete()}
#}
#if($truth_value -eq 1){break}
#}
#new try
##find row to start delete on
$row_start = $null
For($j=1;$j -lt $num_rows;$j++){
if($sheet.Cells.Item($j,1).Text -eq "Daily Usage"){
$row_start = $j
$start_delete = $row_start
}
}
##deleting row 56 everytime cause it moves up one when its deleted? doesnt work dont think
#For($row_start;$row_start -lt $num_rows; $row_start++){
# [void]$sheet.Cells.Item($row_start,1).EntireRow.Delete()
#$start_delete +=1
#}
#long solution
#For($i=1;$i -lt $num_cols;$i++){
# For($j=$start_delete;$j -lt $num_rows;$j++){
# [void]$sheet.Cells.Item($i,$j).clear()
# }
# }
##GET RID OF
#save
$file_name = Read-Host "What would you like the new files name to be"
#must create a folder for this
$workbook.SaveAs("C:\Users\tyarn\desktop\New_Billing\$file_name")
#close and release com
$workbook.Close($true)
$excel.Quit()
[void][System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel)
Remove-Variable excel
我只包含了“错误列表”,因为有些列没有删除,出于某种原因,我只包含了其他删除列for循环,因为仍有一些没有被删除,但这不是主要问题,主要问题是无论我试图循环删除某一点之后的所有行它都不会工作,它有时只有一行,或者所有行中的半列而不是所有行。
答案 0 :(得分:0)
你几乎在那里尝试了一次。使用以下代码,当$ start_delete已知时,$ start_delete行中的所有行都将被删除:
For($j = $start_delete; $j -le $num_rows; $j++){
[void]$sheet.Cells.Item($start_delete,1).EntireRow.Delete()
}
导入以使用-le
,这也将删除最后一行。使用$start_delete
作为行的指示,将确保每次都删除相同的行,因为文件正在缩小。