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

Google Apps Script超MAXIMUM_RUNNING_TIME及API配额矛盾求助

解决Google Url Shortener API配额与GAS运行时间冲突的方案

嘿,这个两难的问题我之前帮朋友处理过,刚好有几个实用的解决思路,帮你梳理下:

1. 优先用API批量请求功能(最有效)

Google Url Shortener API其实支持批量提交多个URL的,不用一个个单独调用——这能直接把调用次数降到原来的几十分之一,自然就不用为了配额放慢脚本,也就不会触发运行时间上限了。

你可以用Google的批量API端点https://www.googleapis.com/batch,一次打包多个缩短请求。下面是个GAS的示例代码:

function batchShortenUrls() {
  // 替换成你的待缩短URL列表(可以从Sheet读取)
  const urlsToShorten = ["https://example.com/page1", "https://example.com/page2", "https://example.com/page3"];
  
  // 构造批量请求数组
  const batchRequests = urlsToShorten.map(url => ({
    method: "POST",
    url: "https://www.googleapis.com/urlshortener/v1/url",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({ longUrl: url })
  }));

  // 发送批量请求
  const options = {
    method: "POST",
    contentType: "application/json",
    payload: JSON.stringify({ batch: batchRequests }),
    headers: { "Authorization": "Bearer " + ScriptApp.getOAuthToken() }
  };

  const response = UrlFetchApp.fetch("https://www.googleapis.com/batch", options);
  const rawResult = response.getContentText();
  
  // 解析返回结果(批量响应是multipart格式,需要拆分提取每个短链接)
  const results = rawResult.split("--batch_").slice(1).map(part => {
    const jsonPart = part.match(/\{.*\}/s)[0];
    return JSON.parse(jsonPart).id;
  });

  // 这里可以把结果写入Sheet或者做其他处理
  console.log("短链接结果:", results);
}

注意:批量请求的单次数量建议控制在50-100个左右,避免触发其他配额限制。

2. 拆分任务+时间驱动触发器

如果批量请求还是满足不了你的需求(比如URL数量特别多),可以把任务拆成多个小批次,用GAS的时间驱动触发器分时段执行,每次运行都控制在MAXIMUM_RUNNING_TIME(当前是6分钟)以内。

步骤如下:

  • 用PropertiesService存储任务进度(比如已处理到第几个URL)
  • 每次运行只处理一小批URL(比如50个)
  • 设置触发器每隔一段时间(比如10分钟)自动运行一次,直到所有任务完成

示例代码:

function processUrlsInBatches() {
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("URL列表");
  const urls = sheet.getRange(2, 1, sheet.getLastRow()-1, 1).getValues().flat();
  const scriptProps = PropertiesService.getScriptProperties();
  
  // 获取上次处理到的索引,默认从0开始
  let lastProcessed = parseInt(scriptProps.getProperty("lastProcessedIndex")) || 0;
  const batchSize = 50; // 每次处理50个,可根据实际调整
  const endIndex = Math.min(lastProcessed + batchSize, urls.length);

  // 处理当前批次的URL
  for (let i = lastProcessed; i < endIndex; i++) {
    const longUrl = urls[i];
    try {
      const shortUrl = shortenSingleUrl(longUrl);
      // 把短链接写入Sheet的第二列
      sheet.getRange(i+2, 2).setValue(shortUrl);
    } catch (e) {
      console.log(`处理URL失败:${longUrl},错误:${e}`);
    }
  }

  // 更新进度或标记任务完成
  if (endIndex < urls.length) {
    scriptProps.setProperty("lastProcessedIndex", endIndex);
  } else {
    scriptProps.deleteProperty("lastProcessedIndex");
    SpreadsheetApp.getUi().alert("所有URL处理完成!");
  }
}

// 单个URL缩短函数
function shortenSingleUrl(longUrl) {
  const options = {
    method: "POST",
    contentType: "application/json",
    payload: JSON.stringify({ longUrl: longUrl }),
    headers: { "Authorization": "Bearer " + ScriptApp.getOAuthToken() }
  };
  const response = UrlFetchApp.fetch("https://www.googleapis.com/urlshortener/v1/url", options);
  return JSON.parse(response.getContentText()).id;
}

创建触发器的方法:
在脚本编辑器中,点击「编辑」→「当前项目的触发器」→「添加触发器」,选择:

  • 运行函数:processUrlsInBatches
  • 事件源:时间驱动
  • 类型:分钟计时器(或小时计时器)
  • 间隔:每10分钟(足够让当前批次处理完成,且不会触发API配额)

3. 备选:改用其他短链接API(如果允许)

如果业务上不限制必须用Google的服务,可以考虑切换到配额更宽松的短链接API,比如Bitly或TinyURL——它们的单用户调用限制通常更高,甚至没有严格的每秒1次限制,这样脚本不用刻意放慢速度,自然不会超时。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 08:01:41