Created
November 28, 2015 13:02
-
-
Save sakadon/5dff6dc3828b81f05099 to your computer and use it in GitHub Desktop.
Googleフォームから投稿された住所から経度緯度を検索しGoogleスプレッドシートに追記するやつ ref: http://qiita.com/sakadon/items/b99c7bd39fa6f3d0c53f
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 【GAS】住所から緯度経度を取得する | |
function getLocation(address) { | |
var requestUrl = "https://maps.googleapis.com/maps/api/geocode/json?address=" + address; // Google Geocoding API | |
var result = JSON.parse( UrlFetchApp.fetch(requestUrl) ); | |
if ( result['status'] === 'ZERO_RESULTS' ) { return 'No Result.' } | |
var location = result['results'][0]['geometry']['location']; | |
return location['lat'] +','+ location['lng']; | |
} | |
// フォームがサブミットされたら | |
function onFormSubmit(e) { | |
Logger.log(e); | |
try { | |
address = e.namedValues["住所1(都道府県)"].toString() +' '+ e.namedValues["住所2(市区町村)"].toString(); | |
address = address.replace(/,/g, '');//配列をtoStringするとカンマが邪魔なので殺す | |
Logger.log('address: '+address); | |
latlng = getLocation(address); | |
Logger.log('latlng: '+latlng); | |
// 経度緯度を書き込むところは固定 | |
var range = e.range | |
var ss = SpreadsheetApp.getActiveSpreadsheet().getDataRange(); | |
ss.getCell( range.getRow(), 15 ).setValue(latlng); | |
} catch (e) { | |
//エラーが発生した場合に管理者にメールを送信する | |
MailApp.sendEmail(Session.getActiveUser().getEmail(), "Error report", e.message); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment