为何Google语言设为中文时Session.getActiveUserLocale()返回空字符串
问题说明
这是Google Workspace插件V8运行环境下的已知兼容性问题,仅在用户账号设置为简体中文(zh-CN)、繁体中文(zh-TW/zh-HK)时复现:Session.getActiveUserLocale() 会返回空值,其他语言区域不受影响,目前该问题尚未被官方修复。
解决方案
你可以通过入口函数统一缓存区域值的方式避免全链路传参,不需要修改现有业务逻辑的调用结构,具体实现如下:
- 方案1:全局变量缓存(同一次请求生命周期内有效)
所有插件入口触发函数中统一存入区域值到全局变量,再封装工具函数全局调用:
// 全局变量存储区域值 let globalUserLocale = null; // 首页打开入口 function onCalendarHomePageOpen(e) { globalUserLocale = e.commonEventObject.userLocale; // 原有业务逻辑 } // 其他入口函数同步赋值,比如事件打开触发函数 function onEventOpen(e) { globalUserLocale = e.commonEventObject.userLocale; // 原有业务逻辑 } // 统一获取区域的工具函数,业务代码直接调用即可 function getActiveUserLocale() { return globalUserLocale || 'zh-CN'; // 可自定义兜底默认值 }
- 方案2:持久化缓存(跨请求有效)
如果需要避免每次请求都重复赋值,可以将区域值存入用户属性缓存:
// 入口处缓存 function onCalendarHomePageOpen(e) { const locale = e.commonEventObject.userLocale; PropertiesService.getUserProperties().setProperty('userLocale', locale); // 原有业务逻辑 } // 统一获取工具函数 function getActiveUserLocale() { const cachedLocale = PropertiesService.getUserProperties().getProperty('userLocale'); return cachedLocale || 'zh-CN'; }
内容的提问来源于stack exchange,提问作者jazzwhistle
相关产品推荐
相关产品推荐

