<aside> 💡

GAS(Google App Script)から ShtockData の API 機能を利用する方法を解説します

</aside>

ホーム画面について

クロール設定一覧

パスワードの再設定

ユーザー管理

基礎ガイド

CSSセレクタ

XPath

セレクタの抽出手順

セレクタ設定練習問題

正規表現

クロール設定例

操作マニュアル

クロール設定新規作成

【1】起点

【2】ページ設定

**【3】**抽出

**【4】**整形

【5】結合

**【6】**出力

【7】監視

【8】クロール

トラブルシューティング

システム全般について

ページ設定について

抽出について

整形について

出力について

クロールについて

API活用ガイド

APIリファレンス

GAS との連携

はじめに

前提条件

処理の流れ

サンプルコード(CSVファイルの場合)

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);
}

サンプルコード(ZIPファイルの場合)

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);
  }
}