google-apps-scriptgoogle-workspace-add-ons

CardService.newAlert() does not work in my Add-In-Training-Script for GMail


I am currently practicing my knowledge of Apps Script for Gmail and have written the attached script. It starts up without any problems, but when I click on a button, I always get the error

TypeError: CardService.newAlert is not a function.

According to Google's input help, this option is available, and according to Gemini, the script is correct—so why am I still getting the error in lines 41 and 51?

Does anyone have any tips on where I might be going wrong, or could you modify my script accordingly with a brief explanation?

Here is the link to my Apps Script: https://script.google.com/d/1yqJe1iykzFvyaov0--cNcygoSYNP0a0UJczkB5523jx_Hg3Zqm4vtlx8/edit?usp=sharing

/**
 * Dies ist die Hauptfunktion, die aufgerufen wird, wenn das Add-on geöffnet wird.
 * Es erstellt die Benutzeroberfläche des Add-ons mit zwei Buttons.
 * @param {Object} e Das Event-Objekt, das Kontextinformationen enthält.
 * @return {Card} Die Card, die die UI des Add-ons darstellt.
 */
function onOpen(e) {
  // Erstellen Sie einen neuen CardBuilder, um die Hauptkarte des Add-ons zu erstellen.
  const cardBuilder = CardService.newCardBuilder();

  // Erstellen Sie einen Abschnitt für die Buttons.
  const section = CardService.newCardSection().setHeader('Wählen Sie eine Aktion');

  // Erstellen Sie den ersten Button mit der Aufschrift "Hallo".
  const halloButton = CardService.newTextButton()
      .setText('Hallo')
      // Rufen Sie die Funktion 'showAlertHallo' auf, wenn dieser Button geklickt wird.
      .setOnClickAction(CardService.newAction().setFunctionName('showAlertHallo'));

  // Erstellen Sie den zweiten Button mit der Aufschrift "Fertig".
  const fertigButton = CardService.newTextButton()
      .setText('Fertig')
      // Rufen Sie die Funktion 'showAlertFertig' auf, wenn dieser Button geklickt wird.
      .setOnClickAction(CardService.newAction().setFunctionName('showAlertFertig'));

  // Fügen Sie die Buttons zum Abschnitt hinzu.
  section.addWidget(halloButton);
  section.addWidget(fertigButton);
  
  // Fügen Sie den Abschnitt zur Card hinzu und bauen Sie die Card.
  cardBuilder.addSection(section);
  return cardBuilder.build();
}

/**
 * Diese Funktion wird aufgerufen, wenn der "Hallo"-Button geklickt wird.
 * Sie erstellt eine Alert-Meldung mit dem Text "Hallo".
 * @return {ActionResponse} Die Antwort, die die Alert-Meldung anzeigt.
 */
function showAlertHallo() {
  const alert = CardService.newAlert().setText('Hallo');
  return CardService.newActionResponseBuilder().setAlert(alert).build();
}

/**
 * Diese Funktion wird aufgerufen, wenn der "Fertig"-Button geklickt wird.
 * Sie erstellt eine Alert-Meldung mit dem Text "Fertig".
 * @return {ActionResponse} Die Antwort, die die Alert-Meldung anzeigt.
 */
function showAlertFertig() {
  const alert = CardService.newAlert().setText('Fertig');
  return CardService.newActionResponseBuilder().setAlert(alert).build();
}

The appsscript.json-File is very simple:

{
  "timeZone": "Europe/Berlin",
  "dependencies": {
  },
  "exceptionLogging": "STACKDRIVER",
  "runtimeVersion": "V8",
  "addOns": {
    "common": {
      "name": "Stefans Add-on",
      "logoUrl": "https://www.gstatic.com/images/icons/material/system/2x/label_googblue_48dp.png"
    },
    "gmail": {
      "contextualTriggers": [{
        "unconditional": {
        },
        "onTriggerFunction": "onOpen"
      }]
    }
  }
}

Ich checked the Google-Docus an Gemini.


Solution

  • Add-On using Card Service

    Upon checking the available public documentation, there is no mention of the method CardService.newAlert(). Please remember that one of the disadvantages of using Generative AI like Gemini is AI hallucinations which produces response that is not really in true or possible.

    Based on how I understand your code, a method that you can use is Notification See sample below:

    Sample Code

    /**
     * Dies ist die Hauptfunktion, die aufgerufen wird, wenn das Add-on geöffnet wird.
     * Es erstellt die Benutzeroberfläche des Add-ons mit zwei Buttons.
     * @param {Object} e Das Event-Objekt, das Kontextinformationen enthält.
     * @return {Card} Die Card, die die UI des Add-ons darstellt.
     */
    function onOpen(e) {
      // Erstellen Sie einen neuen CardBuilder, um die Hauptkarte des Add-ons zu erstellen.
      const cardBuilder = CardService.newCardBuilder();
    
      // Erstellen Sie einen Abschnitt für die Buttons.
      const section = CardService.newCardSection().setHeader('Wählen Sie eine Aktion');
    
      // Erstellen Sie den ersten Button mit der Aufschrift "Hallo".
      const halloButton = CardService.newTextButton()
          .setText('Hallo')
          // Rufen Sie die Funktion 'showAlertHallo' auf, wenn dieser Button geklickt wird.
          .setOnClickAction(CardService.newAction().setFunctionName('showAlertHallo'));
    
      // Erstellen Sie den zweiten Button mit der Aufschrift "Fertig".
      const fertigButton = CardService.newTextButton()
          .setText('Fertig')
          // Rufen Sie die Funktion 'showAlertFertig' auf, wenn dieser Button geklickt wird.
          .setOnClickAction(CardService.newAction().setFunctionName('showAlertFertig'));
    
      // Fügen Sie die Buttons zum Abschnitt hinzu.
      section.addWidget(halloButton);
      section.addWidget(fertigButton);
      
      // Fügen Sie den Abschnitt zur Card hinzu und bauen Sie die Card.
      cardBuilder.addSection(section);
      return cardBuilder.build();
    }
    
    /**
     * Diese Funktion wird aufgerufen, wenn der "Hallo"-Button geklickt wird.
     * Sie erstellt eine Alert-Meldung mit dem Text "Hallo".
     * @return {ActionResponse} Die Antwort, die die Alert-Meldung anzeigt.
     */
    
    function showAlertHallo() {
      return CardService.newActionResponseBuilder()
          .setNotification(
              CardService.newNotification().setText('Hallo'),
              )
          .build();
    }
    
    function showAlertFertig() {
      return CardService.newActionResponseBuilder()
          .setNotification(
              CardService.newNotification().setText('Fartig'),
              )
          .build();
    }
    

    References: Class Notification