Coder Social home page Coder Social logo

Comments (4)

pauljnav avatar pauljnav commented on June 2, 2024

Hello. I performed a test of this today , and I'm observing no issue.
My observation; As mentioned by PakoGomezG: after the first 'Export-Excel' $defaultStyle [PSBoundParametersDictionary] has "Range" property with the dimension of the sheet ("A1:BO11" in my test). Those dimensions are static for all 3x iterations of the foreach loop, and the styling (HorizontalAlignment Center) is applied as expected to all 3x worksheets.

Versions used in test :

PSVersion 5.1.19041.4046 with ImportExcel 7.8.6
PSVersion 7.3.2 with ImportExcel 7.8.6

Code sample:

$defaultStyle = New-ExcelStyle -AutoSize -HorizontalAlignment Center

$sheets = 'firstSheet','secondSheet','thirdSheet'

$data = Get-Process | Select-Object -First 10

$excelFile = "C:\Temp\testFile1.xlsx"

foreach ($sheet in $sheets) {
    $data | Export-Excel -Path $excelFile -WorksheetName $sheet -Style $defaultStyle
    $defaultStyle | Out-String | Write-Host # Lets observe $defaultStyle using Write-Host
}

from importexcel.

PakoGomezG avatar PakoGomezG commented on June 2, 2024

Hello,

In your case is working fine because you are exporting the same data with the same range of table, but usually different data is exported with a different range.

For example:

$computers = @('cpt1','cpt2','cpt3')
$defaultStyle = New-ExcelStyle -AutoSize -HorizontalAlignment Center

foreach ($cpt in $computers) {
      $data = Get-Process -ComputerName $cpt
      $data | Export-Excel -Path $excelFile -WorksheetName $cpt -Style $defaultStyle
}

In this case the DefaultStyle variable will have the range of the first Export and if the rest of the exports have a higher range, the style will not be applied to all the information.
So I can't use a default styles variable and have to define it before each export.

$computers = @('cpt1','cpt2','cpt3')

foreach ($cpt in $computers) {
      $data = Get-Process -ComputerName $cpt
      $defaultStyle = New-ExcelStyle -AutoSize -HorizontalAlignment Center
      $data | Export-Excel -Path $excelFile -WorksheetName $cpt -Style $defaultStyle
}

from importexcel.

pauljnav avatar pauljnav commented on June 2, 2024

Hi @PakoGomezG
Yes, my example was not changing data of range. So adjusting to follow your example; Dynamic data, and relocating the New-ExcelStyle command inside the loop, the sample below generates 3 different data sets, and also 3 different range sizes, and the issue is not reproduced. The ranges are adjusting and consequently the styling is correctly applied to the different ranges.
The ranges should change in your case as they do in this example.

$sheets = 'firstSheet','secondSheet','thirdSheet'

$excelFile = "C:\Temp\testFile1.xlsx"

foreach ($sheet in $sheets) {
    # different data per iteration
    $data = Get-Process | Select-Object -First (Get-Random -min 8 -max 20) -Skip (Get-Random -max 6)
    
    $defaultStyle = New-ExcelStyle -AutoSize -HorizontalAlignment Center    
    $data | Export-Excel -Path $excelFile -WorksheetName $sheet -Style $defaultStyle
    
    # Observe $defaultStyle using Write-Host
    $defaultStyle | Out-String | Write-Host
}

Each range is adjusting to the data, and I would expect same for you.

Key                 Value  
---                 -----  
AutoSize            True   
HorizontalAlignment Center 
Range               A1:BO20

Key                 Value  
---                 -----  
AutoSize            True   
HorizontalAlignment Center 
Range               A1:BO15

Key                 Value  
---                 -----  
AutoSize            True   
HorizontalAlignment Center 
Range               A1:BO18

from importexcel.

dfinke avatar dfinke commented on June 2, 2024

Thanks @PakoGomezG for raising this and @pauljnav for the scenarios.

Will keep an eye on this thread. 👀

from importexcel.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.