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

如何使用googleapis npm实现Google表单的接受响应开关功能?

解决方案

要通过Node.js的googleapis包实现Google表单的开启/关闭接受响应功能,核心是调用Forms API的batchUpdate方法,使用updateSettings请求类型修改表单配置:

  • 确保你的Google Cloud项目已启用Forms API,且授权凭证(OAuth2或服务账号)拥有forms.body.update权限,同时表单已共享给授权账号(服务账号需共享对应邮箱)。
  • 构造batchUpdate请求时,在requests数组中传入updateSettings对象,指定acceptingResponses状态(true开启,false关闭),并设置updateMask为"acceptingResponses"(必须指定,否则API会忽略更新)。

示例代码

const { google } = require('googleapis');

// 切换表单接受响应状态
async function toggleFormResponses(formId, acceptResponses) {
  // 初始化Forms客户端,auth为你的授权对象(如JWT/OAuth2客户端)
  const forms = google.forms({ version: 'v1', auth: YOUR_AUTH_OBJECT });

  const updateRequest = {
    formId,
    requestBody: {
      requests: [
        {
          updateSettings: {
            settings: { acceptingResponses },
            updateMask: 'acceptingResponses'
          }
        }
      ]
    }
  };

  try {
    const res = await forms.forms.batchUpdate(updateRequest);
    console.log('状态更新成功:', res.data);
    return res.data;
  } catch (err) {
    console.error('更新失败:', err.errors);
    throw err;
  }
}

// 使用示例:开启响应
toggleFormResponses('your-form-id', true);
// 关闭响应
// toggleFormResponses('your-form-id', false);

关键说明

  • updateMask是必填项,用于明确告知API要更新的字段,这里必须设为"acceptingResponses"。
  • 授权对象需正确配置:如果用服务账号,要将表单共享给服务账号的邮箱地址;如果用OAuth2,需确保用户已授权https://www.googleapis.com/auth/forms.body范围。

内容的提问来源于stack exchange,提问作者Daniel Fernández

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 22:12:11