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

GAS UserProperties部署Web App时用户数据互相覆盖问题咨询

问题结论

该现象不属于Bug,是对PropertiesService中User Properties的作用域规则理解偏差导致的预期不符。

核心原因

  • User Properties的作用域是脚本所属开发者账号 + 拥有脚本编辑权限的运行用户的组合,并非面向所有访问Web App的外部用户独立分配存储空间。你部署的Web App的脚本归属你的个人Google账号,所有外部用户访问时调用getUserProperties()实际访问的都是同一份属于开发者的User Properties空间,因此新用户写入的数据会覆盖之前的内容。
  • 你提供的复现代码存在隐式错误:首次运行时getProperty('userData')返回null,JSON.parse(null)得到null,后续执行'foo' in userData会直接抛出类型错误,需要补充兜底逻辑。

正确实现方案

如果需要为每个访问用户存储独立数据,可将用户唯一标识(如登录邮箱)拼接到属性键中,使用Script Properties存储即可,参考代码如下:

function doGet(e) {
  const scriptProps = PropertiesService.getScriptProperties();
  const userEmail = Session.getActiveUser().getEmail();
  // 拼接用户专属属性键,避免不同用户数据冲突
  const userPropKey = `userData_${userEmail}`;
  // 兜底空对象,避免首次运行解析报错
  let userData = JSON.parse(scriptProps.getProperty(userPropKey) || '{}');
  if (!('foo' in userData)) {
    userData = {foo: e.parameters.userData}
    scriptProps.setProperty(userPropKey, JSON.stringify(userData));
  }
  return HtmlService.createHtmlOutput(`<p>Expected data specific to ${userEmail}:</p><p>${userData.foo}`);
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.02 06:54:02