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

使用FormApp.openByUrl访问Google表单报错:无法找到指定ID项

问题分析与解决方案

核心问题

你从Classroom API获取的表单URL是表单填写页面链接(格式:https://docs.google.com/forms/d/e/{PUBLISH_ID}/viewform),其中的{PUBLISH_ID}并非Google Drive中表单文件的唯一ID,因此无法直接通过FormApp.openByUrl()或DriveApp.getFileById()访问表单资源——这就是你遇到权限/找不到项目错误的根本原因。

而你之前用SpreadsheetApp.getActiveSheet().getFormUrl()拿到的是表单的编辑页面链接(格式:https://docs.google.com/forms/d/{FILE_ID}/edit),包含了Drive文件ID,所以FormApp.openByUrl()可以正常识别。

可行解决方案

方案1:从填写链接提取真实表单文件ID

通过UrlFetchApp请求填写链接,解析响应HTML提取表单的Drive文件ID,再用ID访问表单:

// 从表单填写链接提取Drive文件ID
const extractFormFileId = (viewUrl) => {
  const response = UrlFetchApp.fetch(viewUrl);
  const htmlContent = response.getContentText();
  const idMatch = htmlContent.match(/"formId":"([^"]+)"/);
  
  if (idMatch && idMatch[1]) {
    return idMatch[1];
  }
  throw new Error("无法从填写链接中提取表单文件ID");
};

// 实际调用逻辑
const x = JSON.parse(materialsString);
if (x.link?.url) {
  try {
    const formFileId = extractFormFileId(x.link.url);
    const targetForm = FormApp.openById(formFileId);
    // 执行表单复制操作
    const copiedForm = targetForm.makeCopy("克隆的表单_" + new Date().toLocaleString());
    // 后续处理复制后的表单(比如更新Classroom链接)
  } catch (err) {
    console.error("处理表单时出错:", err.message);
  }
}

方案2:优先使用Classroom API返回的Drive文件ID

如果你的CourseWork材料是通过Drive文件添加的,Classroom API返回的materials对象中可能包含driveFile属性(而非仅link),直接使用该属性的id即可:

const x = JSON.parse(materialsString);
if (x.driveFile?.id) {
  const targetForm = FormApp.openById(x.driveFile.id);
  const copiedForm = targetForm.makeCopy("克隆的表单");
}

权限验证

确保你的脚本已启用以下服务:

  • 在脚本编辑器中,点击「扩展」→「Apps Script API」,确认已启用
  • 点击「服务」→「添加服务」,分别启用Drive API和Forms API

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.15 21:36:23