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

Looker Studio社区连接器分步配置错误与参数获取问题

解决Looker Studio社区连接器的两个核心问题

1. 抑制configParams未定义错误

报错原因是首次调用getConfig时,request对象可能不存在configParams属性,直接读取会触发TypeError。

修复方案

替换原代码第2-4行,使用安全取值方式避免报错:

var config = cc.getConfig();
// 用可选链安全获取configParams,兼容首次请求场景
var configParams = request?.configParams;
var isFirstRequest = !configParams; // 简化判断:无configParams即为首次请求

如果你的Apps Script环境不支持可选链(?.),改用传统条件判断:

var configParams = (request && request.configParams) || {};
var isFirstRequest = Object.keys(configParams).length === 0;

2. 获取用户选中的GBP位置值

用户通过下拉框选中的位置值(即setValue(option.value)中的option.value)会自动存入request.configParams.gbpLocation(因为你给下拉组件设置的id是gbpLocation),后续在getData或其他需要调用API的函数中直接读取即可:

示例(在getData函数中使用)

function getData(request) {
  // 获取用户选中的GBP位置ID
  var selectedLocation = request.configParams.gbpLocation;
  
  // 调用GBP API时传入该值
  let gmbService = getOAuthService();
  var apiUrl = `https://mybusinessbusinessinformation.googleapis.com/v1/${selectedLocation}/metrics`; // 替换为目标API端点
  var response = UrlFetchApp.fetch(apiUrl, {
    headers: { Authorization: 'Bearer ' + gmbService.getAccessToken() }
  });
  
  // 后续数据处理逻辑...
}

额外优化

你的代码中var newOpts = opts.map(...)这段逻辑是无用的,可直接删除,不影响功能。

修改后的完整getConfig函数

function getConfig(request) {
  var config = cc.getConfig();
  // 安全获取configParams,避免未定义报错
  var configParams = request?.configParams;
  var isFirstRequest = !configParams;
  
  if (isFirstRequest) {
    config.setIsSteppedConfig(true);
  }

  // Initialize the OAuth2 service
  let gmbService = getOAuthService();

  var response = UrlFetchApp.fetch("https://mybusinessaccountmanagement.googleapis.com/v1/accounts", {
    headers: {
      Authorization: 'Bearer ' + gmbService.getAccessToken()
    }
  });

  //Strip the account ID out of the JSON output with a regex and cache
  var regExp = /[0-9]{10,40}/g;
  var accountId = regExp.exec(response);

  //With the account ID we pull a list of locations from GBP
  var bob = accountId[0];
  var requestURL = 'https://mybusinessbusinessinformation.googleapis.com/v1/accounts/' + bob + '/locations?readMask=Title,storefrontAddress,name&pageSize=100';
  var locationList = UrlFetchApp.fetch(requestURL, {
    headers: { Authorization: 'Bearer ' + gmbService.getAccessToken() },
    muteHttpExceptions: true
  });

  //Parse the response into a Javascript object
  var parsedLocationList = JSON.parse(locationList);

  //Create object array of location data options
  var opts = []
  //Loop through the location data and add the relevant fields of each item to the option array
  var i = 0;

  parsedLocationList.locations.forEach(function (opt) {
    var city = "Service Area Only";
    if (typeof opt.storefrontAddress !== 'undefined') { city = opt.storefrontAddress.locality; }
    opts[i] = {
      "label": opt.title + " | " + city, //Use city name to differentiate same-named businesses
      "value": opt.name
    };
    i++;
  });

  //Alphabetize the options
  opts.sort(function (a, b) {
    var nameA = a.label.toUpperCase(); // ignore upper and lowercase
    var nameB = b.label.toUpperCase(); // ignore upper and lowercase
    if (nameA < nameB) {
      return -1;
    }
    if (nameA > nameB) {
      return 1;
    }
    // case: names are equal
    return 0;
  });

  config.setDateRangeRequired(true);
  config.newInfo()
    .setId('instructions')
    .setText('Hello Tasty, Please Select a Business Location.');

  config.newTextInput()
    .setId('package')
    .setName('Success, You Have Connected to the Following Account:')
    .setHelpText('...there is no help here.')
    .setPlaceholder(bob);

  var selectConfig = config.newSelectSingle()
    .setId('gbpLocation')
    .setName('Choose a Gooogle Busines Profile from the Drop Down: ')
    .setHelpText('...there is no help here.')
    .setAllowOverride(true)
  opts.forEach(function (option) {
    selectConfig.addOption(
      config.newOptionBuilder()
        .setLabel(option.label)
        .setValue(option.value)
    );
  });

  if (!isFirstRequest) {
    config.setIsSteppedConfig(false);
  };

  return config.build();
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 10:23:23