12
19

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

30分でGoogleスプレッドシートを使ってgeojsonを返すAPIを公開する

Last updated at Posted at 2019-06-24

サンプルデータの準備

ScreenShot 2019-06-24 22.39.34.png

Google Action Script

  • スプレッドシートのツール -> スクリプトエディタを選択します

ScreenShot 2019-06-24 22.43.39.png

以下のコードをコピペ

function getData(sheetName) {
  var sheet = SpreadsheetApp.getActive().getSheetByName(sheetName);
  var rows = sheet.getDataRange().getValues();
  return rows.map(function(row) {
    var obj = {};
    obj.type = "Feature"
    obj.properties = {};
    obj.properties.name = row[0]; //A列
    obj.geometry = {};
    obj.geometry.type = "Point";
    obj.geometry.coordinates = [];
    obj.geometry.coordinates.push(row[2],row[1]);//C列,B列
    return obj;
  });
}

function doGet(e) {
  var data = getData('Sheet1');
  var geojson = {};
  geojson.type = "FeatureCollection";
  geojson.features = data;

  var callback = e.parameter.callback;
  
  if (callback) {
    return ContentService
    .createTextOutput(callback+'('+JSON.stringify(geojson, null, 2)+')')
    .setMimeType(ContentService.MimeType.JAVASCRIPT);
  } else {
     return ContentService
    .createTextOutput(JSON.stringify(geojson, null, 2))
    .setMimeType(ContentService.MimeType.JSON);
  }
}

APIとして公開する

  • 公開 -> ウェブアプリケーションとして導入

ScreenShot 2019-06-24 22.48.47.png

  • アプリケーションにアクセスできるユーザを「全員(匿名ユーザを含む)」

ScreenShot 2019-06-24 22.49.11.png

  • 以下のようなURLが発行されます
https://script.google.com/macros/s/XXXXX/exec

APIを叩いてみる

  • curl
% curl -sL https://script.google.com/macros/s/XXXXX/exec|head -n 30
{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {
        "name": "大崎"
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          139.728553,
          35.6197
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {
        "name": "五反田"
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          139.723444,
          35.626446
        ]
      }
    },
    {
  • web browser

ScreenShot 2019-06-24 23.01.13.png

  • 以上で公開完了です
12
19
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
19

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?