代码之家  ›  专栏  ›  技术社区  ›  GN.

谷歌应用程序脚本:困惑于如何更新卡

  •  0
  • GN.  · 技术社区  · 4 年前

    如何更新中的数据 PropertiesService 并在另一张卡片上显示?

    数据总是显得陈旧,或者卡/UI陈旧。

    例如

    1. 为谷歌日历设置一个项目。运行buildInitState函数对其进行初始化
    // appsscript.json
    // set the trigger to build the home
    ....
    ....
     "addOns": {
        "calendar": {
          "homepageTrigger": {
            "enabled": true,
            "runFunction": "buildInitState"
          }
        },
        "common": {
          ....
    ....
    
    1. 初始化函数以构建家庭卡
    const userStore = PropertiesService.getUserProperties();
    
    // setup userStore and add default values for date 
    userStore.setProperties({
      date: (new Date()).getTime(),
    });
    
    const showSettingsPageAction =
      CardService.newAction().setFunctionName('showSettingsPage');
    
    const handleDateChangeAction =
      CardService.newAction().setFunctionName('handleDateChange');
    
    function handleDateChange(e) {
      //change the date to what user selects
      const newDate = (new Date(e.formInput.date_field.msSinceEpoch)).getTime();
      userStore.setProperty('date', newDate);
    }
    
    // Homepage card
    function buildInitState(e) {
      const mainCard = CardService.newCardBuilder();
    
      // add a date picker
      const DatePicker = CardService.newDatePicker()
        .setTitle('set date')
        .setFieldName('date_field')
        .setValueInMsSinceEpoch((new Date()).getTime())
        .setOnChangeAction(
          CardService.newAction().setFunctionName('handleDateChangeAction')
        );
    
      // add a button to nav to another card
      const ButtonFindSlots = CardService.newTextButton()
        .setText('NAV TO SETTINGS CARD')
        .setOnClickAction(showSettingsPageAction);
    
      mainCard
      .addSection(DatePicker)
      .addSection(ButtonFindSlots)
    
      mainCard.build();
    }
    
    1. 这个 showSettingsPage 制作一张新卡。
    function showSettingsPage() {
      const card = CardService.newCardBuilder();
      const cardSection = CardService.newCardSection();
    
      let content = userStore.getProperty('date');
      let p = CardService.newTextParagraph().setText(content);
    
      card.addSection(cardSection);
      return card.build();
    }
    

    即使用户更改了家庭卡中的日期并导航到设置页面,日期也始终显示默认日期值。

    我不确定是数据没有更新还是卡没有更新。我试着阅读 docs on action responses 但我不明白这是怎么回事。

    0 回复  |  直到 4 年前