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

Google Data Studio社区连接器:如何通过报告字段/过滤器调用API查询

Hey there! Let's get that string-based filtering working for your Community Connector. I’ve built a few of these before, so here’s a clear breakdown to fix your setup and get the report-level filtering you need:

Step 1: Set Up a Filterable Field in Your Schema (For Native Report Filters)

First, if you want to use Data Studio’s built-in report filters (instead of config params), you need to mark the relevant field as filterable in your connector’s schema. This lets users add filters directly in the report editor, not just the data source settings.

Here’s how to update your getSchema() function:

function getSchema(request) {
  return {
    schema: [
      {
        name: "your_target_field",
        label: "Field to Search",
        dataType: "STRING",
        filterable: true // This enables the filter UI for this field in Data Studio
      },
      // Add all your other existing fields here
    ]
  };
}
Step 2: Capture the Filter Value in Your getData() Function

Unlike config params, report filter values live in the request.dimensionFilters object (for string/dimension fields). You’ll need to parse this to extract the user’s input string, then pass it to your API.

Here’s a code snippet to handle this:

function getData(request) {
  // Initialize the search value as null
  let searchString = null;

  // Look for a "CONTAINS" filter on your target field
  if (request.dimensionFilters && request.dimensionFilters.length > 0) {
    const matchingFilter = request.dimensionFilters.find(filter => 
      filter.fieldName === "your_target_field" && filter.operator === "CONTAINS"
    );
    
    if (matchingFilter) {
      searchString = matchingFilter.values[0]; // Grab the user's input string
    }
  }

  // Build your API request with the search string (if provided)
  let apiUrl = "https://your-api-endpoint.com/your-data-endpoint";
  if (searchString) {
    // Adjust the query parameter to match your API's expected key (e.g., "q", "search", etc.)
    apiUrl += `?search=${encodeURIComponent(searchString)}`;
  }

  // Fetch and process your API data as you already do
  const apiResponse = UrlFetchApp.fetch(apiUrl);
  const rawData = JSON.parse(apiResponse.getContentText());
  
  // Map raw data to your Data Studio schema format (your existing logic here)
  const formattedData = rawData.map(item => ({
    your_target_field: item.relevantApiField,
    // Map other fields...
  }));

  return {
    schema: getSchema(request).schema,
    rows: formattedData
  };
}
Step 3: Fixing the Config Params Approach (If You Prefer That)

If you were using request.configParams.field_name and ran into issues, the most common problem is that config params are data-source level, not report-level. That means all reports using this data source will share the same filter value, and users can’t adjust it without editing the data source settings.

If you still want to use a dedicated input field in the data source settings, make sure you’ve defined it in getConfig() first:

function getConfig(request) {
  return {
    configParams: [
      {
        name: "custom_search_string",
        label: "Enter Search Term",
        type: "STRING",
        placeholder: "Type to filter API results..."
      }
    ]
  };
}

Then in getData(), you can grab it like this (and make sure to handle empty values):

const searchString = request.configParams.custom_search_string || null;
Key Notes to Avoid Headaches
  • Always use encodeURIComponent() when passing user input to your API to avoid URL parsing errors.
  • Test both filter operators (like "CONTAINS", "EXACT", "STARTS_WITH") if your API supports them—just adjust the filter.operator check in the code.
  • If your API expects the search string in the request body instead of a query param, modify the UrlFetchApp.fetch() call to include a payload with the search value.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 03:54:06