Guest User

Untitled

a guest
Jan 22nd, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. ######################################################################################################################
  2. # manually enter these values:
  3. $vendorName = 'CompanyX'
  4. $saveAsName = "CompanyX Items $(get-date -f yyyy-MM-dd).xlsx"
  5. $sheetName = 'Items'
  6. $colorColumn = 'G'
  7. $colorSwatchImageHyperlinkColumn = 'Y'
  8. ######################################################################################################################
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15. #progress bar function
  16. $progressBar = @{
  17. Activity = "Processing $vendorName file"
  18. Status = 'Gathering images'
  19. CurrentOperation = ''
  20. PercentComplete = 0
  21. Id = 1
  22. ParentId = -1
  23. }
  24. Write-Progress @progressBar
  25.  
  26.  
  27. ##get images
  28. $imageFolder = 'S:NSC914INS Vendor Data ProjectEcommerce FTP Images' + $vendorName + 'Images Consolidated'
  29. $images = Get-ChildItem -Path $imageFolder
  30. $progressBar.PercentComplete = 100; Write-Progress @progressBar
  31. ##get vendor's excel file
  32. $excelFolder = 'S:SharedProduct Enrichment DataEmergency Image Sprint'
  33. $vendorFile = Get-ChildItem -Path $excelFolder | Where-Object {$_.Name -like $vendorName + '*'} | Select-Object -ExpandProperty FullName
  34.  
  35.  
  36. ##start excel and open vendor file
  37. $progressBar.Status = 'Working with Excel'; $progressBar.CurrentOperation = 'Opening Excel'; $progressBar.PercentComplete = 0; Write-Progress @progressBar
  38. $excel = New-Object -ComObject Excel.Application
  39. $workbook = $excel.Workbooks.Open($vendorFile)
  40. $worksheet = $workbook.Worksheets.Item($sheetName)
  41. $excel.Visible = $true
  42. $rowCount = $worksheet.UsedRange.Rows.Count
  43. $progressBar.PercentComplete = 50; Write-Progress @progressBar
  44.  
  45.  
  46. $hyperlinkchunk1 = "=HYPERLINK("""
  47. $hyperlinkchunk2 = """, """
  48. $hyperlinkchunk3 = """)"
  49.  
  50.  
  51. ##stores column number based on user's input of column letter
  52. $x = $worksheet.Range("${abcColorColumn}1").Column
  53. $y = $worksheet.Range("${colorSwatchImageHyperlinkColumn}1").Column
  54. $progressBar.PercentComplete = 100; Write-Progress @progressBar
  55.  
  56. ##check if color matches any of the image file names
  57. ##if a match is found, creates a hyperlink to that image in the Colorswatchimagehyperlink column
  58. for ($i=2; $i -le $rowCount; $i++) {
  59. $progressBar.CurrentOperation = "Checking line $i of $rowCount"; $progressBar.PercentComplete = ($i / $rowCount) * 100; Write-Progress @progressBar
  60. $color = $worksheet.Cells.Item($i,$x).Text -replace 's','' ##Item() = (row,column)
  61. foreach ($image in $images) {
  62. if (($image.BaseName -replace 'W','') -match $color) { ##needs to be longerString -match shorterString
  63. $worksheet.Cells.Item($i,$y).Value = $hyperlinkchunk1 + $image.FullName + $hyperlinkchunk2 + $image.Name + $hyperlinkchunk3
  64. $worksheet.Cells.Item($i,$y).Font.Underline = $false
  65. $worksheet.Cells.Item($i,$y).Font.ColorIndex = $xlAutomatic
  66. }
  67. }
  68. }
  69.  
  70.  
  71. ##format, save, and quit excel; release comObjects
  72. $progressBar.CurrentOperation = 'Saving and closing'; $progressBar.PercentComplete = 50; Write-Progress @progressBar
  73. $worksheet.Columns($colorSwatchImageHyperlinkColumn).AutoFit()
  74. $workbook.SaveAs($excelFolder+$saveAsName)
  75. $workbook.Close()
  76. $excel.Quit()
  77. Get-Process excel | Stop-Process -Force
  78. [System.Runtime.Interopservices.Marshal]::ReleaseComObject($worksheet)
  79. [System.Runtime.Interopservices.Marshal]::ReleaseComObject($workbook)
  80. [System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel)
Add Comment
Please, Sign In to add comment