This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

I created a Windows Powershell script file to fix issues with the Garmin Connect (Web) Weight Export function

Hi, I noticed a few other people had issues with the Garmin Connect Weight Export function so I created a Windows Powershell script file to help format it properly.

I am bit of a newbie with Powershell so pardon any unorthodox code and crude interface.

Once invoked the script will prompt for an input csv file/location followed by a prompt for an output csv file/location and then format and create the updated file.  You can uncomment some of the debugging code at the end of the code to see the finished file in a Powershell Grid-View window.

You can run the code by right-clicking on the .ps1 file in File Explorer and use the "Run with Powershell" option.

Here is the code (save it as a .ps1 file) and please post a reply if you have issues or find errors:

Edit:  Can't seem to post the code.  Only worked if I replied to this post!

  • #---- Start - ConvertGarminWeightCSV.ps1 ----------------------------------------------------------------------------------
    # Login to https://connect.garmin.com/ & Navigate to 'Health Stats > Weight > 1 Year'
    # at the top there is an export, that'll get you the csv.
    # but the csv is in a poor format, with the date on the row above the data - this will reformat it for you
    # also works with new format of multiple readings per day.
    # Formatting includes removing the "lbs" and "%" signs in the numeric data fields and cleans any "--" data in those fields

    #

    #-----------------------------------
    function Get-FileName($initialDirectory) {  
    [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") |     Out-Null

    $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog;
    $OpenFileDialog.initialDirectory = $initialDirectory;
    $OpenFileDialog.filter = "All files (*.*)| *.*";
    $OpenFileDialog.ShowDialog() | Out-Null;
    $OpenFileDialog.filename;
    }

    #-----------------------------------
    Function Save-File ([string]$initialDirectory) {

        [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
        
        $OpenFileDialog = New-Object System.Windows.Forms.SaveFileDialog
        $OpenFileDialog.initialDirectory = $initialDirectory
        #$OpenFileDialog.filter = "CSV (*.csv)| *.csv"
        $OpenFileDialog.FileName = $SaveFileName
        $OpenFileDialog.ShowDialog() | Out-Null

        return $OpenFileDialog.filename

    }
    #-- Get Input and Output filename and locations ---------------------------------

    $initialDirectory = "C:\Users\YOURNAME\Documents";

    Write-Host "Please Select The File To Upload:";
    $SourceFile = Get-FileName -initialDirectory $initialDirectory;
    Write-Host "SourceFile: $($Sourcefile) ";
    $SaveFileName = $sourceFile

    Write-Host "Please Select The File To Save:";
    $DestinationFile = Save-File
    Write-Host "DestinationFile: $($Destinationfile) ";


    #--- Read the CSV input file --------------------------------

    # read the CSV input file in and add a Date column as first column
    $csvin = import-csv $Sourcefile | Select-Object @{Name='Date';Expression={' '}},*

        #-- Start debugging ---------------------------------
        #Get the data in Table-like format
        #$csvin | Format-Table
        #$csvin | Out-Gridview
        #-- End debugging ---------------------------------
     
    # Loop through each row in the CSV and
    foreach ($row in $csvin) {
        # Check if the 'Name' column has a value
        if ($row.Time) {
            if ($row.Weight) {
                $row.Date = $datex
            } else {
                $datex = $row.Time.trim()

               
            }
        }
    }



    #--- Run through the input CSV, make format changes, drop the "date" records and prepare the output CSV file -----------------------
    $deletedrows = 0
    $outputrows = 0
    $inputrows = 0

    $csvout = foreach($row in $csvin) {
        $inputrows = $inputrows + 1
        #--- if this is a row with the time and all of the details then format it and keep it.
        if ($row.Weight) {
            $row.Weight = $row.Weight.SubString(0,$row.Weight.Length-4)
            if ($row.Change) {
                # remove " lbs" from end of string
                $row.Change = $row.Change.SubString(0,$row.Change.Length-4)
            }
            if ($row."Body Fat") {
                # remove " %" from end of string
                $row."Body Fat" = $row."Body Fat".SubString(0,$row."Body Fat".Length-2)
            }
            if ($row."Skeletal Muscle Mass") {
                 if ($row."Skeletal Muscle Mass".trim().Length -eq 2) {
                    # get rid of  the weird "--" in some fields that cause errors
                    $row."Skeletal Muscle Mass" = " "
                 } else {
                    # remove " lbs" from end of string
                    $row."Skeletal Muscle Mass" = $row."Skeletal Muscle Mass".SubString(0,$row."Skeletal Muscle Mass".Length-4)
                 }
            }
            if ($row."Bone Mass") {
                if ($row."Bone Mass".trim().Length -eq 2){
                    # get rid of  the weird "--" in some fields that cause errors
                    $row."Bone Mass" = " "
                } else {
                    # remove " lbs" from end of string
                    $row."Bone Mass" = $row."Bone Mass".SubString(0,$row."Bone Mass".Length-4)
                }
            }
            if ($row."Body Water") {
                # remove " %" from end of string
                $row."Body Water" = $row."Body Water".SubString(0,$row."Body Water".Length-2)
            }
            #--- keep only the records with full details
            $row
            $outputrows = $outputrows + 1
        } else {
        #--- if this is NOT a row with the time and all of the details then delete it but keep count.
            $deletedrows = $deletedrows + 1
        }
    }


    Write-Host "Input Rows      : $($inputrows) ";
    Write-Host "Deleted Rows  - : $($deletedrows) ";
    Write-Host "                  ----- ";
    Write-Host "Output Rows   = : $($outputrows) ";

        #-- Start debugging ---------------------------------
        #Get the data in Table-like format
        #$csvout | Format-Table
        #$csvout | Out-Gridview
        #-- End debugging ---------------------------------

    #-- Output updated CSV file to selected location
     
    $csvout | Export-Csv $DestinationFile -NoTypeInformation

    #--- end

    #---- End - ConvertGarminWeightCSV.ps1 ---------------------------------------------------------------------------------------------




  • Fix for converting to yyyy-mm-dd date.  Can't seem to edit/update post above (?)


        # Check if the 'Name' column has a value
        if ($row.Time) {
            if ($row.Weight) {
                $row.Date = $datex.ToString("yyyy/MM/dd")
            } else {
                $datex = Get-Date $row.Time.trim()
            }
        }