4/24/2010

Scripts in Google spreadsheets


Recently became available scripts in Google Spreadsheets for personal accounts (gmail).
Abstract
In addition, we can publish scripts in the gallery.
Now we can create scripts for Google Spreadsheets, upload, post, communicate with other users.
Scripts have a rich set of features. We can use all the management services available in Google.

The result of the script, for example, may be:
  • new function;
  • value returned by the Web service, including, SOAP, WSDL, and others;
  • management of other services, such as website, calendar, mailing;
  • automatic filling of tables (direct control of our spreadsheets);
  • creation of user interfaces (custom spreadsheets UI).

Examples of scripts in neogeography

Great Circle Distance between points
  1. function GCD1(lat1, lon1, lat2, lon2) {
  2. // Return Great Circle Distance between points calculation
  3. function radians(a) {
  4. var outNum =Math.PI*a/180;
  5. return outNum;
  6. }
  7. var R = 6372.795;
  8. var d1=Math.sin(radians(lat1))*Math.sin(radians(lat2))+Math.cos(radians(lat1))*Math.cos(radians(lat2))*Math.cos(radians(lon2)-radians(lon1));
  9. var d2=Math.cos(radians(lat2))*Math.sin(radians(lon2)-radians(lon1));
  10. var d3=Math.cos(radians(lat1))*Math.sin(radians(lat2))-Math.sin(radians(lat1))*Math.cos(radians(lat2))*Math.cos(radians(lon2)-radians(lon1));
  11. var len=R*Math.atan2(Math.sqrt(d2*d2+d3*d3),d1);
  12. return len;
  13. }​
Geocoding
Return KML by placename
  1. function getKML(placeName) {
  2. // Return KML by placename
  3. if (placeName == "") {
  4. return "You have to write the name the place"
  5. }
  6. var url = "http://maps.google.com/maps/geo?q="+ placeName+"&output=kml";
  7. var response = UrlFetchApp.fetch(url);
  8. var str = response.getContentText();
  9. return str;
  10. }
Return Longitude and Latitude by placename
  1. function getLngLat(placeName) {
  2. // Return LngLatitude by placename
  3. if (placeName == "") {
  4. return "You have to write the name the place"
  5. }
  6. var url = "http://maps.google.com/maps/geo?q="+ placeName+"&output=json";
  7. var response = UrlFetchApp.fetch(url);
  8. var str=eval('(' + response.getContentText() + ')').Placemark[ 0].Point.coordinates;
  9. return str;
  10. }
Return Longitude by placename
  1. function getLng(placeName) {
  2. // Return Longitude by placename
  3. if (placeName == "") {
  4. return "You have to write the name the place"
  5. }
  6. var url = "http://maps.google.com/maps/geo?q="+ placeName+"&output=json";
  7. var response = UrlFetchApp.fetch(url);
  8. var str=eval('(' + response.getContentText() + ')').Placemark[ 0].Point.coordinates[ 0];
  9. return str;
  10. }
Return Latitude by placename
  1. function getLat(placeName) {
  2. // Return Latitude by placename
  3. if (placeName == "") {
  4. return "You have to write the name the place"
  5. }
  6. var url = "http://maps.google.com/maps/geo?q="+ placeName+"&output=json";
  7. var response = UrlFetchApp.fetch(url);
  8. var str=eval('(' + response.getContentText() + ')').Placemark[ 0].Point.coordinates[1];
  9. return str;
  10. }
Reverse geocoding
Return address by placename
  1. function getAddress(placeCoord) {
  2. // Return Address by placeCoord (reverse geocoding) placeCoord=lat,lng
  3. if (placeCoord == "") {
  4. return "You have to write the name the place"
  5. }
  6. var url = "http://maps.google.com/maps/geo?q="+ placeCoord+"&output=json";
  7. var response = UrlFetchApp.fetch(url);
  8. var str=eval('(' + response.getContentText() + ')').Placemark[ 0].address;
  9. return str;
  10. }
Example

Post a Comment