﻿Google AppSheetで作るアプリサンプルブック

掲載リスト




リスト6-1
const apikey = "……API Key……"; //☆


const lat = 35.6807876;
const lng = 139.7680843;


function getLibrariesAPI() {
  const geo = lng + "," + lat;
  const url = "https://api.calil.jp/library?appkey=" + apikey + "&geocode=" + geo + "&limit=10";
  const response = UrlFetchApp.fetch(url);
  const result = response.getContentText();
  const document = XmlService.parse(result);
  const root = document.getRootElement();
  const libs = root.getChildren();
  const data = [];
  libs.forEach((value, index, array)=> {
    const geo = value.getChild("geocode").getText().split(",");
    const ltlg = geo[1] + "," + geo[0];
    data.push([
      value.getChild("formal").getText(),
      value.getChild("address").getText(),
      value.getChild("tel").getText(),
      value.getChild("url_pc").getText(),
      ltlg
    ])
  });
  const sheet = SpreadsheetApp.getActiveSheet();
  sheet.getRange(2,1,libs.length,5).setValues(data);
}




リスト6-2
function areacode() {
  const url = "https://www.jma.go.jp/bosai/common/const/area.json";
  const response = UrlFetchApp.fetch(url);
  const result = JSON.parse(response.getContentText());
  const data = [];
  for (var item of Object.keys(result.offices)){
    data.push([item, result.offices[item].name, result.offices[item].officeName]);
  }
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("コードリスト");
  sheet.getRange(2,1,data.length,3).setValues(data);
}




リスト6-3
function GetWeather() {
  const codesheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("コード");
  const code = codesheet.getRange(2,1).getValue();
  const Weathersheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("天気");


  const url = "https://www.jma.go.jp/bosai/forecast/data/overview_forecast/" + code + ".json";
  const response = UrlFetchApp.fetch(url);
  const result = JSON.parse(response.getContentText());


  Weathersheet.getRange(2,1).setValue(result.targetArea);
  Weathersheet.getRange(2,2).setValue(new Date(result.reportDatetime).toLocaleString());
  Weathersheet.getRange(2,3).setValue(result.text);
}