<aside> 💡
GAS(Google App Script)から ShtockData の API 機能を利用する方法を解説します
</aside>
基礎ガイド
操作マニュアル
トラブルシューティング
API活用ガイド
FLOW_ID、API キーを API_KEY として設定してください。const FLOW_ID = ''; // クロール設定 ID
const API_KEY = ''; // API キー
// CSV がダウンロードされる場合
function downloadLatestCsv() {
// API から CSV をダウンロード
const response = UrlFetchApp.fetch(`https://app.shtockdata.com/api/v1/download/latest?flow_id=${FLOW_ID}&api_key=${API_KEY}`);
// ダウンロードした CSV を GAS の2重配列に変換
const values = Utilities.parseCsv(response.getContentText());
// CSV のファイル名を取得
const filename = decodeURI(response.getHeaders()['Content-Disposition'].replace(/^.+filename\\*=UTF-8''(.+)$/, '$1'));
// 新しいスプレッドシートを作成
const sheetFile = SpreadsheetApp.create(filename);
// 新しいスプレッドシートにあるデフォルトのシートを取得
const sheet = sheetFile.getSheets()[0];
// そこに CSV から取得したデータを挿入
sheet.getRange(1, 1, values.length, values[0].length).setValues(values);
}
FLOW_ID、API キーを API_KEY として設定してください。const FLOW_ID = ''; // クロール設定 ID
const API_KEY = ''; // API キー
// ZIP がダウンロードされる場合
function downloadLatestZip() {
// API から ZIP をダウンロード
const response = UrlFetchApp.fetch(`https://app.shtockdata.com/api/v1/download/latest?flow_id=${FLOW_ID}&api_key=${API_KEY}`);
// ZIP を解凍して CSV ファイルの配列を取得する
const blobCsvs = Utilities.unzip(response.getBlob());
// CSV ファイルごとに処理
for (const csvBlob of blobCsvs) {
// CSV を GAS の2重配列に変換
const values = Utilities.parseCsv(csvBlob.getDataAsString());
// 新しいスプレッドシートを作成
const sheetFile = SpreadsheetApp.create(csvBlob.getName());
// 新しいスプレッドシートにあるデフォルトのシートを取得
const sheet = sheetFile.getSheets()[0];
// そこに CSV から取得したデータを挿入
sheet.getRange(1, 1, values.length, values[0].length).setValues(values);
}
}