这是我在这里的第一篇文章。我对编码和Powershell相当陌生,因此这里可能会遗漏一些明显的东西。我使用Powershell和Smartsheet为自己的工作制作了QR码标签制作器。无需赘述,它会从我们在Smartsheet中制作的“产品跟踪器”中的一行中提取数据,并根据该数据创建带有QR码的标签。在该行的其中一列中,它模仿了您扫描QR码时产生的数据。它像这样模仿它。...
$SkuCell.Value = "BEGIN:VCARD
VERSION:3.0
KIND:individual
N:$($ptCO.Desc);$($ptCO.Po)
FN:$($ptCO.Po) $($ptCO.Desc)
ORG:All New Glass
EMAIL;TYPE=INTERNET:$($ptCO.Assign)
END:VCARD"
...,输出看起来像这样.....
BEGIN:VCARD
VERSION:3.0
KIND:individual
N:Test stuff here;12354
FN:12354 Test stuff here
ORG:All New Glass
EMAIL;TYPE=INTERNET:ericg@allnewglass.com
END:VCARD
然后在其他Smartsheet上,我们的驱动程序将扫描标签上的QR码,表明其已离开我们的集线器。该单元格中的扫描数据看起来像这样.....
BEGIN:VCARD
VERSION:3.0
KIND:individual
N:Test stuff here;12354
FN:12354 Test stuff here
ORG:All New Glass
EMAIL;TYPE=INTERNET:ericg@allnewglass.com
END:VCARD
然后我有一个不同的脚本来检查两张纸之间是否匹配,因此在产品跟踪器中,我们知道何时有东西离开轮毂。问题是,即使扫描的数据和模仿的数据看起来绝对相同,并且它们都说它们是字符串,但它们永远不会匹配。它们彼此不相等,我尝试了一切([string],out-string,convert-string,compare-object,trim等),我想让它们匹配,唯一可行的是我在对象上流水线出主机。但是,这些对象包含在一个foreach循环中,该循环检查两个工作表中的所有行,这意味着脚本会以某种方式降低显示所有数据的速度。扫描数据中似乎存在一些隐藏字符,导致它们不匹配。谁能想到使这两个字符串相同的解决方案?有没有办法删除所有隐藏的字符?还是有人有更好的主意?比较它们的另一种方法可能是吗?
这是试图进行比较的代码。...
# by GrEcHkO
#cd "C:\Users\Jacly\Desktop\ANG_System_Files"
cd "P:\ANG_System_Files"
function Load-Dll
{
param(
[string]$assembly
)
Write-Host "Loading $assembly"
$driver = $assembly
$fileStream = ([System.IO.FileInfo] (Get-Item $driver)).OpenRead();
$assemblyBytes = new-object byte[] $fileStream.Length
$fileStream.Read($assemblyBytes, 0, $fileStream.Length) | Out-Null;
$fileStream.Close();
$assemblyLoaded = [System.Reflection.Assembly]::Load($assemblyBytes);
}
function Get-ComparisonObjects
{
param([Smartsheet.Api.Models.Sheet]$sheet)
Write-Host "Getting Sheet $($sheet.Name) Comparison Objects"
$data = $sheet.Rows | foreach {
$checkVal = $false
$trackedCheckVal = $false
$finishedCheckVal = $false
if($_.Cells[3].Value -eq $true)
{
$checkVal = $true
}
if($_.Cells[16].Value -eq $true)
{
$trackedCheckVal = $true
}
if($_.Cells[12].Value -eq $true)
{
$finishedCheckVal = $true
}
[pscustomobject]@{
Attachments = $_.Attachments;
RowId = $_.Id;
RowNumber = $_.RowNumber;
Parent = $_.ParentId;
PoCol = $_.Cells[0].ColumnId;
Po = $_.Cells[0].Value;
JobsCol = $_.Cells[1].ColumnId;
Jobs = $_.Cells[1].Value;
DescCol = $_.Cells[2].ColumnId;
Desc = $_.Cells[2].Value;
CheckCol = $_.Cells[3].ColumnId;
Check = $checkVal;
SupplierCol = $_.Cells[4].ColumnId;
Supplier = $_.Cells[4].Value;
AssignCol = $_.Cells[5].ColumnId;
Assign = $_.Cells[5].Value;
DestinationCol = $_.Cells[11].ColumnId;
Destination = $_.Cells[11].Value;
FinishedCol = $_.Cells[12].ColumnId;
Finished = $finishedCheckVal;
DueCol = $_.Cells[14].ColumnId;
Due = $_.Cells[14].Value;
DeliveryCol = $_.Cells[15].ColumnId;
Delivery = $_.Cells[15].Value;
TrackedCol = $_.Cells[16].ColumnId;
Tracked = $trackedCheckVal;
SKUCol = $_.Cells[18].ColumnId;
SKU = $_.Cells[18].Value;
}
}| where {![string]::IsNullOrWhiteSpace($_.Po)}
Write-Host "$($data.Count) Returned"
return $data
}
function Get-DriverComparisonObjects
{
param([Smartsheet.Api.Models.Sheet]$sheet)
Write-Host "Getting Sheet $($sheet.Name) Comparison Objects"
$data = $sheet.Rows | foreach {
$checkVal = $false
$trackedCheckVal = $false
$finishedCheckVal = $false
$archiveCheckVal = $false
if($_.Cells[2].Value -eq $true)
{
$checkVal = $true
}
if($_.Cells[8].Value -eq $true)
{
$trackedCheckVal = $true
}
if($_.Cells[3].Value -eq $true)
{
$finishedCheckVal = $true
}
if($_.Cells[11].Value -eq $true)
{
$archiveCheckVal = $true
}
[pscustomobject]@{
Attachments = $_.Attachments;
RowId = $_.Id;
RowNumber = $_.RowNumber;
Parent = $_.ParentId;
DayCol = $_.Cells[0].ColumnId;
Day = $_.Cells[0].Value;
DueCol = $_.Cells[1].ColumnId;
Due = $_.Cells[1].Value;
CompletedCol = $_.Cells[2].ColumnId; #########hidden
Completed = $checkVal;
CheckCol = $_.Cells[3].ColumnId;
Check = $finishedCheckVal;
SupplierCol = $_.Cells[4].ColumnId;#########hidden
Supplier = $_.Cells[4].Value;
AssignCol = $_.Cells[5].ColumnId;#########hidden
Assign = $_.Cells[5].Value;
JobNameCol = $_.Cells[6].ColumnId;
JobName = $_.Cells[6].Value;
MainCol = $_.Cells[7].ColumnId;
Main = $_.Cells[7].Value;
TrackedCol = $_.Cells[8].ColumnId;
Tracked = $trackedCheckVal;
PoNumCol = $_.Cells[9].ColumnId;
PoNum = $_.Cells[9].Value;
ModifiedCol = $_.Cells[10].ColumnId;
Modified = $_.Cells[10].Value;
ArchiveCol = $_.Cells[11].ColumnId;
Archive = $archiveCheckVal;
}
} | where {![string]::IsNullOrWhiteSpace($_.PoNum)}
Write-Host "$($data.Count) Returned"
return $data
}
function Get-AttachmentFromSmartsheet
{
param (
[long]$attachmentId,
[long]$sheetId
)
Write-Host "Getting Attachement $attachmentId of Sheet $sheetId"
try
{
$attachment = $client.SheetResources.AttachmentResources.GetAttachment($sheetId,$attachmentId)
}
catch
{
Write-Error $_.Exception.Message
Write-Host ""
}
$downloads = New-Item -ItemType Directory ".\downloads" -Force
$filepath = "$($downloads.Fullname)\$($attachment.Name)"
Write-Host "Downloading $filepath"
Invoke-WebRequest -Uri $attachment.Url -OutFile $filepath
Get-Item $filepath
}
function Save-AttachmentToSheetRow
{
param(
[long]$sheetId,
[long]$rowId,
[System.IO.FileInfo]$file,
[string]$mimeType
)
Write-Host "Saving $($file.Fullname) to Sheet $sheetId"
$result = $client.SheetResources.RowResources.AttachmentResources.AttachFile($sheetId, $rowId, $file.FullName, $mimeType)
return $result
}
function Merge-DriverChecklistWithProductTracker
{
param(
[pscustomobject[]]$orbitalRecords,
[long]$orbitalId
)
foreach ($orbitalRecord in $orbitalRecords)
{
$descFound = $false
$skuFound = $false
if ($orbitalRecord.Archive -eq $false) #THIS IS WHERE I NEED HELP
{ #THIS IS WHERE I NEED HELP
foreach ($ptCO in $ptCOs) #THIS IS WHERE I NEED HELP
{ #THIS IS WHERE I NEED HELP
$orbitalQR = "$($orbitalRecord.PoNum)" #THIS IS WHERE I NEED HELP
$ptQR = "$($ptCO.SKU)" #THIS IS WHERE I NEED HELP
$noParent = $false #THIS IS WHERE I NEED HELP
#THIS IS WHERE I NEED HELP
if ($orbitalQR -eq $ptQR) #THIS IS WHERE I NEED HELP
{ #THIS IS WHERE I NEED HELP
$descFound = $true #THIS IS WHERE I NEED HELP
break #THIS IS WHERE I NEED HELP
} #THIS IS WHERE I NEED HELP
#THIS IS WHERE I NEED HELP
if ($orbitalQR -eq $ptQR) #THIS IS WHERE I NEED HELP
{ #THIS IS WHERE I NEED HELP
$skuFound = $true #THIS IS WHERE I NEED HELP
break #THIS IS WHERE I NEED HELP
} #THIS IS WHERE I NEED HELP
} #THIS IS WHERE I NEED HELP
#THIS IS WHERE I NEED HELP
if ($skuFound) #THIS IS WHERE I NEED HELP
{ #THIS IS WHERE I NEED HELP
if (![string]::IsNullOrWhiteSpace($orbitalRecord.PoNum) -and ![string]::IsNullOrWhiteSpace($ptCO.SKU)) #THIS IS WHERE I NEED HELP
{ #THIS IS WHERE I NEED HELP
Write-Host "SKU found. Updating data on PT." #THIS IS WHERE I NEED HELP
#THIS IS WHERE I NEED HELP
$destinationCell = [Smartsheet.Api.Models.Cell]::new() #THIS IS WHERE I NEED HELP
$destinationCell.ColumnId = $ptDestinationCol.Id #THIS IS WHERE I NEED HELP
$destinationCell.Value = "ITEM SCANNED TO ANG TRUCK" #THIS IS WHERE I NEED HELP
#THIS IS WHERE I NEED HELP
$shippedCell = [Smartsheet.Api.Models.Cell]::new() #THIS IS WHERE I NEED HELP
$shippedCell.ColumnId = $ptShippedCol.Id #THIS IS WHERE I NEED HELP
$shippedCell.Value = if ($orbitalRecord.Modified -ne $null){$orbitalRecord.Modified} else {[string]::Empty} #THIS IS WHERE I NEED HELP
#THIS IS WHERE I NEED HELP
$row = [Smartsheet.Api.Models.Row]::new() #THIS IS WHERE I NEED HELP
$row.Id = $ptCO.RowId #THIS IS WHERE I NEED HELP
$row.Cells = [Smartsheet.Api.Models.Cell[]]@($destinationCell, $shippedCell) #THIS IS WHERE I NEED HELP
#THIS IS WHERE I NEED HELP
$updateRow = $client.SheetResources.RowResources.UpdateRows($ptId, [Smartsheet.Api.Models.Row[]]@($row)) #THIS IS WHERE I NEED HELP
} #THIS IS WHERE I NEED HELP
} #THIS IS WHERE I NEED HELP
elseif ($orbitalRecord.PoNum -eq $ptCO.Po)
{
if ($descFound)
{
if ([string]::IsNullOrWhiteSpace($ptCO.Parent))
{
$noParent = $true
}
if ($noParent)
{
Write-Host "Updating Product Tracker with Driver Checklist $($orbitalRecord.PoNum) $($orbitalRecord.Main)"
$jobsCell = [Smartsheet.Api.Models.Cell]::new()
$jobsCell.ColumnId = $ptJobsCol.Id
$JobsCell.Value = if ($orbitalRecord.JobName -ne $null){$orbitalRecord.JobName} else {[string]::Empty}
$descCell = [Smartsheet.Api.Models.Cell]::new()
$descCell.ColumnId = $ptDescCol.Id
$descCell.Value = if ($orbitalRecord.Main -ne $null){$orbitalRecord.Main} else {[string]::Empty}
$checkCell = [Smartsheet.Api.Models.Cell]::new()
$checkCell.ColumnId = $ptCheckCol.Id
$checkCell.Value = $orbitalRecord.Completed
$assignCell = [Smartsheet.Api.Models.Cell]::new()
$assignCell.COlumnId = $ptAssignCol.Id
$assignCell.Value = if ($orbitalRecord.Assign -ne $null){$orbitalRecord.Assign} else {"ianz@allnewglass.com"}
$supCell = [Smartsheet.Api.Models.Cell]::new()
$supCell.COlumnId = $ptSupplierCol.Id
$supCell.Value = if (($orbitalRecord.Completed -eq $false) -and ($orbitalRecord.Supplier -eq "In Shop FAB")){$suppliers["$fabId"]} else {$suppliers["$DriveId"]}
$finishedCell = [Smartsheet.Api.Models.Cell]::new()
$finishedCell.ColumnId = $ptFinishedCol.Id
$finishedCell.Value = $orbitalRecord.Check
$row = [Smartsheet.Api.Models.Row]::new()
$row.Id = $ptCO.RowId
$row.Cells = [Smartsheet.Api.Models.Cell[]]@($JobsCell, $checkCell, $supCell, $assignCell, $finishedCell)
try
{
$updateRow = $client.SheetResources.RowResources.UpdateRows($ptId, [Smartsheet.Api.Models.Row[]]@($row))
$reference = if ($orbitalRecord.Attachments.Name -ne $null){$orbitalRecord.Attachments.Name} else {[string]::Empty}
$difference = if ($ptCO.Attachments.Name -ne $null){$ptCO.Attachments.Name} else {[string]::Empty}
$compareResults = Compare-Object -ReferenceObject $reference -DifferenceObject $difference -IncludeEqual -SyncWindow ([int]::MaxValue)
$missing = $compareResults | where SideIndicator -eq '<='
$missingAttachments = $missing.InputObject
$attachmentsToGet = $orbitalRecord.Attachments | where Name -in $missingAttachments
foreach($attachment in $attachmentsToGet)
{
Write-Host "Adding missing attachment $($attachment.name)"
$file = Get-AttachmentFromSmartsheet -attachmentId $attachment.Id -sheetId $orbitalId
$result = Save-AttachmentToSheetRow -sheetId $ptId -rowId $newRow.Id -file $file.FullName -mimeType $attachment.MimeType
}
}
catch
{
Write-Error $_.Exception.Message
Write-Host ""
}
if ($orbitalRecord.Check -eq $true)
{
$dateCell = [Smartsheet.Api.Models.Cell]::new()
$dateCell.ColumnId = $ptShippedCol.Id
$dateCell.Value = $orbitalRecord.Modified
$row = [Smartsheet.Api.Models.Row]::new()
$row.Id = $ptCO.RowId
$row.Cells = [Smartsheet.Api.Models.Cell[]]@($dateCell)
try
{
$updateRow = $client.SheetResources.RowResources.UpdateRows($ptId, [Smartsheet.Api.Models.Row[]]@($row))
}
catch
{
Write-Error $_.Exception.Message
Write-Host ""
}
}
if (![string]::IsNullOrWhiteSpace($orbitalRecord.Due))
{
$dateCell = [Smartsheet.Api.Models.Cell]::new()
$dateCell.ColumnId = $ptAnticipatedCol.Id
$dateCell.Value = $orbitalRecord.Due
$row = [Smartsheet.Api.Models.Row]::new()
$row.Id = $ptCO.RowId
$row.Cells = [Smartsheet.Api.Models.Cell[]]@($dateCell)
try
{
$updateRow = $client.SheetResources.RowResources.UpdateRows($ptId, [Smartsheet.Api.Models.Row[]]@($row))
}
catch
{
Write-Error $_.Exception.Message
Write-Host ""
}
}
}
}
}
else
{
if (!($descFound -or $skuFound))
{
if (![string]::IsNullOrWhiteSpace($orbitalRecord.PoNum))
{
if ($orbitalRecord.Tracked -eq "$true")
{
Write-Host "Adding to Product Tracker from Driver Checklist $($orbitalRecord.Po) $($orbitalRecord.Main)"
$poCell = [Smartsheet.Api.Models.Cell]::new()
$poCell.ColumnId = $ptPoCol.Id
$poCell.Value = if ($orbitalRecord.PoNum -ne $null){$orbitalRecord.PoNum} else {[string]::Empty}
$jobsCell = [Smartsheet.Api.Models.Cell]::new()
$jobsCell.ColumnId = $ptJobsCol.Id
$JobsCell.Value = if ($orbitalRecord.JobName -ne $null){$orbitalRecord.JobName} else {[string]::Empty}
$descCell = [Smartsheet.Api.Models.Cell]::new()
$descCell.ColumnId = $ptDescCol.Id
$descCell.Value = if ($orbitalRecord.Main -ne $null){$orbitalRecord.Main} else {[string]::Empty}
$checkCell = [Smartsheet.Api.Models.Cell]::new()
$checkCell.ColumnId = $ptCheckCol.Id
$checkCell.Value = $orbitalRecord.Completed
$supCell = [Smartsheet.Api.Models.Cell]::new()
$supCell.COlumnId = $ptSupplierCol.Id
$supCell.Value = if (($orbitalRecord.Completed -eq $false) -and ($orbitalRecord.Supplier -eq "In Shop FAB")){$suppliers["$fabId"]} else {$suppliers["$DriveId"]}
$assignCell = [Smartsheet.Api.Models.Cell]::new()
$assignCell.COlumnId = $ptAssignCol.Id
$assignCell.Value = if ($orbitalRecord.Assign -ne $null){$orbitalRecord.Assign} else {"ianz@allnewglass.com"}
$finishedCell = [Smartsheet.Api.Models.Cell]::new()
$finishedCell.ColumnId = $ptFinishedCol.Id
$finishedCell.Value = $orbitalRecord.Check
$row = [Smartsheet.Api.Models.Row]::new()
$row.ToBottom = $true
$row.Cells = [Smartsheet.Api.Models.Cell[]]@($poCell,$jobsCell,$descCell,$checkCell,$supCell, $assignCell, $finishedCell)
try
{
$newRow = $client.SheetResources.RowResources.AddRows($ptId, [Smartsheet.Api.Models.Row[]]@($row))
foreach($attachment in $orbitalRecord.Attachments)
{
$file = Get-AttachmentFromSmartsheet -attachmentId $attachment.Id -sheetId $orbitalId
$result = Save-AttachmentToSheetRow -sheetId $ptId -rowId $newRow.Id -file $file.FullName -mimeType $attachment.MimeType
}
}
catch
{
Write-Error $_.Exception.Message
Write-Host ""
}
}
}
}
}
}
}
}
Write-Host "Loading Dlls"
Load-Dll ".\smartsheet-csharp-sdk.dll"
Load-Dll ".\RestSharp.dll"
Load-Dll ".\Newtonsoft.Json.dll"
Load-Dll ".\NLog.dll"
while($true)
{
Write-Host "Fab Log to Driver List to Product Tracker system starting up."
$DriveId = ""
$fabId = ""
$ptId = ""
$suppliers = @{
$fabId = "In Shop FAB";
$DriveId = "DRIVER CHECKLIST";
}
$token = ""
$smartsheet = [Smartsheet.Api.SmartSheetBuilder]::new()
$builder = $smartsheet.SetAccessToken($token)
$client = $builder.Build()
$includes = @([Smartsheet.Api.Models.SheetLevelInclusion]::ATTACHMENTS)
$includes = [System.Collections.Generic.List[Smartsheet.Api.Models.SheetLevelInclusion]]$includes
Write-Host "Loading Sheets"
$Drive = $client.SheetResources.GetSheet($DriveId, $includes, $null, $null, $null, $null, $null, $null);
$fab = $client.SheetResources.GetSheet($fabId, $includes, $null, $null, $null, $null, $null, $null);
$pt = $client.SheetResources.GetSheet($ptId, $includes, $null, $null, $null, $null, $null, $null);
Write-Host "Comparing Objects"
$DriveCOs = Get-DriverComparisonObjects $Drive
$fabCOs = Get-ComparisonObjects $fab
$ptCOs = Get-ComparisonObjects $pt
Write-Host "Identifying Driver Checklist Columns"
#NO SPACE... BODY CHARACTER LIMIT FOR STACK OVERFLOW
Merge-DriverChecklistWithProductTracker -orbitalRecords $DriveCOs -orbitalId $DriveId
Write-Host "Driver Checklist to Product Tracker finished."
Start-Sleep -Seconds 10
}