Skip to content

Commit

Permalink
Merge pull request #23 from elhaus/GSheet
Browse files Browse the repository at this point in the history
Added functions for Google Sheets
  • Loading branch information
MVKozlov authored Jul 18, 2024
2 parents 443398d + 67b8832 commit 5270001
Show file tree
Hide file tree
Showing 24 changed files with 962 additions and 11 deletions.
43 changes: 43 additions & 0 deletions .github/workflows/powershell.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.
#
# https://github.com/microsoft/action-psscriptanalyzer
# For more information on PSScriptAnalyzer in general, see
# https://github.com/PowerShell/PSScriptAnalyzer

name: PSScriptAnalyzer

on: [push]

permissions:
contents: read

jobs:
build:
permissions:
contents: read # for actions/checkout to fetch code
security-events: write # for github/codeql-action/upload-sarif to upload SARIF results
actions: read # only required for a private repository by github/codeql-action/upload-sarif to get the Action run status
name: PSScriptAnalyzer
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Run PSScriptAnalyzer
uses: microsoft/psscriptanalyzer-action@6b2948b1944407914a58661c49941824d149734f
with:
# Check https://github.com/microsoft/action-psscriptanalyzer for more info about the options.
# The below set up runs PSScriptAnalyzer to your entire repository and runs some basic security rules.
path: .\GMGoogleDrive\
recurse: true
# Include your own basic security rules. Removing this option will run all the rules
# includeRule: '"PSAvoidGlobalAliases", "PSAvoidUsingConvertToSecureStringWithPlainText"'
output: results.sarif

# Upload the SARIF file generated in the previous step
- name: Upload SARIF results file
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: results.sarif
16 changes: 15 additions & 1 deletion GMGoogleDrive/GMGoogleDrive.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ FunctionsToExport = @(
'Get-GDriveItemRevisionList',
'Set-GDriveItemContent',
'Set-GDriveItemProperty',
'Export-GDriveItemContent',

'Get-GDriveItemPermissionList',
'Get-GDriveItemPermission',
Expand Down Expand Up @@ -119,7 +120,20 @@ FunctionsToExport = @(
'Clear-GDriveTrash',

'Get-GDriveProxySetting',
'Set-GDriveProxySetting'
'Set-GDriveProxySetting',

'New-GSheetsSpreadSheet',
'New-GSheetsSheet',
'Remove-GSheetsSpreadSheet',
'Remove-GSheetsSheet',
'Copy-GSheetsSheet',
'Clear-GSheetsValue',
'Get-GSheetsValue',
'Set-GSheetsValue',
'Convert-A1NotationToGridRange',
'Export-GSheets',
'Get-GSheetsSpreadsheet',
'Set-GSheetsFormatting'
)

# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
Expand Down
1 change: 1 addition & 0 deletions GMGoogleDrive/GMGoogleDrive.psm1
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ $GDriveAccountsTokenUri = "https://accounts.google.com/o/oauth2/v2/auth"
$GDriveRevokeTokenUri = "https://accounts.google.com/o/oauth2/revoke"
$GDriveAboutURI = "https://www.googleapis.com/drive/v2/about"
$GDriveTrashUri = "https://www.googleapis.com/drive/v3/files/trash"
$GDriveSheetsUri = "https://sheets.googleapis.com/v4/spreadsheets"

$GDriveAuthScope = "https://www.googleapis.com/auth/drive"

Expand Down
7 changes: 4 additions & 3 deletions GMGoogleDrive/Private/NewJWTToken.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
# -Scope 'https://www.googleapis.com/auth/drive'

function NewJWTToken {
[CmdletBinding()]
[CmdletBinding()]
[OutputType([String])]
param(
[Parameter(Mandatory)]
[string]$Issuer,

[string]$KeyId,

[string]$ImpersonationUser,

[Parameter(Mandatory)]
Expand Down
2 changes: 1 addition & 1 deletion GMGoogleDrive/Public/Add-GDriveFolder.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ param(
[string]$AccessToken
)
$folder = Get-Item $path

$CommonParams = @{
ParentID = $ParentID
AccessToken = $AccessToken
Expand Down
45 changes: 45 additions & 0 deletions GMGoogleDrive/Public/Clear-GSheetsValue.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<#
.SYNOPSIS
Clear data from Google Sheet
.DESCRIPTION
Clear data from Google Sheet
.PARAMETER AccessToken
Access Token for request
.PARAMETER SpreadsheetId
SpreadsheetId file id
.EXAMPLE
Clear-GSheetsValue -AccessToken $AccessToken -SpreadsheetId "123456789Qp4QuHv8KD0mMXPhkoPtoe2A9YESi0" -A1Notation "Test!1:15"
.OUTPUTS
.NOTES
Author: Jan Elhaus
.LINK
https://developers.google.com/sheets/api/samples/sheet
#>
function Clear-GSheetsValue {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[string]$AccessToken,

[Parameter(Mandatory)]
[ValidatePattern('([a-zA-Z0-9-_]+)')]
[string]$SpreadsheetId,

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

$Headers = @{
"Authorization" = "Bearer $AccessToken"
}
$requestParams = @{
Uri = $GDriveSheetsUri + "/" + $SpreadsheetId + "/values/" + $A1Notation + ":clear"
Headers = $Headers
ContentType = "application/json; charset=utf-8"
}

Write-Verbose "Webrequest: $($requestParams | ConvertTo-Json -Depth 2)"
Invoke-RestMethod @requestParams -Method POST @GDriveProxySettings

}
81 changes: 81 additions & 0 deletions GMGoogleDrive/Public/Convert-A1NotationToGridRange.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<#
.SYNOPSIS
Convert A1Notation to GridRange
.DESCRIPTION
Convert A1Notation to GridRange
.PARAMETER AccessToken
Access Token for request
.PARAMETER SpreadsheetId
SpreadsheetId file id
.PARAMETER A1Notation
A1Notation of the data range
.EXAMPLE
Convert-A1NotationToGridRange -AccessToken $AccessToken -SpreadsheetId "123456789Qp4QuHv8KD0mMXPhkoPtoe2A9YESi0" -A1Notation "Test!1:15"
.OUTPUTS
.NOTES
Author: Jan Elhaus
.LINK
#>
function Convert-A1NotationToGridRange {
[CmdletBinding()]
[OutputType([String])]
param(
[Parameter(Mandatory)]
[string]$AccessToken,

[Parameter(Mandatory)]
[ValidatePattern('([a-zA-Z0-9-_]+)')]
[string]$SpreadsheetId,

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

if($A1Notation -match '^(?<sheet>.+\!)(?<startcolumn>[A-Za-z]{0,3})(?<startrow>\d{0,7})$') {
$A1Notation = $A1Notation + ":" + $Matches.startcolumn + $Matches.startrow
}

if($A1Notation -match '^(?<sheet>.+\!)(?<startcolumn>[A-Za-z]{0,3})(?<startrow>\d{0,7}):(?<endcolumn>[A-Za-z]{0,3})(?<endrow>\d{0,7})$') {

$Return = @{}

$SheetName = $Matches.sheet.Substring(0,$Matches.sheet.Length-1)
$SpreadsheetMeta = Get-GSheetsSpreadsheet -AccessToken $AccessToken -SpreadsheetId $SpreadsheetId
$Return["sheetId"] = ($SpreadsheetMeta.sheets.properties | Where-Object {$_.title -eq $SheetName}).sheetId
if(-not $Return["sheetId"]) {
throw "SheetName not found"
}

if($Matches.startcolumn) {

$Alphabet = "#ABCDEFGHIJKLMNOPQRSTUVWXYZ"

[int]$Return["startColumnIndex"] = 0
for ($i = 0; $i -lt $Matches.startcolumn.Length; $i++) {
[int]$Return["startColumnIndex"] += $Alphabet.IndexOf($Matches.startcolumn.Substring($i,1).toUpper()) * [math]::pow(26, $i)
}
[int]$Return["startColumnIndex"] -= 1

[int]$Return["endColumnIndex"] = 0
for ($i = 0; $i -lt $Matches.endcolumn.Length; $i++) {
[int]$Return["endColumnIndex"] += $Alphabet.IndexOf($Matches.endcolumn.Substring($i,1).toUpper()) * [math]::pow(26, $i)
}

}

if($Matches.startrow) {
[int]$Return["startRowIndex"] = $Matches.startrow -1
[int]$Return["endRowIndex"] = $Matches.endrow
}

Write-Verbose "GridRange: $($Return | ConvertTo-Json -Compress)"

$Return

} else {
throw "does not match A1Notation format"
}

}
72 changes: 72 additions & 0 deletions GMGoogleDrive/Public/Copy-GSheetsSheet.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<#
.SYNOPSIS
Copy an existing Sheet to another existing GoogleSheet file
.DESCRIPTION
Copy an existing Sheet from one GoogleSheet to another existing GoogleSheet file
.PARAMETER AccessToken
Access Token for request
.PARAMETER SpreadsheetId
SpreadsheetId file id
.PARAMETER DestinationSpreadsheetId
Destination SpreadsheetId file id
.PARAMETER SheetName
name of the sheet to be copied
.EXAMPLE
Copy-GSheetsSheet -AccessToken $AccessToken -SpreadsheetId "123456789Qp4QuHv8KD0mMXPhkoPtoe2A9YESi0" $DestinationSpreadsheetId "123456789Qp4QuHv8KD0mMXPhkoPtoe2A9YESi1" -SheetName "Test1"
.EXAMPLE
Copy-GSheetsSheet -AccessToken $AccessToken -SpreadsheetId "123456789Qp4QuHv8KD0mMXPhkoPtoe2A9YESi0" $DestinationSpreadsheetId "123456789Qp4QuHv8KD0mMXPhkoPtoe2A9YESi1" -SheetId 1
.OUTPUTS
.NOTES
Author: Max Kozlov
.LINK
https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.sheets/copyTo
#>
function Copy-GSheetsSheet {
[CmdletBinding(SupportsShouldProcess)]
param(
[Parameter(Mandatory)]
[string]$AccessToken,

[Parameter(Mandatory)]
[ValidatePattern('([a-zA-Z0-9-_]+)')]
[string]$SpreadsheetId,

[Parameter(Mandatory)]
[ValidatePattern('([a-zA-Z0-9-_]+)')]
[Alias('TargetSpreadsheetId')]
[string]$DestinationSpreadsheetId,

[Parameter(Mandatory, ParameterSetName='SheetId')]
[int]$SheetId,
[Parameter(Mandatory, ParameterSetName='SheetName')]
[string]$SheetName
)
if ($PSCmdlet.ParameterSetName -eq 'SheetName') {
$SpreadsheetMeta = Get-GSheetsSpreadsheet -AccessToken $AccessToken -SpreadsheetId $SpreadsheetId
$SheetId = ($SpreadsheetMeta.sheets.properties | Where-Object {$_.title -eq $SheetName}).sheetId
if($null -eq $SheetId) {
throw "SheetName not found"
}
Write-Verbose "Found $SheetName as $SheetId"
$SheetId = $Id
}
$Headers = @{
"Authorization" = "Bearer $AccessToken"
}
$requestParams = @{
Uri = $GDriveSheetsUri + "/" + $SpreadsheetId + "/sheets/" + $SheetId + ":copyTo"
Headers = $Headers
ContentType = "application/json; charset=utf-8"
Body = @{
destinationSpreadsheetId = $DestinationSpreadsheetId
} | ConvertTo-Json
}

Write-Verbose "Webrequest: $($requestParams | ConvertTo-Json)"

if($PSCmdlet.ShouldProcess("SheetName $SheetName")){
Invoke-RestMethod @requestParams -Method POST @GDriveProxySettings
}

}
2 changes: 1 addition & 1 deletion GMGoogleDrive/Public/Export-GDriveItemContent.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
Return content as raw byte[] array
.PARAMETER Encoding
Set output encoding if content will be returned as string.
By default used GoodleDrive supplied encoding
By default used GoodleDrive supplied encoding
.PARAMETER AccessToken
Access Token for request
.EXAMPLE
Expand Down
Loading

0 comments on commit 5270001

Please sign in to comment.