
住所から郵便番号を自動取得するExcelAPIが提供するスプレッドシート関数をGoogle Apps Scriptにした例
この説明は、ChatGPTで作成しています。
このスクリプトは、スプレッドシートに入力された住所を元に、外部APIを使って郵便番号を自動的に取得し、スプレッドシートに記録する仕組みを持っています。
スクリプトの動作の仕組み
スプレッドシートIDとタブ名の指定
スクリプトの冒頭で、対象となるスプレッドシートのIDと、処理対象のシートタブ名を指定します。スプレッドシートIDの場所: "●●●ここにスプレッドシートIDをいれてください●●●"
タブ名の場所: "●●●ここにシートタブ名をいれてください●●●"
データ範囲の取得と処理開始
スプレッドシートから全てのデータを取得し、2行目以降のデータ(ヘッダー部分はスキップ)を処理します。APIを使った郵便番号の取得
各行のA列に入力されている住所を基に、外部APIを利用して郵便番号を取得します。APIのエンドポイント: https://api.excelapi.org/post/zipcode
住所はURLエンコードされ、APIリクエストのパラメーターに含まれます。
取得した郵便番号の記録
APIのレスポンスに郵便番号が含まれている場合、それをB列に記録します。
エラーが発生した場合は、「エラー:エラー内容」をB列に記録します。
変更内容の保存
スクリプトの最後で SpreadsheetApp.flush() を呼び出して、スプレッドシートへの変更を確定させます。
スクリプトの利用手順
Apps Scriptエディターの起動
スプレッドシートのメニューから以下を選択します:
拡張機能 > Apps Scriptコードの貼り付け
エディターに表示されたコードエリアに、このスクリプトをコピー&ペーストします。スクリプト内の指定部分を編集
スプレッドシートID
シートタブ名
をそれぞれ実際の値に置き換えます。
スクリプトの実行
エディターの再生ボタン(▶)の左にある保存ボタンをクリックします。
エディターの再生ボタン(▶)をクリックしてスクリプトを実行します。
初回実行時に権限の許可が求められるので、指示に従って許可を行ってください。許可したら、再度、再生ボタン(▶)をクリックしてスクリプトを実行します。
注意点
外部API: このスクリプトは https://api.excelapi.org を利用しています。このAPIの利用規約や制限を事前に確認してください。
エラー処理: API呼び出しに失敗した場合、B列にエラー内容が表示されます。
リファレンスリンク:
function convertAddressToZipExcelApi() {
// スプレッドシートIDとタブ名を指定
const spreadsheetId = "●●●ここにスプレッドシートIDをいれてください●●●";
const sheetName = "●●●ここにシートタブ名をいれてください●●●";
// スプレッドシートとシートを取得
const sheet = SpreadsheetApp.openById(spreadsheetId).getSheetByName(sheetName);
if (!sheet) {
throw new Error(`シート「${sheetName}」が見つかりません。`);
}
// データ範囲を取得
const data = sheet.getDataRange().getValues();
// 2行目からデータを処理
for (let i = 1; i < data.length; i++) {
const address = data[i][0]; // A列にある住所
if (address) {
try {
// API URLを作成
const apiUrl = `https://api.excelapi.org/post/zipcode?address=${encodeURIComponent(address)}`;
// APIを呼び出し
const response = UrlFetchApp.fetch(apiUrl);
const json = JSON.parse(response.getContentText());
// 郵便番号を取得してB列に入力
const postalCode = json;
sheet.getRange(i + 1, 2).setValue(postalCode);
} catch (error) {
sheet.getRange(i + 1, 2).setValue("エラー:" + error); // エラー時にB列に「エラー:エラー内容」を入力
}
}
}
SpreadsheetApp.flush();
}
関連キーワード
#Google Apps Script #Google Drive #スプレッドシート #郵便番号取得 #住所変換 #API連携 #JavaScript #スプレッドシート操作 #外部API #エラー処理 #データ自動化 #スプレッドシート連携 #スクリプト開発 #Googleスプレッドシート #プログラミング初心者 #自動化ツール #URLエンコード #権限設定 #Google拡張機能 #データ処理
English Translation:
Script Name: Automatically Retrieve Postal Codes from Addresses
This script fetches postal codes based on the addresses entered in a Google Sheet by utilizing an external API and records them in the same sheet.
How the Script Works:
Specify Spreadsheet ID and Tab Name
At the beginning of the script, you specify the ID of the target spreadsheet and the name of the sheet tab to process.Spreadsheet ID placeholder: "●●●ここにスプレッドシートIDをいれてください●●●"
Tab name placeholder: "●●●ここにシートタブ名をいれてください●●●"
Retrieve Data Range and Begin Processing
The script fetches all the data from the sheet and starts processing from the second row (skipping the header).Fetch Postal Codes via API
For each row, the script takes the address in column A and retrieves its postal code using an external API.API Endpoint: https://api.excelapi.org/post/zipcode
The address is URL-encoded and included as a parameter in the API request.
Record Retrieved Postal Codes
If the API response contains a postal code, it is written to column B.
If an error occurs, column B displays the error details.
Save Changes
The script calls SpreadsheetApp.flush() at the end to ensure all changes are saved.
Steps to Use the Script:
Open the Apps Script Editor:
In the Google Sheet menu, select:
Extensions > Apps ScriptPaste the Script:
Copy and paste this script into the editor.Edit the Script:
Replace the placeholders for Spreadsheet ID and Tab Name with actual values.Run the Script:
Click the play button (▶) in the editor to execute the script.
On the first run, you may need to grant permissions; follow the prompts.
Notes:
External API: This script uses https://api.excelapi.org. Be sure to review the API's terms of use and limitations.
Error Handling: If the API call fails, error details will be displayed in column B.
Reference Links: