Cmdlets for Airtable

Build 22.0.8479

接続の確立

CData Cmdlets ユーザーは、データモジュールをインストールし、接続プロパティを設定してスクリプトを開始できます。このセクションでは、CSV インポートおよびエクスポートcmdlet などのネイティブPowerShell cmdlet でAirtable Cmdlets を使用する例を示します。

インストールおよび接続

PSGet がある場合は、PowerShell Gallery から次のコマンドを使ってcmdlet をインストールできます。CData サイトからセットアップを取得することもできます。

Install-Module AirtableCmdlets

プロファイルに以下を追加すると、次のセッションでcmdlet がロードされます。

Import-Module AirtableCmdlets;

Connect-Airtable cmdlet を使って、別のcmdlet に渡すことができる接続オブジェクトを作成します。

$conn = Connect-Airtable -APIKey "keymz3adb53RqsU" -BaseId "appxxN2fe34r3rjdG7" -TableNames "TableA,TableB,TableC" -ViewNames "TableA.ViewA,TableA.ViewB,..,TableX.ViewY"

Airtable への接続

The 本製品 requests tables and views from Airtable.Authenticate, specify a base, and specify a list of tables to connect.

  • ApiKey:Log into Airtable and navigate to the Account icon in the top-right corner.Then, select Account from the dropdown menu.From the Account page, navigate to the API section.Then, click Generate API key.Set the ApiKey to the generated key.
  • BaseId:同じ[API]セクションで、BaseId を特定します。この値を特定するには、https://airtable.com/api に移動してベースを選択します。In the introduction section of the Airtable documentation for the selected base, note the value specified in "The ID of this base is Id."
  • TableNames:A comma-separated list of table names for the selected base in the format TableA,TableB..etc..Supply the table names exactly as they are displayed in the Airtable UI.
  • ViewNames(オプション):その形式のビューのカンマ区切りリスト。 TableA.ViewA,TableA.ViewB,..etc。 これらは、UI にあるビューと同じ名前です。

Note: カラム名に'.' 文字は使用しないでください。これは、パスに基づいてカラム名を作成する際に使用され、INSERT / UPDATE ステートメントを破壊する危険性があります。すでに'.' 文字を含むカラム名が存在する場合は、PathDelimiter をカラム名に使用しない文字に設定してください。

データの取得

Select-Airtable cmdlet はデータを取得するためのネイティブなPowerShell インターフェースを提供します。

$results = Select-Airtable -Connection $conn -Table "SampleTable_1" -Columns @("Id, Column1") -Where "Column2='Bob'"
Invoke-Airtable cmdlet はSQL インターフェースを提供します。このcmdlet を使うと、Query パラメータを介してSQL クエリを実行できます。

cmdlet 出力のパイプ処理

cmdlet は行オブジェクトをパイプラインに一度に一行ずつ返します。以下は、結果をCSV ファイルにエクスポートします。

Select-Airtable -Connection $conn -Table SampleTable_1 -Where "Column2 = 'Bob'" | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\mySampleTable_1Data.csv -NoTypeInformation

Select-Airtable からの結果をSelect-Object cmdlet にパイプして、Export-CSV cmdlet にパイプする前にいくつかのプロパティを実行していることがわかるでしょう。これをする理由は、CData Cmdlets は接続、テーブル、およびカラムの情報を結果セットの各行オブジェクトに追加しますが、必ずしもその情報がCSV ファイルに必要ではないからです。

ただし、これによってcmdlet の出力を別のcmdlet にパイプすることが容易になります。以下に、結果セットをJSON に変換する例を示します。

 
PS C:\> $conn  = Connect-Airtable -APIKey "keymz3adb53RqsU" -BaseId "appxxN2fe34r3rjdG7" -TableNames "TableA,TableB,TableC" -ViewNames "TableA.ViewA,TableA.ViewB,..,TableX.ViewY"
PS C:\> $row = Select-Airtable -Connection $conn -Table "SampleTable_1" -Columns (Id, Column1) -Where "Column2 = 'Bob'" | select -first 1
PS C:\> $row | ConvertTo-Json
{
  "Connection":  {

  },
  "Table":  "SampleTable_1",
  "Columns":  [

  ],
  "Id":  "MyId",
  "Column1":  "MyColumn1"
} 

データの削除

以下は、抽出条件に合うあらゆるレコードを削除します。

Select-Airtable -Connection $conn -Table SampleTable_1 -Where "Column2 = 'Bob'" | Remove-Airtable

データの変更

cmdlet はデータクレンジング同様、データの変換を容易にします。次の例は、レコードがすでに存在するかどうか、挿入する前に更新が必要かどうかをチェックしてから、CSV ファイルのデータをAirtable にロードします。

Import-Csv -Path C:\MySampleTable_1Updates.csv | %{
  $record = Select-Airtable -Connection $conn -Table SampleTable_1 -Where ("Id = `'"+$_.Id+"`'")
  if($record){
    Update-Airtable -Connection $conn -Table SampleTable_1 -Columns @("Id","Column1") -Values @($_.Id, $_.Column1) -Where "Id  = `'$_.Id`'"
  }else{
    Add-Airtable -Connection $conn -Table SampleTable_1 -Columns @("Id","Column1") -Values @($_.Id, $_.Column1)
  }
}

Copyright (c) 2023 CData Software, Inc. - All rights reserved.
Build 22.0.8479