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

关于CardService Apps Script中离开上下文时自定义弹窗内容的技术求助

CardService Apps Script中离开上下文时自定义弹窗内容的技术求助

Hey Alex, great question—this is definitely one of those tricky scenarios where the official docs don’t spell things out clearly, so I feel your frustration!

I’ve messed around with similar requirements before, and while CardService doesn’t have a direct "trigger popup on Back button click" event, there are a couple of workarounds you can try:

1. Replace the default Back button with a custom one (most reliable)

The system’s native Back button is hard to intercept directly, but you can hide it and add your own custom Back button instead. This way, you can control exactly what happens when the user clicks it—including showing your custom message first.

Here’s a quick code snippet to get you started:

// 生成自定义返回按钮
function buildCustomBackButton() {
  const backAction = CardService.newAction()
    .setFunctionName('handleCustomBackClick');
  
  return CardService.newTextButton()
    .setText('Back')
    .setOnClickAction(backAction);
}

// 处理自定义返回按钮的点击事件
function handleCustomBackClick(e) {
  // 创建你要显示的提示内容
  const customNotification = CardService.newNotification()
    .setText('这里就是你想在离开时展示的文本啦!')
    .setType(CardService.NotificationType.INFO); // 可选设置提示类型,比如WARNING/SUCCESS
  
  // 导航回上一级上下文
  const navigation = CardService.newNavigation().popCard();
  
  // 返回响应:先显示提示,再跳转
  return CardService.newActionResponseBuilder()
    .setNotification(customNotification)
    .setNavigation(navigation)
    .build();
}

Just make sure to add this custom button to your card’s header or sections instead of relying on the native Back button.

2. Try the onClose event (hit-or-miss)

If you absolutely need to use the native Back button, you can experiment with the card’s onClose action. This triggers when the card is closed, which might include when the user clicks Back—but note that this behavior can vary depending on the client (Gmail web vs. mobile, etc.).

Here’s how that would look:

// 构建你的卡片时设置关闭事件
function buildMailContextCard() {
  const card = CardService.newCardBuilder()
    .setHeader(CardService.newCardHeader().setTitle('邮件操作卡片'))
    .addSection(CardService.newCardSection().addWidget(/* 你的卡片内容 */))
    .setOnCloseAction(CardService.newAction().setFunctionName('handleCardClose'))
    .build();
  
  return card;
}

// 处理卡片关闭事件
function handleCardClose(e) {
  const closeNotification = CardService.newNotification()
    .setText('你已经离开了邮件上下文哦!');
  
  return CardService.newActionResponseBuilder()
    .setNotification(closeNotification)
    .build();
}

Keep in mind that the onClose notification is a global toast-style message, not a popup, but it’s the closest you can get with native events.

A quick heads-up: Since Apps Script runs in a sandboxed environment, you can’t fully intercept native browser events like the system Back button. The custom button method is the most consistent way to get the behavior you want.

If neither of these work for your use case, you could also submit a feature request to the Google Workspace Issue Tracker—they do take user feedback into account for future updates!

备注:内容来源于stack exchange,提问作者Alex Nietsvietov

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.20 10:28:04