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

使用Google Apps Script调用ClickUp API时遇ReferenceError: URLSearchParams未定义

问题解决:Google Apps Script中URLSearchParams未定义及请求错误

错误原因

  1. Google Apps Script基于Rhino旧版引擎运行,不支持浏览器/Node.js环境中的URLSearchParams API。
  2. 代码中直接使用的fetch函数在GAS环境中不存在,需改用GAS内置的UrlFetchApp.fetch()方法发起网络请求。

修正后的代码

function getTime() {
  // 定义查询参数集合
  const params = {
    start_date: '0',
    end_date: '0',
    assignee: '0',
    include_task_tags: 'true',
    include_location_names: 'true',
    space_id: '0',
    folder_id: '0',
    list_id: '0',
    task_id: '0',
    custom_task_ids: 'true',
    team_id: '123'
  };
  
  // 手动构建URL查询字符串,对参数键值进行URL编码
  const queryString = Object.entries(params)
    .map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`)
    .join('&');
  
  const teamId = '123';
  const apiUrl = `https://api.clickup.com/api/v2/team/${teamId}/time_entries?${queryString}`;
  
  // GAS标准请求配置
  const requestOptions = {
    method: 'GET',
    headers: {
      'Content-Type': 'application/json',
      Authorization: 'tk_49237802_NCN'
    }
  };
  
  // 发起请求并处理响应
  const response = UrlFetchApp.fetch(apiUrl, requestOptions);
  const responseData = response.getContentText();
  console.log(responseData);
}

关键说明

  • 用Object.entries()遍历参数对象,结合encodeURIComponent()对每个键值对进行URL编码,避免参数中特殊字符导致的请求错误。
  • UrlFetchApp.fetch()是GAS中发起HTTP请求的官方方法,返回HTTPResponse对象,通过getContentText()获取响应文本;若返回JSON格式,可直接用getJSON()解析为JavaScript对象。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 12:50:25