简体   繁体   中英

How can I find geolocation of MULTIPLE IP addresses via Google Apps Script in Google Sheets?

I have a list of IP addresses in Column A of my sheet and I need to translate those IP Addresses into Country and city on the next columns. How can I do this via Google Apps script in Google sheets?

You want the Country and City of IP addresses in Column A.

Google Sheets does not have a 'native' function to do this, and the Google Maps Geolocation API requires a billing account (though there is a $200 monthly credit which permits the equivalent of 40,000 geolocation requests ).

There are many examples on StackOverflow of using an external API though very few using Google Sheets. In addition, none are particularly recent, and some are out-dated because of elapse of time, and other APIs may offer "free" geolocation services but require an account to access the API.

This answer demonstrates the process using iplocate.com which does not require an account to access their free API.


function so5840531801() {

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheetname = "IP_sheet";
  var sheet = ss.getSheetByName(sheetname);

  //get the IP values
  var LR = sheet.getLastRow();
  var ipdata = sheet.getRange(1,1,LR,1).getValues();
  var iplength  = ipdata.length;
  // Logger.log(iplength);// DEBUG 

    // props to [wcndave](https://stackoverflow.com/a/20456709/1330560)
    // for link to iplocate
    // iplocate have an API but dont require an account
  for (var i=0; i<iplength; i++){
    // Logger.log("DEBUG: i="+i+", IP = "+ipdata[i][0]);
    var url = "https://www.iplocate.io/api/lookup/"+ipdata[i][0];
    // Logger.log("DEBUG: url = "+url)
    var response = UrlFetchApp.fetch(url);
    // Logger.log(response.getContentText());//DEBUG
    var jsondata = JSON.parse(response);  
    Logger.log("DEBUG: Country = "+jsondata.country+", City = "+jsondata.city);
    }
}

You can use the IP2Location Google Sheet Add-On which requires a subscription to the IP2Location Web Service .

Installation

Install from the Google Sheet via the menu (Add-ons -> Get add-ons... -> IP2Location)

Or, install from the direct link https://chrome.google.com/webstore/detail/ip2location/bhbkibfamajihabmhlebcllfmijlflnm .

Click on the Add-ons in the Google Sheet menu.

Go to IP2Location -> Edit settings.

Enter your API key and click Login.

Sample usage

=IP2Location(A1:A20, "WS24")

The above assumes you have IP addresses in A1 to A20 and you want to call the WS24 package. Key in the above into B1 and the results will appear next to your IP address column.

If you just need country and city, you can use "WS3" instead.

For more info about the rest of the packages and the results returned, see https://www.ip2location.com/web-service/ip2location

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM