Home » excel » Summarize Excel Cells In PowerShell

Summarize Excel Cells In PowerShell

Posted by: admin May 14, 2020 Leave a comment

Questions:

I want to summarize number of cells from a sheet in excel into an array cell. Unfortunately it saves the numbers in the cells as strings and not as the actual sum. Tired to change the cell type to “value” but it seems to be not working.

How shall I get the value from the cell as “double” type?

$EntryAmmount[25] = $IncomingSummary1Worksheet.Cells.Item(69,18).Text + $IncomingSummary1Worksheet.Cells.Item(71,18).Text

In this situation it throws this error (0.00 and 29,453.17 are the values in the cells):

Cannot convert value "0.0029,453.17" to type "System.Double". Error: "Input string was not in a correct format."
At C:...PS.ps1:148 char:5
+     $EntryAmmount[25] = $IncomingSummary1Worksheet.Cells.Item(69, ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvalidCastFromStringToDoubleOrSingle
How to&Answers:

You need to cast the values to double, in your current code the op_addition (+) method is acting on string types and is therefore concatenating them.

Try:

$EntryAmmount[25] = [double]$IncomingSummary1Worksheet.Cells.Item(69,18).Text + [double]$IncomingSummary1Worksheet.Cells.Item(71,18).Text