接続の確立
CData Cmdlets ユーザーは、データモジュールをインストールし、接続プロパティを設定してスクリプトを開始できます。このセクションでは、CSV インポートおよびエクスポートcmdlet などのネイティブPowerShell cmdlet でSmartsheet Cmdlets を使用する例を示します。
Connecting to Smartsheet
Smartsheet supports connections via the following authentication methods:
- Using the Personal Access Token
Personal Access Token
Use the personal token to test and to access your own data. To obtain the personal token, follow the steps below:
- Log into Smartsheet.
- Click Account and select Personal Settings.
- Click API Access and use the form to generate new access tokens or manage existing access tokens.
Synch Connections
Before you connect, set the following variables:- OAuthClientId: The client Id assigned when you registered your custom OAuth application.
- OAuthClientSecret: The client secret assigned when you registered your custom OAuth application.
Click Connect to Smartsheet to open the OAuth endpoint in your default browser. Log in and grant permissions to the application.
The driver then completes the OAuth process as follows:
- Extracts the access token from the callback URL.
- Obtains a new access token when the old one expires.
- Saves OAuth values so that they persist across connections.
接続オブジェクトの作成
Connect-Smartsheet cmdlet を使って、別のcmdlet に渡すことができる接続オブジェクトを作成します。
$conn = Connect-Smartsheet -AuthScheme 'OAuth' -InitiateOAuth 'GETANDREFRESH' -OAuthClientId 'MyOAuthClientId' -OAuthClientSecret 'MyOAuthClientSecret' -CallbackURL 'http://localhost:33333'
データの取得
Select-Smartsheet cmdlet はデータを取得するためのネイティブなPowerShell インターフェースを提供します。
$results = Select-Smartsheet -Connection $conn -Table "Sheet_Test_Sheet" -Columns @("Id, Name") -Where "Favorite='True'"
Invoke-Smartsheet cmdlet はSQL インターフェースを提供します。このcmdlet を使うと、Query パラメータを介してSQL クエリを実行できます。
cmdlet 出力のパイプ処理
cmdlet は行オブジェクトをパイプラインに一度に一行ずつ返します。以下は、結果をCSV ファイルにエクスポートします。
Select-Smartsheet -Connection $conn -Table Sheet_Test_Sheet -Where "Favorite = 'True'" | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\mySheet_Test_SheetData.csv -NoTypeInformation
Select-Smartsheet からの結果をSelect-Object cmdlet にパイプして、Export-CSV cmdlet にパイプする前にいくつかのプロパティを実行していることがわかるでしょう。これをする理由は、CData Cmdlets は接続、テーブル、およびカラムの情報を結果セットの各行オブジェクトに追加しますが、必ずしもその情報がCSV ファイルに必要ではないからです。
ただし、これによってcmdlet の出力を別のcmdlet にパイプすることが容易になります。以下に、結果セットをJSON に変換する例を示します。
PS C:\> $conn = Connect-Smartsheet -AuthScheme 'OAuth' -InitiateOAuth 'GETANDREFRESH' -OAuthClientId 'MyOAuthClientId' -OAuthClientSecret 'MyOAuthClientSecret' -CallbackURL 'http://localhost:33333'
PS C:\> $row = Select-Smartsheet -Connection $conn -Table "Sheet_Test_Sheet" -Columns (Id, Name) -Where "Favorite = 'True'" | select -first 1
PS C:\> $row | ConvertTo-Json
{
"Connection": {
},
"Table": "Sheet_Test_Sheet",
"Columns": [
],
"Id": "MyId",
"Name": "MyName"
}
データの削除
以下は、抽出条件に合うあらゆるレコードを削除します。
Select-Smartsheet -Connection $conn -Table Sheet_Test_Sheet -Where "Favorite = 'True'" | Remove-Smartsheet
データの変更
cmdlet はデータクレンジング同様、データの変換を容易にします。次の例は、レコードがすでに存在するかどうか、挿入する前に更新が必要かどうかをチェックしてから、CSV ファイルのデータをSmartsheet にロードします。
Import-Csv -Path C:\MySheet_Test_SheetUpdates.csv | %{
$record = Select-Smartsheet -Connection $conn -Table Sheet_Test_Sheet -Where ("Id = `'"+$_.Id+"`'")
if($record){
Update-Smartsheet -Connection $conn -Table Sheet_Test_Sheet -Columns @("Id","Name") -Values @($_.Id, $_.Name) -Where "Id = `'$_.Id`'"
}else{
Add-Smartsheet -Connection $conn -Table Sheet_Test_Sheet -Columns @("Id","Name") -Values @($_.Id, $_.Name)
}
}