How to automatically update the UI after user updates a userProperty in Google Apps Script Workspace Add-ons for Google Sheets?

huangapple go评论67阅读模式
英文:

How to automatically update the UI after user updates a userProperty in Google Apps Script Workspace Add-ons for Google Sheets?

问题

我希望能自动更新用户在提交用户输入后更新设置后的UI设置。

目前,我正在尝试使用CardService中的updateCard()方法,如下所示。文档在此,但它们不包含任何示例代码。

我期望在用户提供输入并提交后,当前的卡片将被替换为一个包含新设置的更新卡片。

然而,实际发生的情况是,我期望更新的卡片并没有自动更新。要查看更改,用户必须手动刷新应用主页。之后,仅在手动刷新后,主页卡片才会更新为新的设置。

如何在不需要手动刷新的情况下自动更新主页?

Code.gs
const changedProperty = PropertiesService.getUserProperties().getProperty( MY_SETTING );
const newNavigation = CardService.newNavigation();
const cardPoppedToRoot = newNavigation.popToRoot();
const homepageCard = getCardFromUiConfig( HOMEPAGE_UI_CONFIG, );
const updatedCard = cardPoppedToRoot.updateCard( homepageCard, );
return updatedCard;

我还尝试了以下代码根据此答案,结果与上述代码完全相同。

Code.gs
return CardService.newActionResponseBuilder()
  .setNavigation(
    CardService.newNavigation()
      .popToRoot()
      .updateCard( homepageCard, )
  ).build();

当我尝试按照答案中所示配置我的appsscript.json文件如下:

appsscript.json
      "homepageTrigger": {
        "runFunction": "onHomepage"
      },
      "contextualTriggers":[
        {
          "unconditional":{},
          "onTriggerFunction": "onHomepage"
        }
      ]

我会得到以下错误:

"appsscript.json"存在错误:无效清单:未知字段:[addOns.common.contextualTriggers]

英文:

I want to automagically update the UI with a new user setting after the user updates that setting by submitting a user input from.

Currently, I am attempting to use the updateCard() method from the CardService as shown in the below code. The docs are here but they do not contain any example code.

I expect that after the user provides the input and submits it, the current card will be replaced by an updated card that will contain the new setting.

However, what’s actually happening is that the card I expect to update is not updating automatically. To see the change, the user has to manually refresh the app homepage. After, and only after, a manual refresh, does the homepage card update with the new setting.

How do I update the homepage automatically without requiring a manual refresh?

Code.gs
const changedProperty = PropertiesService.getUserProperties().getProperty( MY_SETTING );
const newNavigation = CardService.newNavigation();
const cardPoppedToRoot = newNavigation.popToRoot();
const homepageCard = getCardFromUiConfig( HOMEPAGE_UI_CONFIG, );
const updatedCard = cardPoppedToRoot.updateCard( homepageCard, );
return updatedCard;

I also tried the following code per this answer and the results are exactly the same as with the above code.

Code.gs
return CardService.newActionResponseBuilder()
  .setNavigation(
    CardService.newNavigation()
      .popToRoot()
      .updateCard( homepageCard, )
  ).build();

When I try to configure my appsscript.json file as shown in the answer as follows:

appsscript.json
      "homepageTrigger": {
        "runFunction": "onHomepage"
      },
      "contextualTriggers":[
        {
          "unconditional":{},
          "onTriggerFunction": "onHomepage"
        }
      ]

I get the following error:

> "appsscript.json" has errors: Invalid manifest: unknown fields: [addOns.common.contextualTriggers]

答案1

得分: 2

contextualTriggers 只能用于 Gmail 添加组件。

contextualTriggers 不能是 common 的子级。

来自 https://developers.google.com/apps-script/manifest/addons#common(不包括链接):

Common

适用于每个主机应用程序的参数的清单配置。这里定义的一些值在特定主机的特定值被省略时用作默认值。

{
  "homepageTrigger": {
    object (HomepageTrigger)
  },
  "layoutProperties": {
    object (LayoutProperties)
  },
  "logoUrl": string,
  "name": string,
  "openLinkUrlPrefixes": [
    string
  ],
  "universalActions": [
    {
      object (UniversalAction)
    }
  ],
  "useLocaleFromApp": boolean
}

据我所知,contextualTriggers 只能与 Gmail 添加组件一起使用。来自 https://developers.google.com/apps-script/manifest/gmail-addons(不包括链接):

Gmail

Gmail 扩展的 Google Workspace 添加组件清单配置。有关更多信息,请参阅使用 Google Workspace 添加组件扩展 Gmail。

{
  "authorizationCheckFunction": string,
  "composeTrigger": {
    object (ComposeTrigger)
  },
  "contextualTriggers": [
    {
      object (ContextualTrigger)
    }
  ],
  "homepageTrigger": {
    object (HomepageTrigger)
  }
}

相关链接

英文:

I think that that is only possible for Gmail add-ons.

<hr>

contextualTriggers can't be child of common.

From https://developers.google.com/apps-script/manifest/addons#common (links not included):

> ### Common
> The manifest configuration for parameters that are common for every host application. Some values defined here are used as a default when specific values for a particular host are omitted.
>
>
&gt; {
&gt; &quot;homepageTrigger&quot;: {
&gt; object (HomepageTrigger)
&gt; },
&gt; &quot;layoutProperties&quot;: {
&gt; object (LayoutProperties)
&gt; },
&gt; &quot;logoUrl&quot;: string,
&gt; &quot;name&quot;: string,
&gt; &quot;openLinkUrlPrefixes&quot;: [
&gt; string
&gt; ],
&gt; &quot;universalActions&quot;: [
&gt; {
&gt; object (UniversalAction)
&gt; }
&gt; ],
&gt; &quot;useLocaleFromApp&quot;: boolean
&gt; }
&gt;

AFAIK contextualTriggers can only be used with Gmail add-ons. From https://developers.google.com/apps-script/manifest/gmail-addons (links not included):

> <h3>Gmail</h3>
>
> The Google Workspace add-on manifest configuration for Gmail extensions. See Extending Gmail with Google Workspace add-ons for more information.
>
>
&gt; {
&gt; &quot;authorizationCheckFunction&quot;: string,
&gt; &quot;composeTrigger&quot;: {
&gt; object (ComposeTrigger)
&gt; },
&gt; &quot;contextualTriggers&quot;: [
&gt; {
&gt; object (ContextualTrigger)
&gt; }
&gt; ],
&gt; &quot;homepageTrigger&quot;: {
&gt; object (HomepageTrigger)
&gt; }
&gt; }
&gt;

Related

huangapple
  • 本文由 发表于 2023年2月6日 09:52:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75356730.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定