【问题标题】:loop over google map api pages in google script在谷歌脚本中循环谷歌地图api页面
【发布时间】:2020-04-14 21:43:13
【问题描述】:

我正在尝试从谷歌地图 api 获取数据并将它们放入谷歌表格中,我创建了一个循环

从下一个令牌页面获取数据。

第一个功能是从工作表中的单元格获取位置

第二个功能是获取第一页并循环到下一页

第三个函数如果在下一页循环

但似乎它们是第三个函数中的问题,可能是因为 fetch 没有读取 Url,我不知道为什么

结果:我得到了前 20 行,然后代码给出(例外:范围内的行数必须至少为 1。)

有人可以给我一些提示或纠正我做错了什么吗?提前谢谢

function APIfromLoc(){
  var ss = SpreadsheetApp.openById('1kiecwnIEHNvTz3VD8AjPT0CK8HvqmzCi86vXRyrU4lc');
  var sheet = ss.getSheetByName('Sheet1');

  var location = sheet.getRange(1,1).getValue();

  callAPIinit(location);

}

// function to call the first page api 
function callAPIinit(location) {
  var ss = SpreadsheetApp.openById('1kiecwnIEHNvTz3VD8AjPT0CK8HvqmzCi86vXRyrU4lc');
  var sheet = ss.getSheetByName('Sheet2');

  var API_key = '';
  //var location = ' ' ;
  var Url = 'https://maps.googleapis.com/maps/api/place/textsearch/json?location='+location+'&raduis=50000&type=restaurant&key='+API_key;

  var response = UrlFetchApp.fetch(Url);



  // Parse the JSON reply
  var json = response.getContentText();
  var data = JSON.parse(json);


  var results = data["results"];

  var token = data["next_page_token"];

  var output = []

  results.forEach(function(elem,i) {

    output.push([elem["formatted_address"],elem["geometry"]["location"]["lat"],elem["geometry"]["location"]["lng"],elem["geometry"]["viewport"]["northeast"]["lat"],elem["geometry"]["viewport"]["northeast"]["lng"],elem["geometry"]["viewport"]["southwest"]["lat"],elem["geometry"]["viewport"]["southwest"]["lng"],elem["id"],elem["name"],elem["place_id"],elem["plus_code"]["compound_code"],elem["plus_code"]["global_code"],elem["rating"],elem["reference"],elem["types"][0],elem["types"][1],elem["types"][2],elem["types"][3],elem["price_level"],elem["user_ratings_total"]]);
   // sheet.setRowHeight(i,65);
  });

    // adds an index number to the array
  output.forEach(function(elem,i) {
    elem.unshift(i + 1);
  });

  var len = output.length;


  // paste in the values
  sheet.getRange(2,1,len,21).setValues(output);
  sheet.getRange(1,1).setValue(token);

 Next_pages(Url);
}

/**
--------------------------------------------------------------------------------------------------------------------------------------------------------------
 */

// function to call the next pages using first page token 
function Next_pages(Url) {


  var ss = SpreadsheetApp.openById('1kiecwnIEHNvTz3VD8AjPT0CK8HvqmzCi86vXRyrU4lc');
  var sheet = ss.getSheetByName('Sheet2');
  var token = sheet.getRange(1,1).getValue();
  Url = Url + "&pagetoken=" + token;
 Logger.log(Url)
 //loop to get all pages  
  do{

  //var location = ' ' ;
 // var Url = Url+'&pagetoken='+token;

  var response = UrlFetchApp.fetch(Url);
  var json = response.getContentText();

  var data = JSON.parse(json);
  Logger.log(data)
  var results = data["results"];
  token = []; 
  token = data["next_page_token"];

  var output = []

  results.forEach(function(elem,i) {

    output.push([elem["formatted_address"],elem["geometry"]["location"]["lat"],elem["geometry"]["location"]["lng"],elem["geometry"]["viewport"]["northeast"]["lat"],elem["geometry"]["viewport"]["northeast"]["lng"],elem["geometry"]["viewport"]["southwest"]["lat"],elem["geometry"]["viewport"]["southwest"]["lng"],elem["id"],elem["name"],elem["place_id"],elem["plus_code"]["compound_code"],elem["plus_code"]["global_code"],elem["rating"],elem["reference"],elem["types"][0],elem["types"][1],elem["types"][2],elem["types"][3],elem["price_level"],elem["user_ratings_total"]]);
   // sheet.setRowHeight(i,65);
  });

    // adds an index number to the array
  output.forEach(function(elem,i) {
    elem.unshift(i + 1);
  });

  var len = output.length;

  // paste in the values
  sheet.getRange(sheet.getLastRow()+1,1,len,21).setValues(output);
  sheet.getRange(1,1).clearContent();

} while(token != Null)
}

【问题讨论】:

    标签: api google-apps-script google-sheets


    【解决方案1】:

    您很可能已达到配额使用限制,因为您尝试获取的位置过多。这就是我在尝试重现这种行为时至少得到的结果。

    当达到配额时,API 不会返回位置列表,而是返回此错误:

    {error_message=You have exceeded your daily request quota for this API. If you did not set a custom daily request quota, verify your project has an active billing account: http://g.co/dev/maps-no-account, status=OVER_QUERY_LIMIT, results=[], html_attributions=[]}

    如果您注意到此错误响应,则 results 字段是一个空数组。结果,output 也是一个空数组,因此 output.length; 为 0。然后您尝试获取一个范围,其中行数 (len) 等于 0,这解释了您的错误得到。

    要查看您当前的配额限制,您可以关注these steps

    注意:

    • 您的网址中有错字:raduis 不是有效的查询参数;应该改为radius

    参考:

    【讨论】:

      【解决方案2】:

      感谢您的帮助,似乎下一页令牌需要一些时间才能出现,所以我将 Utilities.sleep(10000) 添加到下一页循环中。

      参考Paging on Google Places API returns status INVALID_REQUEST

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-17
        • 1970-01-01
        相关资源
        最近更新 更多