UMN-Google.psm1

###############
# Module for interacting with Google API
# More details found at https://developers.google.com/sheets/ and https://developers.google.com/drive/
#
###############

###
# Copyright 2017 University of Minnesota, Office of Information Technology

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with Foobar. If not, see <http://www.gnu.org/licenses/>.
###

#region Dependancies

function ConvertTo-Base64URL
{
    <#
        .Synopsis
            convert text or byte array to URL friendly Base64

        .DESCRIPTION
            Used for preparing the JWT token to a proper format.
        
        .PARAMETER bytes
            The bytes to be converted

        .PARAMETER text
            The text to be converted

        .EXAMPLE
            ConvertTo-Base64URL -text $headerJSON

        .EXAMPLE
            ConvertTo-Base64URL -Bytes $rsa.SignData($toSign,"SHA256")
    #>

    param
    (
        [Parameter(ParameterSetName='Bytes')]
        [System.Byte[]]$Bytes,
        
        [Parameter(ParameterSetName='String')]
        [string]$text
    )

    if($Bytes){$base = $Bytes}
    else{$base =  [System.Text.Encoding]::UTF8.GetBytes($text)}
    $base64Url = [System.Convert]::ToBase64String($base)
    $base64Url = $base64Url.Split('=')[0]
    $base64Url = $base64Url.Replace('+', '-')
    $base64Url = $base64Url.Replace('/', '_')
    $base64Url
}

#endregion

#region oAuth 2.0

function Get-GOAuthTokenService
{
    <#
        .Synopsis
            Get google auth 2.0 token for a service account

        .DESCRIPTION
            This is used in server-server OAuth token generation
        
        .PARAMETER certPath
            Local or network path to .p12 used to sign the JWT token

        .PARAMETER certPswd
            Password to access the private key in the .p12

        .PARAMETER iss
            This is the Google Service account address

        .PARAMATER scope
            The API scopes to be included in the request. Space delimited, "https://www.googleapis.com/auth/spreadsheets https://www.googleapis.com/auth/drive"
                
        .EXAMPLE
            Get-GOAuthTokenService -scope "https://www.googleapis.com/auth/spreadsheets" -certPath "C:\users\$env:username\Desktop\googleSheets.p12" -certPswd 'notasecret' -iss "serviceAccount@googleProjectName.iam.gserviceaccount.com"

    #>

    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory)]
        [string]$certPath,

        [Parameter(Mandatory)]
        [string]$certPswd,

        [Parameter(Mandatory)]
        [string]$iss,
        
        [Parameter(Mandatory)]
        [string]$scope
    )

    Begin
    {
        # build JWT header
        $headerJSON = [Ordered]@{
            alg = "RS256"
            typ = "JWT"
        } | ConvertTo-Json -Compress
        $headerBase64 = ConvertTo-Base64URL -text $headerJSON
    }
    Process
    {        
        # Build claims for JWT
        $now = (Get-Date).ToUniversalTime()
        $iat = [Math]::Floor([decimal](Get-Date($now) -UFormat "%s"))
        $exp = [Math]::Floor([decimal](Get-Date($now.AddMinutes(59)) -UFormat "%s")) 
        $aud = "https://www.googleapis.com/oauth2/v4/token"
        $claimsJSON = [Ordered]@{
            iss = $iss
            scope = $scope
            aud = $aud
            exp = $exp
            iat = $iat
        } | ConvertTo-Json -Compress

        $claimsBase64 = ConvertTo-Base64URL -text $claimsJSON

        ################# Create JWT
        # Prep JWT certificate signing
        $googleCert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($certPath, $certPswd,[System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::Exportable ) 
        $rsaPrivate = $googleCert.PrivateKey 
        $rsa = New-Object System.Security.Cryptography.RSACryptoServiceProvider 
        $null = $rsa.ImportParameters($rsaPrivate.ExportParameters($true))
        
        # Signature is our base64urlencoded header and claims, delimited by a period.
        $toSign = [System.Text.Encoding]::UTF8.GetBytes($headerBase64 + "." + $claimsBase64)
        $signature = ConvertTo-Base64URL -Bytes $rsa.SignData($toSign,"SHA256") ## this needs to be converted back to regular text
        
        # Build request
        $jwt = $headerBase64 + "." + $claimsBase64 + "." + $signature
        $fields = 'grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Ajwt-bearer&assertion='+$jwt

        # Fetch token
        $response = Invoke-RestMethod -Uri "https://www.googleapis.com/oauth2/v4/token" -Method Post -Body $fields -ContentType "application/x-www-form-urlencoded"

    }
    End
    {
        return $response.access_token
    }
}

function Get-GOAuthTokenUser
{
    <#
        .Synopsis
            Get Valid OAuth Token.
        
        .DESCRIPTION
            The access token is good for an hour, the refresh token is mostly permanent and can be used to get a new access token without having to reauthenticate.
        
        .PARAMETER appKey
            The google project App Key

        .PARAMETER appSecret
            The google project application secret

        .PARAMETER projectID
            The google project ID

        .PARAMETER redirectUri
            An https project redirect. Can be anything as long as https

        .PARAMETER refreshToken
            A refresh token if refreshing

        .PARAMATER scope
            The API scopes to be included in the request. Space delimited, "https://www.googleapis.com/auth/spreadsheets https://www.googleapis.com/auth/drive"
        
        .EXAMPLE
            Get-GOAuthTokenUser -appKey $appKey -appSecret $appSecret -projectID $projectID -redirectUri $redirectUri -scope $scope
                
        .EXAMPLE
            Get-GOAuthTokenUser -appKey $appKey -appSecret $appSecret -projectID $projectID -redirectUri $redirectUri -scope $scope -refreshToken $refreshToken
            
        .NOTES
            Requires GUI with Internet Explorer to get first token.
    #>

    [CmdletBinding()]
    [OutputType([array])]
    Param
    (
        [Parameter(Mandatory)]
        [string]$appKey,

        [Parameter(Mandatory)]
        [string]$appSecret,
        
        [Parameter(Mandatory)]
        [string]$projectID,
        
        [Parameter(Mandatory)]
        [string]$redirectUri,

        [string]$refreshToken,

        [Parameter(Mandatory)]
        [string]$scope

    )

    Begin
    {
        $requestUri = "https://accounts.google.com/o/oauth2/token"
    }
    Process
    {

        if(!($refreshToken))
        { 
            ### Get the authorization code - IE Popup and user interaction section
            $auth_string = "https://accounts.google.com/o/oauth2/auth?scope=$scope&response_type=code&redirect_uri=$redirectUri&client_id=$appKey&access_type=offline&approval_prompt=force"
            $ie = New-Object -comObject InternetExplorer.Application
            $ie.visible = $true
            $null = $ie.navigate($auth_string)

            #Wait for user interaction in IE, manual approval
            do{Start-Sleep 1}until($ie.LocationURL -match 'code=([^&]*)')
            $null = $ie.LocationURL -match 'code=([^&]*)'
            $authorizationCode = $matches[1]
            $null = $ie.Quit()

            # exchange the authorization code for a refresh token and access token
            $requestBody = "code=$authorizationCode&client_id=$appKey&client_secret=$appSecret&grant_type=authorization_code&redirect_uri=$redirectUri"
 
            $response = Invoke-RestMethod -Method Post -Uri $requestUri -ContentType "application/x-www-form-urlencoded" -Body $requestBody

            $props = @{
                accessToken = $response.access_token
                refreshToken = $response.refresh_token
            }
        }

        else
        { 
            # Exchange the refresh token for new tokens
            $requestBody = "refresh_token=$refreshToken&client_id=$appKey&client_secret=$appSecret&grant_type=refresh_token"
 
            $response = Invoke-RestMethod -Method Post -Uri $requestUri -ContentType "application/x-www-form-urlencoded" -Body $requestBody
            $props = @{
                accessToken = $response.access_token
                refreshToken = $refreshToken
            }
        }
        
    }
    End
    {
        return new-object psobject -Property $props
    }
}

#endregion

#region Permissions for Google Drive files

function Get-GFilePermissions
{
    <#
        .Synopsis
            Get Permissions on Google Drive File

        .DESCRIPTION
            Get Permission ID list on Google File

        .PARAMETER accessToken
            OAuth Access Token for authorization.
                  
        .PARAMETER fileID
            The fileID to query. This is returned when a new file is created.

        .EXAMPLE
            Get-GFilePermissions -fileID 'String of File ID'
    #>

    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory)]
        [string]$accessToken,

        #[Alias("spreadSheetID")]
        [Parameter(Mandatory)]
        [string]$fileID
    )

    Begin
    {
        $uri = "https://www.googleapis.com/drive/v3/files/$fileID/permissions"
        $headers = @{"Authorization"="Bearer $accessToken"}
    }

    Process
    {
        Invoke-RestMethod -Method Get -Uri $uri -Headers $headers
    }
    End{}
}

function Move-GFile
{
    <#
        .Synopsis
            Change parent folder metadata

        .DESCRIPTION
            A function to change parent folder metadata of a file.

        .PARAMETER accessToken
            OAuth Access Token for authorization.
                  
        .PARAMETER fileID
            The fileID to move.

        .PARAMETER folderID
            The fileID of the new parent folder.

        .PARAMETER parentFolderID
            The fileID of the parentFolder. Optional parameter. root (My Drive) is assumed if not specified.

        .EXAMPLE
            MoveGFile -fileID 'String of File ID' -folderID 'String of folder's File ID'
    #>

    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory)]
        [string]$accessToken,

        #[Alias("spreadSheetID")]
        [Parameter(Mandatory)]
        [string]$fileID,

        [Parameter(Mandatory)]
        [string]$folderID,

        [string]$parentFolderID='root'
    )

    Begin
    {
        $uriAdd = "https://www.googleapis.com/drive/v3/files/$fileID"+"?removeParents=$parentFolderID"
        $uriRemove = "https://www.googleapis.com/drive/v3/files/$fileID"+"?addParents=$folderID"
        $headers = @{"Authorization"="Bearer $accessToken"}
    }

    Process
    {
        Invoke-RestMethod -Method patch -Uri $uriAdd -Headers $headers

        Invoke-RestMethod -Method patch -Uri $uriRemove -Headers $headers
    }
    End{}
}

function Remove-GFilePermissions
{
    <#
        .Synopsis
            Remove Permissions on Google Drive File

        .DESCRIPTION
            Remove Permission ID list on Google File

        .PARAMETER accessToken
            OAuth Access Token for authorization.
                  
        .PARAMETER fileID
            The fileID to query. This is returned when a new file is created.

        .PARAMETER permissionsID
            The permission ID to be removed. See Get-GFilePermissions

        .EXAMPLE
            Remove-GFilePermissions -fileID 'String of File ID' -accessToken $accessToken -permissionID 'ID of the permission'

        .NOTES
            A successfull removal returns no body data.
    #>

    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory)]
        [string]$accessToken,

        #[Alias("spreadSheetID")]
        [Parameter(Mandatory)]
        [string]$fileID,
        
        [Parameter(Mandatory)]
        [string]$permissionID

    )

    Begin
    {
        $uri = "https://www.googleapis.com/drive/v3/files/$fileId/permissions/$permissionId"
        $headers = @{"Authorization"="Bearer $accessToken"}
    }

    Process
    {
        Invoke-RestMethod -Method Delete -Uri $uri -Headers $headers
    }
    End{}
}

function Set-GFilePermissions
{
    <#
        .Synopsis
            Set Permissions on Google File

        .DESCRIPTION
            For use with any google drive file ID

        .PARAMETER accessToken
            access token used for authentication. Get from Get-GOAuthTokenUser or Get-GOAuthTokenService

        .PARAMETER emailAddress
            Email address of the user or group to grant permissions to
        
        .PARAMETER fileID
            The fileID to apply permissions to.

        .PARAMETER role
            Role to assign, select from 'writer','reader','commenter'

        .PARAMETER sendNotificationEmail
            Boolean response on sending email notification.

        .PARAMETER type
            This refers to the emailAddress, is it a user or a group

        .EXAMPLE
            set-GFilePermissions -emailAddress 'user@email.com' -role writer -sheetID $sheetID -type user

        .NOTES
            Requires drive and drive.file API scope.
    #>

    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory)]
        [string]$accessToken,
        
        [Parameter(Mandatory)]
        [string]$emailAddress,

        #[Alias("spreadhSheetID")]
        [Parameter(Mandatory)]
        [string]$fileID,
        
        [ValidateSet('writer','reader','commenter')]
        [string]$role = "writer",
        
        [ValidateSet($true,$false)]
        [boolean]$sendNotificationEmail = $false,

        [ValidateSet('user','group')]
        [string]$type
    )

    Begin{
        $json = @{emailAddress=$emailAddress;type=$type;role=$role} | ConvertTo-Json
        $ContentType = "application/json"
        $uri = "https://www.googleapis.com/drive/v3/files/$fileID/permissions/?sendNotificationEmail=$sendNotificationEmail"
        $headers = @{"Authorization"="Bearer $accessToken"}
    }
    Process
    {
        Invoke-RestMethod -Method post -Uri $uri -Body $json -ContentType $ContentType -Headers $headers
    }
    End{}
}

function Update-GFilePermissions
{
    <#
        .Synopsis
            Update Permissions on Google File

        .DESCRIPTION
            Update Permissions on Google File
     
        .PARAMETER accessToken
            access token used for authentication. Get from Get-GOAuthTokenUser or Get-GOAuthTokenService

        .PARAMETER fileID
            The sheetID to apply permissions to. This is returned when a new sheet is created or use Get-GSheetID

        .PARAMETER permissionID
            The permission ID of the entiry with permissions. Sett Get-GFilePermissions to get a lsit
        
        .PARAMETER role
            Role to assign, select from 'writer','reader','commenter','Owner','Organizer'
        
        .PARAMETER supportTeamDrives
            Boolean for TeamDrive Support

        .PARAMETER transferOwnership
            Update ownership of file to permission ID

        .EXAMPLE
            Update-GFilePermissions -emailAddress 'user@email.com' -role writer -fileID $sheetID -permissionID 'ID of the permission'

        .NOTES
            This is usefull for changing ownership. You cannot change ownership from non-domain to domain.
    #>

    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory)]
        [string]$accessToken,
        
        #[Alias("spreadSheetID")]
        [Parameter(Mandatory)]
        [string]$fileID,
        
        [Parameter(Mandatory)]
        [string]$permissionID,

        [ValidateSet('writer','reader','commenter','owner','organizer')]
        [string]$role = "writer",

        [ValidateSet($true,$false)]
        [string]$supportTeamDrives = $false,

        [ValidateSet($true,$false)]
        [string]$transferOwnership = $false
    )

    Begin{
        $json = @{role=$role} | ConvertTo-Json
        $ContentType = "application/json"
        $uri = "https://www.googleapis.com/drive/v3/files/$fileID/permissions/$permissionID/?transferOwnership=$transferOwnership"
        $headers = @{"Authorization"="Bearer $accessToken"}
    }
    Process
    {

        Invoke-RestMethod -Method Patch -Uri $uri -Body $json -ContentType $ContentType -Headers $headers
    }
    End{}
}

#endregion

#region Spread Sheet API Functions

function Add-GSheetSheet
{
    <#
        .Synopsis
            Add named sheets to an existing spreadSheet file.
    
        .DESCRIPTION
            This function will add a specified sheet name to a google spreadsheet.

        .PARAMETER accessToken
            access token used for authentication. Get from Get-GOAuthTokenUser or Get-GOAuthTokenService
    
        .PARAMETER sheetName
            Name to apply to new sheet
            
        .PARAMETER spreadSheetID
            ID for the target Spreadsheet. This is returned when a new sheet is created or use Get-GSheetSpreadSheetID

        
        .EXAMPLE
            Add-GSheetSheet -accessToken $accessToken -sheetName 'NewName' -spreadSheetID $spreadSheetID
    
    #>

    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory)]
        [string]$accessToken,
        
        [Parameter(Mandatory)]
        [string]$sheetName,

        [Parameter(Mandatory)]
        [string]$spreadSheetID


    )

    Begin
    {
        $properties = @{requests=@(@{addSheet=@{properties=@{title=$sheetName}}})} |convertto-json -Depth 10
    }

    Process
    {
        $suffix = "$spreadSheetID" + ":batchUpdate"
        $uri = "https://sheets.googleapis.com/v4/spreadsheets/$suffix"
        Invoke-RestMethod -Method Post -Uri $uri -Body $properties -ContentType 'application/json' -Headers @{"Authorization"="Bearer $accessToken"}
    }
    End{}
}

function Clear-GSheetSheet
{
    <#
        .Synopsis
            Clear all data and leave formatting intact for a sheet from a spreadsheet based on sheetID

        .DESCRIPTION
            This function will delete data from a sheet

        .PARAMETER accessToken
            access token used for authentication. Get from Get-GOAuthTokenUser or Get-GOAuthTokenService

        .PARAMETER sheetName
            Name of sheet to clear

        .PARAMETER spreadSheetID
            ID for the target Spreadsheet. This is returned when a new sheet is created or use Get-GSheetSpreadSheetID

        .EXAMPLE
            $pageID = 0 ## using pageID to differentiate from sheetID --
            In this case, index 0 is the actual sheetID per the API and will be cleared.

            $sheetID = ## the id number of the file/spreadsheet

            clear-gsheet -pageID $pageID -sheetID $sheetID -accessToken $accessToken

        
    #>

    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory)]
        [string]$accessToken,
        
        [Parameter(Mandatory)]
        [string]$sheetName,

        [Parameter(Mandatory)]
        [string]$spreadSheetID

    )

    Begin{}
    Process
    {
        $sheetID = Get-GSheetSheetID -accessToken $accessToken -spreadSheetID $spreadSheetID -sheetName $sheetName
        $properties = @{requests=@(@{updateCells=@{range=@{sheetId=$sheetID};fields="userEnteredValue"}})} |ConvertTo-Json -Depth 10
        $suffix = "$spreadSheetID" + ":batchUpdate"
        $uri = "https://sheets.googleapis.com/v4/spreadsheets/$suffix"
        Invoke-RestMethod -Method Post -Uri $uri -Body $properties -ContentType 'application/json' -Headers @{"Authorization"="Bearer $accessToken"}
    }
    End{}
}

function Get-GSheetData
{
    <#
        .Synopsis
            Basic function for retrieving data from a specific Sheet in a Google SpreadSheet.

        .DESCRIPTION
            Basic function for retrieving data from a specific Sheet in a Google SpreadSheet.

        .PARAMETER accessToken
            access token used for authentication. Get from Get-GOAuthTokenUser or Get-GOAuthTokenService
        
        .PARAMETER cell
            Required switch for getting all data, or a subset of cells.

        .PARAMETER rangeA1
            Range in A1 notation https://msdn.microsoft.com/en-us/library/bb211395(v=office.12).aspx. The dimensions of the $values you put in MUST fit within this range
        
        .PARAMETER sheetName
            Name of sheet to data from

        .PARAMETER spreadSheetID
            ID for the target Spreadsheet. This is returned when a new sheet is created or use Get-GSheetSpreadSheetID

        .PARAMETER valueRenderOption
            How the data is renderd. Switch option from formatted to unformatted data or 'formula'

        .EXAMPLE
            Get-GSheetData -accessToken $accessToken -cell 'AllData' -sheetName 'Sheet1' -spreadSheetID $spreadSheetID

        .EXAMPLE
            Get-GSheetData -accessToken $accessToken -cell 'Range' -rangeA1 'A0:F77' -sheetName 'Sheet1' -spreadSheetID $spreadSheetID
        
    #>

    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory)]
        [string]$accessToken,
        
        [Parameter(Mandatory)]
        [ValidateSet('AllData','Range')]
        [string]$cell,

        [string]$rangeA1,
        
        [Parameter(Mandatory)]
        [string]$sheetName,

        [Parameter(Mandatory)]
        [string]$spreadSheetID,

        [Parameter()]
        [ValidateSet('FORMATTED_VALUE', 'UNFORMATTED_VALUE', 'FORMULA')]
        [string]$valueRenderOption = "FORMATTED_VALUE"

    )

    Begin{}
    Process
    {
        $uri = "https://sheets.googleapis.com/v4/spreadsheets/$spreadSheetID/values/$sheetName"

        if($cell -eq "Range") {
            $uri += "!$rangeA1"
        }

        $uri += "?valueRenderOption=$valueRenderOption"

        $result = Invoke-RestMethod -Method GET -Uri $uri -Headers @{"Authorization"="Bearer $accessToken"}
        
        # Formatting the returned data
        $sheet = $result.values
        $Rows = $sheet.Count
        $Columns = $sheet[0].Count
        $HeaderRow = 0
        $Header = $sheet[0]
        foreach ($Row in (($HeaderRow + 1)..($Rows-1))) { 
            $h = [Ordered]@{}
            foreach ($Column in 0..($Columns-1)) {
                if ($sheet[0][$Column].Length -gt 0) {
                    $Name = $Header[$Column]
                    $h.$Name = $Sheet[$Row][$Column]
                }
            }
            [PSCustomObject]$h
        }
    }
    End{}
}

function Get-GSheetSheetID
{
    <#
        .Synopsis
            Get ID of specific sheet in a Spreadsheet

        .DESCRIPTION
         Get ID of specific sheet in a Spreadsheet

        .PARAMETER accessToken
            access token used for authentication. Get from Get-GOAuthTokenUser or Get-GOAuthTokenService
        
        .PARAMETER sheetName
            The name of the sheet

        .PARAMETER spreadSheetID
            ID for the target Spreadsheet. This is returned when a new sheet is created or use Get-GSheetSpreadSheetID

        .EXAMPLE
            Get-GSheetSheetID -accessToken $accessToken -sheetName 'Sheet1' -spreadSheetID $spreadSheetID
    #>

    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory)]
        [string]$accessToken,

        [Parameter(Mandatory)]
        [string]$sheetName,

        [Parameter(Mandatory)]
        [string]$spreadSheetID
    )

    Begin{}
    Process
    {
        $spreadSheet = Get-GSheetSpreadSheetProperties -spreadSheetID $spreadSheetID -accessToken $accessToken
        ($spreadSheet.sheets.properties | Where-Object {$_.title -eq $sheetName}).sheetID
    }
    End{}
}

function Get-GSheetSpreadSheetID
{
    <#
        .Synopsis
            Get a spreadsheet ID.

        .DESCRIPTION
            Provide a case sensative file name to the function to get back the sheetID used in many other API calls.
            mimeTymes are split out to only retrieve spreadSheet IDs (no folders or other files)

        .PARAMETER accessToken
            access token used for authentication. Get from Get-GOAuthTokenUser or Get-GOAuthTokenService

        .PARAMETER fileName
            Name of file to retrive ID for. Case sensitive
        
        .EXAMPLE
            Get-GSheetSpreadSheetID -accessToken $accessToken -fileName 'Name of some file'
    #>

    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory)]
        [string]$accessToken,
        [Parameter(Mandatory)]

        [Alias("spreadSheetName")] 
        [string]$fileName
    )

    Begin{}
    Process
    {
        $uri = "https://www.googleapis.com/drive/v3/files?q=name%3D'$fileName'"
        $spreadSheetID = (((Invoke-RestMethod -Method get -Uri $uri -Headers @{"Authorization"="Bearer $accessToken"}).files) | where {$_.mimetype -eq "application/vnd.google-apps.spreadsheet"}).id
        
        # Logic on multiple IDs being returned
        If ($spreadSheetID.count -eq 0){Write-Warning "There are no files matching the name $fileName"}
        If ($spreadSheetID.count -gt 1){Write-Warning "There are $($spreadSheetID.Count) files matching the provided name. Please investigate the following sheet IDs to verify which file you want.";return($spreadSheetID)}
        Else{return($spreadSheetID)}
    }
    End{}
}

function Get-GSheetSpreadSheetProperties
{
    <#
        .Synopsis
            Get the properties of a SpreadSheet

        .DESCRIPTION
            Get all properties of a SpreadSheet

        .PARAMETER spreadSheetID
            ID for the target Spreadsheet. This is returned when a new sheet is created or use Get-GSheetSpreadSheetID

        .PARAMETER accessToken
            access token used for authentication. Get from Get-GOAuthTokenUser or Get-GOAuthTokenService

        .EXAMPLE
    #>

    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory)]
        [string]$accessToken,

        [Parameter(Mandatory)]
        [string]$spreadSheetID
    )

    Begin{}
    Process
    {
        $uri = "https://sheets.googleapis.com/v4/spreadsheets/$spreadSheetID"
        Invoke-RestMethod -Method GET -Uri $uri -Headers @{"Authorization"="Bearer $accessToken"}
    }
    End{}
}

function Move-GSheetData
{
    <#
        .Synopsis
            Move data around between sheets in a spreadSheet.

        .DESCRIPTION
            This is a cut and paste between sheets in a spreadsheet.
            The function will find the row index based on search criteria, and copy/paste between the sheets provided.

        .PARAMETER accessToken
            oAuth access token used for authentication. Get from Get-GOAuthTokenUser or Get-GOAuthTokenService

        .PARAMETER columnKey
            Row 0 column value. A key to search for data by. $columnKey = 'Column header'

        .PARAMETER currentSheetName
            Name of sheet to be searched, and copied from.

        .PARAMETER newSheetName
            Name of destination sheet data is to be written to.

        .PARAMETER query
            Value to be queried for in specified column (see columnKey) $query = 'Cell Content'
        
        .PARAMETER spreadSheetID
            ID for the target Spreadsheet. This is returned when a new sheet is created or use Get-GSheetSpreadSheetID

        .EXAMPLE
            Move-GSheetData -accessToken $accessToken -columnKey 'Column Header -destinationSheetName 'New Sheet!' -query 'Cell Content' -sourceSheetName 'Old Sheet' -spreadSheetID $spreadSheetID
    #>


    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory)]
        [string]$accessToken,

        [Parameter(Mandatory)]
        [string]$columnKey,

        [Parameter(Mandatory)]
        [string]$destinationSheetName,
        
        [Parameter(Mandatory)]
        [string]$query,
        
        [Parameter(Mandatory)]
        [string]$sourceSheetName,

        [Parameter(Mandatory)]
        [string]$spreadSheetID
    )

    Begin{}

    Process
    {
        ## Query all data from sheet
        $data = Get-GSheetData -spreadSheetID $spreadSheetID -accessToken $accessToken -sheetName $sourceSheetName -cell AllData
        $destinationData = Get-GSheetData -spreadSheetID $spreadSheetID -accessToken $accessToken -sheetName $destinationSheetName -cell AllData

        ## Get row query belongs to
        $Index = (0..($data.count -1) | where {$Data[$_].$columnKey -eq $query})
        
        ## Sanity Check - is this the data?
        if (-not $Index) {
            write-Warning "$Query in $columnKey does not exist"
            return $null
            }

        Else {
        $rowIndex = $index[0] + 2    
        $startRow = $Index[0] + 1
        $destinationRow = ($destinationData).count + 2
        $destinationStartRow = ($destinationData).count + 1
        }

        ## Get sheet index ID numbers
        $allSheetProperties = (Get-GSheetSpreadSheetProperties -spreadSheetID $spreadSheetID -accessToken $accessToken).sheets.properties

        $srcSheetIndex = ($allSheetProperties | where {$_.title -eq $sourceSheetName}).sheetID
        $dstSheetIndex = ($allSheetProperties | where {$_.title -eq $destinationSheetName}).sheetID                                

        $method = 'POST'
        $uri = "https://sheets.googleapis.com/v4/spreadsheets/$spreadSheetID"+":batchUpdate"
        $ContentType = "application/json"
        
   
        ## cutPaste row to row
        $values = @{"cutPaste"=@{"source"=@{"sheetId"=$srcSheetIndex;"startRowIndex"=$startRow;"endRowIndex"=$rowIndex};"destination"=@{"sheetId"=$dstSheetIndex;"rowIndex"=$destinationRow};"pasteType"="PASTE_NORMAL"}}
        $JSON = @{"requests"=$values} |ConvertTo-Json -Depth 20
            
        
        
        Invoke-RestMethod -Method $method -Uri $uri -Body $json -ContentType $ContentType -Headers @{"Authorization"="Bearer $accessToken"}
        
    }
    
    End{}
}

function New-GSheetSpreadSheet
{
    <#
        .Synopsis
            Create a new Google SpreadSheet.
        
        .DESCRIPTION
            Create a new Google SpreadSheet.

        .PARAMETER accessToken
            access token used for authentication. Get from Get-GOAuthTokenUser or Get-GOAuthTokenService
        
        .PARAMETER properties
            Alternatively, the properties that can be set are extensive. Cell color, formatting etc. If you use this you MUST include @{properties=@{title='mY sheet'}} |convertto-json
            at a minimum. More details at https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/create

        .PARAMETER title
            Use this in the simplest case to just create a new sheet with a Title/name

        .EXAMPLE
            Create-GSheet -properties $properties -accessToken $accessToken

        .EXAMPLE
            create-GSheet -title 'My sheet' -accessToken $accessToken

    #>

    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory)]
        [string]$accessToken,

        [Parameter(ParameterSetName='properties')]
        [array]$properties,

        [Parameter(ParameterSetName='title')]
        [string]$title
    )

    Begin
    {
        If (!$properties)
            {
                $properties = @{properties=@{title=$title}} |convertto-json
            }

        $uri = "https://sheets.googleapis.com/v4/spreadsheets"
    }

    Process
    {
        Invoke-RestMethod -Method Post -Uri $uri -Body $properties -ContentType "application/json" -Headers @{"Authorization"="Bearer $accessToken"}
    }

    End{}
}

function Remove-GSheetSheet
{
    <#
        .Synopsis
            Removes a sheet from a spreadsheet based on sheetID

        .DESCRIPTION
            This function will delete an individual sheet.

        .PARAMETER accessToken
            access token used for authentication. Get from Get-GOAuthTokenUser or Get-GOAuthTokenService

        .PARAMETER sheetName
            Name of sheet to delete

        .PARAMETER spreadSheetID
            ID for the target Spreadsheet. This is returned when a new sheet is created or use Get-GSheetSpreadSheetID

        .EXAMPLE
            Remove-GSheetSheet -accessToken $accessToken -sheetName 'Name to delete' -spreadSheetID $spreadSheetID
        
    #>

    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory)]
        [string]$accessToken,

        [Parameter(Mandatory)]
        [string]$sheetName,

        [Parameter(Mandatory)]
        [string]$spreadSheetID
    )

    Begin{}
    Process
    {
        $sheetID = Get-GSheetSheetID -accessToken $accessToken -sheetName $sheetName -spreadSheetID $spreadSheetID
        $properties = @{requests=@(@{deleteSheet=@{sheetId=$sheetID}})} |convertto-json -Depth 10
        $suffix = "$spreadSheetID" + ":batchUpdate"
        $uri = "https://sheets.googleapis.com/v4/spreadsheets/$suffix"
        $data = Invoke-RestMethod -Method Post -Uri $uri -Body $properties -ContentType "application/json" -Headers @{"Authorization"="Bearer $accessToken"}
    }
    End
    {
    return([array]$data)
    }
}

function Remove-GSheetSpreadSheet
{
    <#
        .Synopsis
            Delete a SpreadSheet

        .DESCRIPTION
            Uses the google File Drive API to delete a file.

        .PARAMETER accessToken
            access token used for authentication. Get from Get-GOAuthTokenUser or Get-GOAuthTokenService

        .PARAMETER fileID
            ID for the target file/spreadSheet. This is returned when a new sheet is created or use Get-GSheetSpreadSheetID

        .EXAMPLE
            Remove-GSheetSpreadSheet -accessToken $accessToken -spreadSheetID $spreadSheetID
    #>

    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory)]
        [string]$accessToken,

        #[Alias("spreadSheetID")]
        [Parameter(Mandatory)]
        [string]$fileID
    )

    Begin{}
    Process
    {
        $uri = "https://www.googleapis.com/drive/v3/files/$fileID"
        Invoke-RestMethod -Method Delete -Uri $uri -Headers @{"Authorization"="Bearer $accessToken"}
    }
    End{}
}

function Set-GSheetColumnWidth
{
    <#
        .Synopsis
            Set the width of a column on a sheet

        .DESCRIPTION
            This function calls the bulk update method to set column dimensions to 'autoResize'.

        .PARAMETER accessToken
            access token used for authentication. Get from Get-GOAuthTokenUser or Get-GOAuthTokenService

        .PARAMETER numberOfColumns
            An optional parameter to specify how many columns to autosize. Default to 26

        .PARAMETER sheetName
            Name of sheet in spreadSheet

        .PARAMETER spreadSheetID
            ID for the target Spreadsheet. This is returned when a new sheet is created or use Get-GSheetSpreadSheetID

        .EXAMPLE
            Set-GSheetColumnWidth -spreadSheetID $id -sheetName 'Sheet1' -accessToken $token -numberOfColumns ($property.count)
        
    #>

    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory)]
        [string]$accessToken,

        [string]$numberOfColumns = '26',
        
        [Parameter(Mandatory)]
        [string]$sheetName,

        [Parameter(Mandatory)]
        [string]$spreadSheetID
    )

    Begin
    {
        $sheetID = Get-GSheetSheetID -accessToken $accessToken -spreadSheetID $spreadSheetID -sheetName $sheetName
        $json = @{requests=@(@{autoResizeDimensions=@{dimensions=@{sheetId=$sheetID;dimension='COLUMNS';startIndex='0';endIndex='26'}}})} |ConvertTo-Json -Depth 20
        $suffix = "$spreadSheetID" + ":batchUpdate"
        $uri = "https://sheets.googleapis.com/v4/spreadsheets/$suffix"
    }

    Process
    {
        Invoke-RestMethod -Method Post -Uri $uri -Body $json -ContentType "application/json" -Headers @{"Authorization"="Bearer $accessToken"}
    }
    
    End{}
}

function Set-GSheetData
{
    <#
        .Synopsis
            Set values in sheet in specific cell locations or append data to a sheet

        .DESCRIPTION
            Set json data values on a sheet in specific cell locations or append data to a sheet

        .PARAMETER accessToken
            access token used for authentication. Get from Get-GOAuthTokenUser or Get-GOAuthTokenService

        .PARAMETER append
            Switch option to append data. See rangeA1 if not appending

        .PARAMETER rangeA1
            Range in A1 notation https://msdn.microsoft.com/en-us/library/bb211395(v=office.12).aspx . The dimensions of the $values you put in MUST fit within this range

        .PARAMETER sheetName
            Name of sheet to set data in

        .PARAMETER spreadSheetID
            ID for the target Spreadsheet. This is returned when a new sheet is created or use Get-GSheetSpreadSheetID

        .PARAMETER valueInputOption
            Default to RAW. Optionally, you can specify if you want it processed as a formula and so forth.

        .PARAMETER values
            The values to write to the sheet. This should be an array list. Each list array represents one ROW on the sheet.

        .EXAMPLE
            Set-GSheetData -accessToken $accessToken -rangeA1 'A1:B2' -sheetName 'My Sheet' -spreadSheetID $spreadSheetID -values @(@("a","b"),@("c","D"))

        .EXAMPLE
            Set-GSheetData -accessToken $accessToken -append 'Append'-sheetName 'My Sheet' -spreadSheetID $spreadSheetID -values $arrayValues

    #>

    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory)]
        [string]$accessToken,

        [Parameter(ParameterSetName='Append')]
        [switch]$append,

        [Parameter(ParameterSetName='set')]
        [string]$rangeA1,

        [Parameter(Mandatory)]
        [string]$sheetName,

        [Parameter(Mandatory)]
        [string]$spreadSheetID,

        [string]$valueInputOption = 'RAW',

        [Parameter(Mandatory)]
        [System.Collections.ArrayList]$values
)

    Begin
    {
        if ($append)
            {
                $method = 'POST'
                $uri = "https://sheets.googleapis.com/v4/spreadsheets/$spreadSheetID/values/$sheetName"+":append?valueInputOption=$valueInputOption"
            }
        else
            {
                $method = 'PUT'
                $uri = "https://sheets.googleapis.com/v4/spreadsheets/$spreadSheetID/values/$sheetName!$rangeA1"+"?valueInputOption=$valueInputOption"
            }
    }

    Process
    {
        $json = @{values=$values} | ConvertTo-Json
        Invoke-RestMethod -Method $method -Uri $uri -Body $json -ContentType "application/json" -Headers @{"Authorization"="Bearer $accessToken"}     
    }

    End{}
}

#endregion

Export-ModuleMember -Function *
# SIG # Begin signature block
# MIIaxgYJKoZIhvcNAQcCoIIatzCCGrMCAQExCzAJBgUrDgMCGgUAMGkGCisGAQQB
# gjcCAQSgWzBZMDQGCisGAQQBgjcCAR4wJgIDAQAABBAfzDtgWUsITrck0sYpfvNR
# AgEAAgEAAgEAAgEAAgEAMCEwCQYFKw4DAhoFAAQUig38knHuxn9n1NIURoo0h0dB
# fnigghW3MIIEmTCCA4GgAwIBAgIPFojwOSVeY45pFDkH5jMLMA0GCSqGSIb3DQEB
# BQUAMIGVMQswCQYDVQQGEwJVUzELMAkGA1UECBMCVVQxFzAVBgNVBAcTDlNhbHQg
# TGFrZSBDaXR5MR4wHAYDVQQKExVUaGUgVVNFUlRSVVNUIE5ldHdvcmsxITAfBgNV
# BAsTGGh0dHA6Ly93d3cudXNlcnRydXN0LmNvbTEdMBsGA1UEAxMUVVROLVVTRVJG
# aXJzdC1PYmplY3QwHhcNMTUxMjMxMDAwMDAwWhcNMTkwNzA5MTg0MDM2WjCBhDEL
# MAkGA1UEBhMCR0IxGzAZBgNVBAgTEkdyZWF0ZXIgTWFuY2hlc3RlcjEQMA4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