我有一个powershell脚本,用于重新排序word文档集合中的信息,其中包含多个条形码列表(以6个数字的字符串格式)和用户信息到一个电子表格和任何条形码未知的用户进入另一个。我想添加一行,以便将未包含条形码的任何用户包含在未知条形码文件中,因为目前这些用户正在处理中丢失。
我有一个用于挑选条形码的正则表达式,我已经尝试检查$ matches -eq''或$ matches -eq $ null的$ matches变量,它返回无输出或每个用户表格,分别。我也尝试检查我存储条形码的变量,如果它是$ null或空的,结果相同。最后,我尝试编写另一个正则表达式来检查条形码的格式是否匹配,这样就返回了工作表上的每个用户。
以下是我正在使用的字段及其属性的示例:
#This is the field which would contain barcodes. Note that there are several instances
#of fields with this name in any given sheet. The text is stored in the Result property,
#and for this field it should include a listing of 6-digit numbers possibly including
#letters to indicate the type of equipment the barcode represents. It will sometimes
#contain notes from whomever filled the sheet in.
Application : Microsoft.Office.Interop.Word.ApplicationClass
Creator : **REDACTED**
Parent : Microsoft.Office.Interop.Word.DocumentClass
Type : 70
Name : Text10
EntryMacro :
ExitMacro :
OwnHelp : False
OwnStatus : False
HelpText :
StatusText :
Enabled : True
Result : EQUIPMENT WAS ALREADY MOVED
TextInput : System.__ComObject
CheckBox : System.__ComObject
DropDown : System.__ComObject
Next : System.__ComObject
Previous : System.__ComObject
CalculateOnExit : False
Range : System.__ComObject
------------------------------------------------
Application : Microsoft.Office.Interop.Word.ApplicationClass
Creator : **REDACTED**
Parent : Microsoft.Office.Interop.Word.DocumentClass
Type : 70
Name : Text10
EntryMacro :
ExitMacro :
OwnHelp : False
OwnStatus : False
HelpText :
StatusText :
Enabled : True
Result :
TextInput : System.__ComObject
CheckBox : System.__ComObject
DropDown : System.__ComObject
Next : System.__ComObject
Previous : System.__ComObject
CalculateOnExit : False
Range : System.__ComObject
-----------------------------------------------
Application : Microsoft.Office.Interop.Word.ApplicationClass
Creator : **REDACTED**
Parent : Microsoft.Office.Interop.Word.DocumentClass
Type : 70
Name : Text10
EntryMacro :
ExitMacro :
OwnHelp : False
OwnStatus : False
HelpText :
StatusText :
Enabled : True
Result : L 123456, M 654321, 456789, D 987654
TextInput : System.__ComObject
CheckBox : System.__ComObject
DropDown : System.__ComObject
Next : System.__ComObject
Previous : System.__ComObject
CalculateOnExit : False
Range : System.__ComObject
以下是我正在使用的代码(与完全没有条形码的用户完全不同),包括我最近未能尝试挑选这些用户:
$word = New-Object -ComObject Word.Application
$word.Visible = $false
$word.DisplayAlerts = "wdAlertsNone"
$results = "Header1,Header2,Header3,Header4`n"
$exceptions = "Header2,Header3,Header4`n"
$list = get-childitem 'H:\directory'
foreach($file in $list){
$path = "H:\directory\$($file.name)"
$doc = $word.Documents.Open($path)
$fields = $doc.FormFields
foreach($field in $fields){
if ($field.name -eq 'Text2'){
$ucc = $field.Result.Replace(' / ',",")
$ucc = $ucc.Replace('/',',')
}
elseif($field.name -eq 'Text5'){
$loc = $field.Result
}
$loc = $loc.Trim()
}
elseif($field.name -eq 'Text10'){
if($field.result -like '*UNK*'){
$exceptions+=$loc + "," + $ucc+"`n"
}
#this is where I am storing the barcodes
$bar = @([regex]::matches($field.Result,'\D*(\d{6})')|%{$_.groups[1].value})
#this is my attempt at picking out blank codes
if([regex]::Match($field.result,"*(\d[6])*") -ne $true){
$exceptions+=$loc + "," + $ucc + "`n"
}
foreach($code in $bar){
$results += $code + "," + $loc + "," + $ucc+"`n"
}
}
}
}
$exceptions | out-file 'H:\directory\exceptions.csv' -Encoding ascii -Force
$results | out-file 'H:\directory\list.csv' -Encoding ascii -Force
$word.quit()
[void][System.Runtime.InteropServices.Marshal]::ReleaseComObject($word)
答案 0 :(得分:1)
您可以使用Where
语句通过管道传递到ForEach
循环并使用自动变量$Matches
重新构建一些内容。
$bar = $field.Result | Where{$_ -match '\D*(\d{6})'} | ForEach{$Matches[1].value}
If(!$bar -eq $null){
foreach($code in $bar){
$results += $code + "," + $loc + "," + $ucc+"`n"
}
}Else{
$exceptions+=$loc + "," + $ucc + "`n"
}
修改:我添加了一些对象来模拟您的示例数据,并为每个对象生成$loc
和$ucc
变量的随机字母。然后我只输出到屏幕而不是文件。这就是我所看到的:
$letters= 97..123|%{[char]$_}
$fields = @(
[pscustomobject]@{'Name' = 'Text10';'Result' = @('asdf123456','dasrew345678','fdswer098765')},
[pscustomobject]@{'Name' = 'Text10';'Result' = 'EQUIPMENT WAS ALREADY MOVED'},
[pscustomobject]@{'Name' = 'Text10';'Result' = ''}
)
foreach($field in $fields){
$loc = (Get-Random -Count 5 -InputObject $letters) -join ''
$ucc = (Get-Random -Count 5 -InputObject $letters) -join ''
if ($field.name -eq 'Text2'){
$ucc = $field.Result.Replace(' / ',",")
$ucc = $ucc.Replace('/',',')
}
elseif($field.name -eq 'Text5'){
$loc = $field.Result
#}
$loc = $loc.Trim()
}
elseif($field.name -eq 'Text10'){
if($field.result -like '*UNK*'){
$exceptions+=$loc + "," + $ucc+"`n"
}
$bar = $field.Result | Where{ $_ -match '\D*(\d{6})' } | ForEach {$Matches[1]}
If(!($bar -eq $null)){
foreach($code in $bar){
"results+=$code + `",`" + $loc + `",`" + $ucc"
}
}Else{
"exceptions+=$loc + `",`" + $ucc"
}
}
}
输出5行。三个用于第一个对象,全部用于相同的"用户" loc和ucc使用相同的5个字符,然后每行一个条形码。然后它为没有条形码的记录再输出2行。
results+=123456 + "," + vpsjt + "," + lmsyv
results+=345678 + "," + vpsjt + "," + lmsyv
results+=098765 + "," + vpsjt + "," + lmsyv
exceptions+=hbptn + "," + ugydn
exceptions+=ygbop + "," + zy{mt