如何将 2024 Google Places API 链接到 Google 表格以获取地点的详细信息?

问题描述 投票:0回答:1

我在 Google 表格中有一个我去过的餐馆列表,我想通过 Places API(新)检索餐馆的评论或评级。如何创建循环遍历餐厅名称行并获取餐厅价格和评论等详细信息的代码?

大多数在线帮助都是关于旧的 Places API 的,我想我不妨使用新的 API,因为有些字段是新的,从长远来看会更容易维护。

我想在谷歌自己的平台上使用 JavaScript 中的应用程序脚本来构建它,因为我认为这将是最原生的方式

google-sheets google-apps-script google-places-api
1个回答
0
投票

在这里发布我自己的代码,因为我找不到引用新 Places API 的任何其他更新源。

ChatGPT 提供了引用旧 API 的代码,因此我对其进行了编辑以使用新的 Places API:

function getRestaurantRating(restaurantName) {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var lastRow = sheet.getLastRow();
  
  // Loop through cells in the range B10 to the last filled row
  for (var i = 10; i <= lastRow; i++) {
    var restaurantNameCell = sheet.getRange('B' + i);
    var statusCell = sheet.getRange('E' + i); // Corresponding cell in column E
    
    // Check if the corresponding cell in column E is empty
    if (statusCell.isBlank()) {
      var restaurantName = restaurantNameCell.getValue();

      // Make a request to the Places API to get the details of the restaurant
      var apiKey = 'AIzaSyAXY1ai0iIYYOYvIxGEG1qFqAs6mjGzG-A'; // Replace 'YOUR_API_KEY' with your actual API key
      var url = 'https://places.googleapis.com/v1/places:searchText';
      
      var payload = {
        textQuery: restaurantName
      };
      
      var headers = {
        'Content-Type': 'application/json',
        'X-Goog-Api-Key': apiKey,
        'X-Goog-FieldMask': 'places.displayName,places.types,places.rating,places.userRatingCount,places.googleMapsUri,places.primaryType,places.priceLevel,places.shortFormattedAddress,places.photos'
      };
      
      var options = {
        method: 'post',
        contentType: 'application/json',
        headers: headers,
        payload: JSON.stringify(payload)
      };
      
      var response = UrlFetchApp.fetch(url, options);
      var jsonResponse = JSON.parse(response.getContentText());


            
      // Check if the API request was successful
      if (typeof jsonResponse.places !== 'undefined') {
          var name = jsonResponse.places[0].displayName.text;
          //var primaryType = jsonResponse.places[0].primaryType;
          //var photos = jsonResponse.places[0].photos;          
          var googleMapsUrl = jsonResponse.places[0].googleMapsUri;
          var type = jsonResponse.places[0].primaryType;
          var priceLevel = jsonResponse.places[0].priceLevel;
          var userRatingCount = jsonResponse.places[0].userRatingCount;
          
          // Update cells with retrieved details
          var nameCell = sheet.getRange('A' + i);
          //var primaryTypeCell = sheet.getRange('M' + i);
          //var photosCell = sheet.getRange('K' + i)
          var googleMapsUrlCell = sheet.getRange('L' + i);
          var typeCell = sheet.getRange('E' + i);
          var priceLevelCell = sheet.getRange('J' + i);
          var userRatingCountCell = sheet.getRange('H' + i);
          
          nameCell.setValue(name)
          //primaryTypeCell.setValue(primaryType)
          //photosCell.setValue(photos)
          googleMapsUrlCell.setValue(googleMapsUrl);
          typeCell.setValue(type);
          priceLevelCell.setValue(priceLevel);
          userRatingCountCell.setValue(userRatingCount);
      } else {
        return 'Not found';
      }
    }

    }
  }

希望这有助于开始使用新的 Places API。

© www.soinside.com 2019 - 2024. All rights reserved.