[英]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.我在工作表的 A 列中有一个 IP 地址列表,我需要将这些 IP 地址翻译成下一列的国家和城市。 How can I do this via Google Apps script in Google sheets?
如何通过 Google 表格中的 Google Apps 脚本执行此操作?
You want the Country and City of IP addresses in Column A.您需要 A 列中的 IP 地址的国家和城市。
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 ).谷歌表格没有“本机” function 来执行此操作,而谷歌地图地理定位 API 需要一个结算帐户(尽管每月有 200 美元的信用额度,允许相当于40,000 个地理定位请求)。
There are many examples on StackOverflow of using an external API though very few using Google Sheets. StackOverflow 上有很多使用外部 API 的示例,但使用 Google 表格的例子很少。 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.
此外,没有一个是特别新的,有些由于时间流逝而过时,其他 API 可能提供“免费”的地理定位服务,但需要一个帐户才能访问 API。
This answer demonstrates the process using iplocate.com which does not require an account to access their free API.此答案演示了使用iplocate.com的过程,该过程不需要帐户即可访问其免费的 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 .您可以使用需要订阅IP2Location Web 服务的 IP2Location Google Sheet Add-On 。
Installation安装
Install from the Google Sheet via the menu (Add-ons -> Get add-ons... -> IP2Location)通过菜单从 Google 表格安装(附加组件 -> 获取附加组件... -> IP2Location)
Or, install from the direct link https://chrome.google.com/webstore/detail/ip2location/bhbkibfamajihabmhlebcllfmijlflnm .或者,从直接链接https://chrome.google.com/webstore/detail/ip2location/bhbkibfamajihabmhlebcllfmijlflnm安装。
Click on the Add-ons in the Google Sheet menu.单击 Google 表格菜单中的附加组件。
Go to IP2Location -> Edit settings. Go 到 IP2Location -> 编辑设置。
Enter your API key and click Login.输入您的 API 密钥,然后单击登录。
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.以上假设您在 A1 到 A20 中有 IP 地址,并且您想调用 WS24 package。 Key in the above into B1 and the results will appear next to your IP address column.
将上述内容键入 B1,结果将出现在您的 IP 地址列旁边。
If you just need country and city, you can use "WS3" instead.如果你只需要国家和城市,你可以使用“WS3”来代替。
For more info about the rest of the packages and the results returned, see https://www.ip2location.com/web-service/ip2location有关包的 rest 和返回结果的更多信息,请参阅https://www.ip2location.com/web-service/ip2location
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.