You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用新版Google Places API+Apps Script批量获取Google Sheets餐厅数据?

用新版Places API批量获取餐厅信息(Google Apps Script实现)

前置准备

  1. 启用新版Places API并获取密钥
    • 登录Google Cloud控制台,找到并启用Places API (New)
    • 创建API密钥,建议限制密钥的调用范围(仅允许Places API、仅绑定当前项目),防止密钥泄露导致超额扣费
  2. 整理Sheet结构
    • 假设你的Sheet布局:
      • A列:餐厅名称(必填)
      • B列:餐厅所在城市/地址(可选,提升搜索匹配精度)
      • C列:预留写入评分
      • D列:预留写入评论总数
      • E列:预留写入价格等级(1-4档,1为最低档)
      • F列:预留写入餐厅详情页链接

完整代码实现

打开Google Sheets,点击扩展程序 > Apps Script,替换默认代码为以下内容:

// 替换为你的Google Places API密钥
const API_KEY = "你的API密钥";
// 替换为你的Sheet名称
const SHEET_NAME = "餐厅列表";
// 新版Places API文本搜索端点
const PLACES_SEARCH_ENDPOINT = "https://places.googleapis.com/v1/places:searchText";

function fetchRestaurantDetails() {
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(SHEET_NAME);
  const dataRange = sheet.getDataRange();
  const values = dataRange.getValues();
  
  // 跳过表头(若第一行是表头,从第二行开始遍历)
  for (let i = 1; i < values.length; i++) {
    const restaurantName = values[i][0];
    const location = values[i][1] || "";
    
    // 跳过空行
    if (!restaurantName) continue;
    
    try {
      // 构建搜索请求体
      const requestBody = {
        textQuery: `${restaurantName} ${location}`,
        languageCode: "zh-CN", // 可按需修改为en-US等
        fields: ["displayName", "rating", "userRatingCount", "priceLevel", "uri"]
      };
      
      // 发送POST请求到新版Places API
      const options = {
        method: "POST",
        contentType: "application/json",
        headers: {
          "X-Goog-Api-Key": API_KEY,
          "X-Goog-FieldMask": "places.displayName,places.rating,places.userRatingCount,places.priceLevel,places.uri"
        },
        payload: JSON.stringify(requestBody)
      };
      
      const response = UrlFetchApp.fetch(PLACES_SEARCH_ENDPOINT, options);
      const responseData = JSON.parse(response.getContentText());
      
      // 取匹配度最高的第一个结果
      if (responseData.places && responseData.places.length > 0) {
        const place = responseData.places[0];
        const rating = place.rating || "无数据";
        const reviewCount = place.userRatingCount || "无数据";
        const priceLevel = place.priceLevel ? `¥${"".repeat(place.priceLevel)}` : "无数据";
        const placeUri = place.uri || "无数据";
        
        // 写入结果到对应单元格
        sheet.getRange(i + 1, 3).setValue(rating);
        sheet.getRange(i + 1, 4).setValue(reviewCount);
        sheet.getRange(i + 1, 5).setValue(priceLevel);
        sheet.getRange(i + 1, 6).setValue(placeUri);
      } else {
        // 未找到匹配结果
        sheet.getRange(i + 1, 3, 1, 4).setValue("未找到匹配餐厅");
      }
      
      // 控制请求频率,避免触发API限流
      Utilities.sleep(1000);
      
    } catch (error) {
      // 捕获错误并标记
      sheet.getRange(i + 1, 3).setValue(`请求错误: ${error.message}`);
    }
  }
  
  SpreadsheetApp.getUi().alert("批量获取完成!");
}

代码关键说明

  • API请求设计:使用新版Places API的searchText端点,通过POST传递参数,fields和X-Goog-FieldMask精准指定返回字段,减少冗余数据传输
  • 搜索精度优化:结合名称+地址搜索,比单一名称搜索的匹配准确率更高
  • 错误与异常处理:捕获请求过程中的配额不足、网络错误等异常,在Sheet中直观标记
  • 限流控制:添加1秒延迟,避免短时间内大量请求触发API频率限制

注意事项

  • API配额监控:新版Places API有免费配额,超出后会产生费用,建议在Google Cloud控制台实时监控使用量
  • 字段扩展:如需获取营业时间、联系方式等更多字段,可修改fields和X-Goog-FieldMask的取值,参考官方字段列表调整
  • 表头适配:若你的Sheet没有表头,将循环起始的i = 1改为i = 0即可

内容的提问来源于stack exchange,提问作者Xenith

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 07:11:10