-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from elhaus/GSheet
Added functions for Google Sheets
- Loading branch information
Showing
24 changed files
with
962 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.