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

自定义Gmail插件打开日历、创建事件并传参的可行性咨询

能否从Gmail插件中打开日历并创建带参数的事件?

当然可以实现!你完全可以通过自定义Gmail插件触发日历事件的创建,不管是直接后台生成事件,还是打开预填好标题、描述的日历页面,都有成熟的方案。下面给你拆解两种常用的实现思路:

方案1:直接通过Calendar API创建事件(后台自动化)

这种方式不需要跳转日历页面,直接在Gmail插件的后端调用Google Calendar API完成事件创建,还能跳转到已创建的事件详情页。

步骤要点:

  • 首先在你的Gmail插件 manifest 文件中添加Calendar API的权限,确保插件能访问日历:
    {
      "oauthScopes": [
        "https://www.googleapis.com/auth/gmail.addons.execute",
        "https://www.googleapis.com/auth/calendar.events"
      ]
    }
    
  • 用Apps Script(如果是基于Google Workspace的插件)编写事件创建逻辑,示例代码如下:
    function createCalendarEvent(title, description) {
      // 定义事件的基本信息,你可以根据需求补充时间、参会人等字段
      const event = {
        summary: title,
        description: description,
        start: {
          dateTime: new Date().toISOString(),
          timeZone: Session.getScriptTimeZone()
        },
        end: {
          dateTime: new Date(Date.now() + 3600000).toISOString(), // 默认1小时后结束
          timeZone: Session.getScriptTimeZone()
        }
      };
      // 插入事件到主日历
      const createdEvent = Calendar.Events.insert(event, 'primary');
      return createdEvent.htmlLink; // 返回事件的网页链接
    }
    
  • 在插件的前端卡片中添加按钮,触发这个创建逻辑,完成后可以跳转到事件页面:
    function buildAddonCard() {
      // 这里可以从邮件内容提取标题、描述作为事件参数
      const eventTitle = "从Gmail生成的会议";
      const eventDesc = "事件内容来自Gmail邮件,可自定义编辑";
    
      const createAction = CardService.newAction()
        .setFunctionName('createAndOpenEvent')
        .setParameters({title: eventTitle, desc: eventDesc});
        
      const createButton = CardService.newTextButton()
        .setText("一键创建日历事件")
        .setOnClickAction(createAction);
    
      const section = CardService.newCardSection().addWidget(createButton);
      return CardService.newCardBuilder().addSection(section).build();
    }
    
    function createAndOpenEvent(e) {
      const eventLink = createCalendarEvent(e.parameters.title, e.parameters.desc);
      return CardService.newActionResponseBuilder()
        .setOpenLink(CardService.newOpenLink().setUrl(eventLink))
        .build();
    }
    

方案2:打开日历预填页面(让用户手动确认修改)

如果你希望用户能在创建前调整事件细节,可以生成带参数的Calendar链接,直接打开预填好标题和描述的事件编辑页面。

实现方式:

Google Calendar支持通过URL参数预填事件信息,核心格式如下:

https://calendar.google.com/calendar/u/0/r/eventedit?text=【标题内容】&details=【描述内容】

你只需要把标题和描述用encodeURIComponent()编码后替换到对应位置,然后在Gmail插件中用Action.openLink()打开这个链接即可。

示例代码:

function buildAddonCard() {
  const eventTitle = "预填的会议标题";
  const eventDesc = "这是从Gmail插件传递过来的事件描述";
  
  // 生成带参数的日历链接
  const calendarUrl = `https://calendar.google.com/calendar/u/0/r/eventedit?text=${encodeURIComponent(eventTitle)}&details=${encodeURIComponent(eventDesc)}`;
  
  const openCalendarAction = CardService.newAction()
    .setOpenLink(CardService.newOpenLink()
      .setUrl(calendarUrl)
      .setOpenAs(CardService.OpenAs.FULL_SIZE));
      
  const openButton = CardService.newTextButton()
    .setText("打开日历编辑事件")
    .setOnClickAction(openCalendarAction);

  const section = CardService.newCardSection().addWidget(openButton);
  return CardService.newCardBuilder().addSection(section).build();
}

两种方案对比

  • API创建:自动化程度高,用户无需手动操作,适合固定模板的事件;
  • 预填页面:保留用户的编辑权限,更灵活,适合需要调整时间、添加参会人的场景。

内容的提问来源于stack exchange,提问作者Freddy Daniel

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.11 09:20:37