mirror of
https://github.com/dfinke/ImportExcel.git
synced 2026-02-05 03:03:31 +00:00
Help updates after more proof-reading
This commit is contained in:
@@ -1,47 +1,57 @@
|
||||
Function Set-ExcelColumn {
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Adds or modifies a column in an Excel sheet, filling values, settings formatting and/or creating named ranges.
|
||||
Adds or modifies a column in an Excel worksheet, filling values, setting formatting and/or creating named ranges.
|
||||
.DESCRIPTION
|
||||
Set-ExcelColumn can take a value which is either a string containing a value or formula or a scriptblock
|
||||
which evaluates to a string, and optionally a column number and fills that value down the column.
|
||||
Set-ExcelColumn can take a value which is either a string containing a
|
||||
value or formula or a scriptblock which evaluates to a string,
|
||||
and optionally a column number and fills that value down the column.
|
||||
A column heading can be specified, and the column can be made a named range.
|
||||
The column can be formatted in the same operation.
|
||||
.EXAMPLE
|
||||
Set-ExcelColumn -Worksheet $ws -Column 5 -NumberFormat 'Currency'
|
||||
|
||||
$ws contains a worksheet object - and column E is set to use the local currency format.
|
||||
Intelisense will complete predefined number formats. You can see how currency is interpreted on the local computer with the command
|
||||
$ws contains a worksheet object - and column "E" is set to use the
|
||||
local currency format. Intelisense will complete the names of predefined
|
||||
number formats. You can see how currency is interpreted on the
|
||||
local computer with the command
|
||||
Expand-NumberFormat currency
|
||||
.EXAMPLE
|
||||
Set-ExcelColumn -Worksheet $ws -Heading "WinsToFastLaps" -Value {"=E$row/C$row"} -Column 7 -AutoSize -AutoNameRange
|
||||
|
||||
Here, $WS already contains a worksheet which contains counts of races won and fastest laps recorded by racing drivers (in columns C and E).
|
||||
Set-ExcelColumn specifies that Column 7 should have a heading of "WinsToFastLaps" and the data cells should contain =E2/C2 , =E3/C3 etc
|
||||
the new data cells should become a named range, which will also be named "WinsToFastLaps" the column width will be set automatically.
|
||||
.EXAMPLE.
|
||||
Here, $WS already contains a worksheet which holds counts of races won
|
||||
and fastest laps recorded by racing drivers (in columns C and E).
|
||||
Set-ExcelColumn specifies that Column 7 should have a heading of
|
||||
"WinsToFastLaps" and the data cells should contain =E2/C2 , =E3/C3 etc
|
||||
the new data cells should become a named range, which will also be
|
||||
named "WinsToFastLaps" and the column width will be set automatically.
|
||||
.EXAMPLE
|
||||
Set-ExcelColumn -Worksheet $ws -Heading "Link" -Value {"https://en.wikipedia.org" + $worksheet.cells["B$Row"].value } -AutoSize
|
||||
|
||||
In this example, the worksheet in $ws has partial links to wikipedia pages in column B.
|
||||
The -Value parameter is is a script block and it outputs a string which begins https... and ends with the value of cell at
|
||||
column B in the current row. When given a valid URI, Set-ExcelColumn makes it a hyperlink. The column will be autosized to fit the links.
|
||||
In this example, the worksheet in $ws has partial links to Wikipedia
|
||||
pages in column B. The -Value parameter is a script block which
|
||||
outputs a string beginning "https..." and ending with the value of
|
||||
the cell at column B in the current row.
|
||||
When given a valid URI, Set-ExcelColumn makes it a hyperlink.
|
||||
The column will be autosized to fit the links.
|
||||
.EXAMPLE
|
||||
4..6 | Set-ExcelColumn -Worksheet $ws -AutoNameRange
|
||||
|
||||
Again $ws contains a worksheet. Here columns 4 to 6 are made into named ranges, row 1 is used for the range name
|
||||
Again $ws contains a worksheet. Here columns 4 to 6 are made into
|
||||
named ranges, row 1 is used for the range name
|
||||
and the rest of the column becomes the range.
|
||||
#>
|
||||
[cmdletbinding()]
|
||||
[Alias("Set-Column")]
|
||||
[OutputType([OfficeOpenXml.ExcelColumn],[String])]
|
||||
Param (
|
||||
#If specifying the worksheet by name, the ExcelPackage object which contains the Sheet also needs to be passed.
|
||||
#If specifying the worksheet by name, the ExcelPackage object which contains the worksheet also needs to be passed.
|
||||
[Parameter(ParameterSetName="Package",Mandatory=$true)]
|
||||
[OfficeOpenXml.ExcelPackage]$ExcelPackage,
|
||||
#The sheet to update can be a given as a name or an Excel Worksheet object - this sets it by name.
|
||||
#The sheet to update can be given as a name or an Excel Worksheet object - this sets it by name.
|
||||
[Parameter(ParameterSetName="Package")]
|
||||
[String]$Worksheetname = "Sheet1",
|
||||
#This passes the worksheet object instead of passing a sheet name and a package.
|
||||
#This passes the worksheet object instead of passing a sheet name and an Excelpackage object.
|
||||
[Parameter(ParameterSetName="sheet",Mandatory=$true)]
|
||||
[OfficeOpenXml.ExcelWorksheet]$Worksheet,
|
||||
#Column to fill down - the first column is 1. 0 will be interpreted as first empty column.
|
||||
@@ -68,9 +78,9 @@
|
||||
[Switch]$Italic,
|
||||
#Underline the text using the underline style in -UnderlineType; use -Underline:$false to remove underlining.
|
||||
[Switch]$Underline,
|
||||
#Should Underline use single or double, normal or accounting mode ? the default is single normal.
|
||||
#Specifies whether underlining should be single or double, normal or accounting mode. The default is "Single".
|
||||
[OfficeOpenXml.Style.ExcelUnderLineType]$UnderLineType = [OfficeOpenXml.Style.ExcelUnderLineType]::Single,
|
||||
#Strike through text; use -Strikethru:$false to remove Strike through.
|
||||
#Strike through text; use -StrikeThru:$false to remove strike through.
|
||||
[Switch]$StrikeThru,
|
||||
#Subscript or Superscript (or None).
|
||||
[OfficeOpenXml.Style.ExcelVerticalAlignmentFont]$FontShift,
|
||||
@@ -80,32 +90,32 @@
|
||||
[float]$FontSize,
|
||||
#Change background color.
|
||||
[System.Drawing.Color]$BackgroundColor,
|
||||
#Background pattern - Solid by default.
|
||||
#Background pattern - "Solid" by default.
|
||||
[OfficeOpenXml.Style.ExcelFillStyle]$BackgroundPattern = [OfficeOpenXml.Style.ExcelFillStyle]::Solid ,
|
||||
#Secondary color for background pattern.
|
||||
[Alias("PatternColour")]
|
||||
[System.Drawing.Color]$PatternColor,
|
||||
#Turn on text wrapping; use -WrapText:$false to turn off word wrapping.
|
||||
#Turn on Text-Wrapping; use -WrapText:$false to turn off wrapping.
|
||||
[Switch]$WrapText,
|
||||
#Position cell contents to Left, Right, Center etc. Default is 'General'.
|
||||
#Position cell contents to Left, Right, Center etc. Default is "General".
|
||||
[OfficeOpenXml.Style.ExcelHorizontalAlignment]$HorizontalAlignment,
|
||||
#Position cell contents to Top, Bottom or Center.
|
||||
[OfficeOpenXml.Style.ExcelVerticalAlignment]$VerticalAlignment,
|
||||
#Degrees to rotate text. Up to +90 for anti-clockwise ("upwards"), or to -90 for clockwise.
|
||||
[ValidateRange(-90, 90)]
|
||||
[int]$TextRotation ,
|
||||
#Autofit cells to width.
|
||||
#Attempt to auto-fit cells to the width their contents.
|
||||
[Alias("AutoFit")]
|
||||
[Switch]$AutoSize,
|
||||
#Set cells to a fixed width, ignored if -Autosize is specified.
|
||||
#Set cells to a fixed width, ignored if -AutoSize is specified.
|
||||
[float]$Width,
|
||||
#Set the inserted data to be a named range.
|
||||
[Switch]$AutoNameRange,
|
||||
#Hide the column.
|
||||
[Switch]$Hide,
|
||||
#If Sepecified, returns the range of cells which were affected.
|
||||
#If specified, returns the range of cells which were affected.
|
||||
[Switch]$Specified,
|
||||
#If Specified, return the Column to allow further work to be done on it.
|
||||
#If specified, return an object representing the Column, to allow further work to be done on it.
|
||||
[Switch]$PassThru
|
||||
)
|
||||
|
||||
|
||||
60
Set-Row.ps1
60
Set-Row.ps1
@@ -1,32 +1,40 @@
|
||||
Function Set-ExcelRow {
|
||||
<#
|
||||
.Synopsis
|
||||
Fills values into a [new] row in an Excel spreadsheet. And sets row formats.
|
||||
Fills values into a [new] row in an Excel spreadsheet, and sets row formats.
|
||||
.Description
|
||||
Set-ExcelRow accepts either a Worksheet object or an Excel Package object returned by Export-Excel and the name of a sheet,
|
||||
and inserts the chosen contents into a row of the sheet.
|
||||
The contents can be a constant e.g. "42", a formula or a script block which is converted into a constant or a formula.
|
||||
Set-ExcelRow accepts either a Worksheet object or an ExcelPackage object
|
||||
returned by Export-Excel and the name of a sheet, and inserts the chosen
|
||||
contents into a row of the sheet. The contents can be a constant,
|
||||
like "42", a formula or a script block which is converted into a
|
||||
constant or a formula.
|
||||
The first cell of the row can optionally be given a heading.
|
||||
.Example
|
||||
Set-ExcelRow -Worksheet $ws -Heading Total -Value {"=sum($columnName`2:$columnName$endrow)" }
|
||||
Set-ExcelRow -Worksheet $ws -Heading Total -Value {"=sum($columnName`2:$columnName$endrow)" }
|
||||
|
||||
$Ws contains a worksheet object, and no Row number is specified so Set-ExcelRow will select the next row after the end
|
||||
of the data in the sheet. The first cell in the row will contain "Total", and each other cell will contain
|
||||
=Sum(xx2:xx99) - where xx is the column name, and 99 is the last row of data.
|
||||
Note the use of `2 to Prevent 2 becoming part of the variable "ColumnName"
|
||||
The script block can use $Worksheet, $Row, $Column (number), $ColumnName (letter), $StartRow/Column and $EndRow/Column
|
||||
$Ws contains a worksheet object, and no Row number is specified so
|
||||
Set-ExcelRow will select the next row after the endof the data in
|
||||
the sheet. The first cell in the row will contain "Total", and
|
||||
each of the other cells will contain
|
||||
=Sum(xx2:xx99)
|
||||
where xx is the column name, and 99 is the last row of data.
|
||||
Note the use of `2 to Prevent 2 becoming part of the variable "ColumnName"
|
||||
The script block can use $Worksheet, $Row, $Column (number),
|
||||
$ColumnName (letter), $StartRow/Column and $EndRow/Column.
|
||||
.Example
|
||||
Set-ExcelRow -Worksheet $ws -Heading Total -HeadingBold -Value {"=sum($columnName`2:$columnName$endrow)" } -NumberFormat 'Currency' -StartColumn 2 -Bold -BorderTop Double -BorderBottom Thin
|
||||
|
||||
This builds on the previous example, but this time the label "Total" appears in column 2 and the formula fills from column 3 onwards;
|
||||
the formula and heading are set in bold face, and the formula is formatted for the local currency,
|
||||
and given a double line border above and single line border below.
|
||||
This builds on the previous example, but this time the label "Total"
|
||||
appears in column 2 and the formula fills from column 3 onwards.
|
||||
The formula and heading are set in bold face, and the formula is
|
||||
formatted for the local currency, and given a double line border
|
||||
above and single line border below.
|
||||
#>
|
||||
[cmdletbinding()]
|
||||
[Alias("Set-Row")]
|
||||
[OutputType([OfficeOpenXml.ExcelRow],[String])]
|
||||
Param (
|
||||
#An Excel package object - e.g. from Export-Excel -passthru - requires a sheet name.
|
||||
#An Excel package object - e.g. from Export-Excel -PassThru - requires a sheet name.
|
||||
[Parameter(ParameterSetName="Package",Mandatory=$true)]
|
||||
[OfficeOpenXml.ExcelPackage]$ExcelPackage,
|
||||
#The name of the sheet to update in the package.
|
||||
@@ -40,20 +48,20 @@
|
||||
$Row = 0 ,
|
||||
#Position in the row to start from.
|
||||
[int]$StartColumn,
|
||||
#Value, formula or script block to fill in. Script block can use $worksheet, $row, $Column [number], $ColumnName [letter(s)], $startRow, $startColumn, $endRow, $endColumn
|
||||
#Value, Formula or ScriptBlock to fill in. A ScriptBlock can use $worksheet, $row, $Column [number], $ColumnName [letter(s)], $startRow, $startColumn, $endRow, $endColumn.
|
||||
$Value,
|
||||
#Optional Row heading.
|
||||
#Optional row-heading.
|
||||
$Heading ,
|
||||
#Set the heading in bold type.
|
||||
[Switch]$HeadingBold,
|
||||
#Change the size of the heading type.
|
||||
#Change the font-size of the heading.
|
||||
[Int]$HeadingSize ,
|
||||
#Number format to apply to cells e.g. "dd/MM/yyyy HH:mm", "£#,##0.00;[Red]-£#,##0.00", "0.00%" , "##/##" , "0.0E+0" etc.
|
||||
[Alias("NFormat")]
|
||||
$NumberFormat,
|
||||
#Style of border to draw around the row.
|
||||
[OfficeOpenXml.Style.ExcelBorderStyle]$BorderAround,
|
||||
#Color of the border
|
||||
#Color of the border.
|
||||
[System.Drawing.Color]$BorderColor=[System.Drawing.Color]::Black,
|
||||
#Style for the bottom border.
|
||||
[OfficeOpenXml.Style.ExcelBorderStyle]$BorderBottom,
|
||||
@@ -63,7 +71,7 @@
|
||||
[OfficeOpenXml.Style.ExcelBorderStyle]$BorderLeft,
|
||||
#Style for the right border.
|
||||
[OfficeOpenXml.Style.ExcelBorderStyle]$BorderRight,
|
||||
#Colour for the text - if none specified it will be left as it it is.
|
||||
#Color for the text - if none specified it will be left as it it is.
|
||||
[System.Drawing.Color]$FontColor,
|
||||
#Make text bold; use -Bold:$false to remove bold.
|
||||
[Switch]$Bold,
|
||||
@@ -71,9 +79,9 @@
|
||||
[Switch]$Italic,
|
||||
#Underline the text using the underline style in -UnderlineType; use -Underline:$false to remove underlining.
|
||||
[Switch]$Underline,
|
||||
#Should Underline use single or double, normal or accounting mode : default is single normal.
|
||||
#Specifies whether underlining should be single or double, normal or accounting mode. The default is "Single".
|
||||
[OfficeOpenXml.Style.ExcelUnderLineType]$UnderLineType = [OfficeOpenXml.Style.ExcelUnderLineType]::Single,
|
||||
#Strike through text; use -Strikethru:$false to remove Strike through.
|
||||
#Strike through text; use -StrikeThru:$false to remove strike through.
|
||||
[Switch]$StrikeThru,
|
||||
#Subscript or Superscript (or none).
|
||||
[OfficeOpenXml.Style.ExcelVerticalAlignmentFont]$FontShift,
|
||||
@@ -88,20 +96,20 @@
|
||||
#Secondary color for background pattern.
|
||||
[Alias("PatternColour")]
|
||||
[System.Drawing.Color]$PatternColor,
|
||||
#Turn on text wrapping; use -WrapText:$false to turn off word wrapping.
|
||||
#Turn on Text-Wrapping; use -WrapText:$false to turn off wrapping.
|
||||
[Switch]$WrapText,
|
||||
#Position cell contents to Left, Right, Center etc. default is 'General'.
|
||||
[OfficeOpenXml.Style.ExcelHorizontalAlignment]$HorizontalAlignment,
|
||||
#Position cell contents to Top Bottom or Center.
|
||||
#Position cell contents to Top, Bottom or Center.
|
||||
[OfficeOpenXml.Style.ExcelVerticalAlignment]$VerticalAlignment,
|
||||
#Degrees to rotate text. Up to +90 for anti-clockwise ("upwards"), or to -90 for clockwise.
|
||||
[ValidateRange(-90, 90)]
|
||||
[int]$TextRotation ,
|
||||
#Set cells to a fixed hieght.
|
||||
#Set cells to a fixed height.
|
||||
[float]$Height,
|
||||
#Hide the Row.
|
||||
#Hide the row.
|
||||
[Switch]$Hide,
|
||||
#If Sepecified returns the range of cells which were affected.
|
||||
#If sepecified, returns the range of cells which were affected.
|
||||
[Switch]$ReturnRange,
|
||||
#If Specified, return a row object to allow further work to be done.
|
||||
[Switch]$PassThru
|
||||
|
||||
@@ -1,27 +1,34 @@
|
||||
Function Set-ExcelRange {
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Applies Number, font, alignment and color formatting, values or formulas to a range of Excel Cells.
|
||||
Applies number, font, alignment and/or color formatting, values or formulas to a range of Excel cells.
|
||||
.DESCRIPTION
|
||||
Set-ExcelRange was created to set the style elements for a range of cells, this includes
|
||||
auto-sizing and hiding, setting font elements (Name, Size, Bold, Italic, Underline & UnderlineStyle and Subscript & SuperScript),
|
||||
font and background colors, borders, text wrapping, rotation, aliginment within cells, and number format.
|
||||
It was orignally named "Set-Format",but it has been extended to set Values, Formulas and
|
||||
ArrayFormulas (sometimes called Ctrl-shift-Enter [CSE] formulas); because of this
|
||||
The name has become Set-ExcelRange - but the old name of Set-Format is preserved as an alias name.
|
||||
Set-ExcelRange was created to set the style elements for a range of cells,
|
||||
this includes auto-sizing and hiding, setting font elements (Name, Size,
|
||||
Bold, Italic, Underline & UnderlineStyle and Subscript & SuperScript),
|
||||
font and background colors, borders, text wrapping, rotation, alignment
|
||||
within cells, and number format.
|
||||
It was orignally named "Set-Format", but it has been extended to set
|
||||
Values, Formulas and ArrayFormulas (sometimes called Ctrl-shift-Enter
|
||||
[CSE] formulas); because of this, the name has become Set-ExcelRange
|
||||
but the old name of Set-Format is preserved as an alias.
|
||||
.EXAMPLE
|
||||
$sheet.Column(3) | Set-ExcelRange -HorizontalAlignment Right -NumberFormat "#,###" -AutoFit
|
||||
|
||||
Selects column 3 from a sheet object (within a workbook object, which is a child of the ExcelPackage object) and passes it to Set-ExcelRange
|
||||
which formats as an integer with comma-separated groups, aligns it right, and auto-fits the column to the contents.
|
||||
Selects column 3 from a sheet object (within a workbook object, which
|
||||
is a child of the ExcelPackage object) and passes it to Set-ExcelRange
|
||||
which formats numbers as a integers with comma-separated groups,
|
||||
aligns it right, and auto-fits the column to the contents.
|
||||
.EXAMPLE
|
||||
Set-ExcelRange -Range $sheet.Cells["E1:H1048576"] -HorizontalAlignment Right -NumberFormat "#,###"
|
||||
|
||||
Instead of piping the address, this version specifies a block of cells and applies similar formatting.
|
||||
Instead of piping the address, this version specifies a block of cells
|
||||
and applies similar formatting.
|
||||
.EXAMPLE
|
||||
Set-ExcelRange $excel.Workbook.Worksheets[1].Tables["Processes"] -Italic
|
||||
|
||||
This time instead of specifying a range of cells, a table is selected by name and formatted as italic.
|
||||
This time instead of specifying a range of cells, a table is selected
|
||||
by name and formatted as italic.
|
||||
#>
|
||||
[cmdletbinding()]
|
||||
[Alias("Set-Format")]
|
||||
@@ -55,16 +62,16 @@
|
||||
$Formula,
|
||||
#Specifies formula should be an array formula (a.k.a CSE [ctrl-shift-enter] formula).
|
||||
[Switch]$ArrayFormula,
|
||||
#Clear Bold, Italic, StrikeThrough and Underline and set colour to black.
|
||||
#Clear Bold, Italic, StrikeThrough and Underline and set color to Black.
|
||||
[Switch]$ResetFont,
|
||||
#Make text bold; use -Bold:$false to remove bold.
|
||||
[Switch]$Bold,
|
||||
#Make text italic; use -Italic:$false to remove italic.
|
||||
[Switch]$Italic,
|
||||
#Underline the text using the underline style in -underline type; use -Underline:$false to remove underlining.
|
||||
#Underline the text using the underline style in -UnderlineType; use -Underline:$false to remove underlining.
|
||||
[Switch]$Underline,
|
||||
#Should Underline use single or double, normal or accounting mode: the default is single normal.
|
||||
[OfficeOpenXml.Style.ExcelUnderLineType]$UnderLineType = [OfficeOpenXml.Style.ExcelUnderLineType]::Single,
|
||||
#Specifies whether underlining should be single or double, normal or accounting mode. The default is "Single".
|
||||
[OfficeOpenXml.Style.ExcelUnderLineType]$UnderLineType = [OfficeOpenXml.Style.ExcelUnderLineType]::Single,
|
||||
#Strike through text; use -Strikethru:$false to remove Strike through
|
||||
[Switch]$StrikeThru,
|
||||
#Subscript or Superscript (or none).
|
||||
@@ -80,11 +87,11 @@
|
||||
#Secondary color for background pattern.
|
||||
[Alias("PatternColour")]
|
||||
[System.Drawing.Color]$PatternColor,
|
||||
#Turn on text wrapping; use -WrapText:$false to turn off word wrapping.
|
||||
#Turn on Text-Wrapping; use -WrapText:$false to turn off wrapping.
|
||||
[Switch]$WrapText,
|
||||
#Position cell contents to Left, Right, Center etc. default is 'General'.
|
||||
[OfficeOpenXml.Style.ExcelHorizontalAlignment]$HorizontalAlignment,
|
||||
#Position cell contents to Top Bottom or Center.
|
||||
#Position cell contents to Top, Bottom or Center.
|
||||
[OfficeOpenXml.Style.ExcelVerticalAlignment]$VerticalAlignment,
|
||||
#Degrees to rotate text. Up to +90 for anti-clockwise ("upwards"), or to -90 for clockwise.
|
||||
[ValidateRange(-90, 90)]
|
||||
@@ -94,7 +101,7 @@
|
||||
[Switch]$AutoSize,
|
||||
#Set cells to a fixed width (columns or ranges only), ignored if Autosize is specified.
|
||||
[float]$Width,
|
||||
#Set cells to a fixed hieght (rows or ranges only).
|
||||
#Set cells to a fixed height (rows or ranges only).
|
||||
[float]$Height,
|
||||
#Hide a row or column (not a range); use -Hidden:$false to unhide.
|
||||
[Switch]$Hidden
|
||||
|
||||
Reference in New Issue
Block a user