Azioni nel calendario

Gli oggetti Action ti consentono di integrare un comportamento interattivo nei componenti aggiuntivi di Google Workspace. Definiscono cosa succede quando un utente interagisce con un widget (ad esempio un pulsante) nell'interfaccia utente del componente aggiuntivo.

Un'azione viene associata a un determinato widget utilizzando una funzione di gestione dei widget, che definisce anche la condizione che attiva l'azione. Quando viene attivata, l'azione esegue una funzione di callback designata. Alla funzione di callback viene passato un oggetto evento che contiene informazioni sulle interazioni lato client dell'utente. Devi implementare la funzione di callback e farle restituire un oggetto di risposta specifico.

Ad esempio, supponiamo che tu voglia un pulsante che crei e mostri una nuova scheda quando viene cliccato. A questo scopo, devi creare un nuovo widget pulsante e utilizzare la funzione di gestione del widget pulsante setOnClickAction(action) per impostare un Action per la creazione di schede. Il Action che definisci specifica una funzione di callback Apps Script che viene eseguita quando viene fatto clic sul pulsante. In questo caso, implementi la funzione di callback per creare la scheda che vuoi e restituire un oggetto ActionResponse. L'oggetto risposta indica al componente aggiuntivo di visualizzare la scheda creata dalla funzione di callback.

Questa pagina descrive le azioni specifiche dei widget di Calendar che puoi includere nel tuo componente aggiuntivo.

Interazioni con il calendario

I componenti aggiuntivi di Google Workspace che estendono Calendar possono includere alcune azioni del widget specifiche di Calendar. Queste azioni richiedono che la funzione di callback associata restituisca oggetti di risposta specializzati:

Azione tentata La funzione di callback deve restituire
Aggiungere partecipanti CalendarEventActionResponse
Impostare i dati della conferenza CalendarEventActionResponse
Aggiungere allegati CalendarEventActionResponse

Per utilizzare queste azioni del widget e gli oggetti di risposta, devono essere soddisfatte tutte le seguenti condizioni:

  • L'azione viene attivata mentre l'utente ha aperto un evento di Calendar.
  • Il campo manifest addOns.calendar.currentEventAccess del componente aggiuntivo è impostato su WRITE o READ_WRITE.
  • Il componente aggiuntivo include https://www.googleapis.com/auth/calendar.addons.current.event.write l'ambito Calendar.

Inoltre, le modifiche apportate dalla funzione di callback dell'azione non vengono salvate finché l'utente non salva l'evento di Calendar.

Aggiunta di partecipanti con una funzione di callback

Il seguente esempio mostra come creare un pulsante che aggiunge un partecipante specifico a un evento di Calendar in fase di modifica:

  /**
   * Build a simple card with a button that sends a notification.
   * This function is called as part of the eventOpenTrigger that builds
   * a UI when the user opens an event.
   *
   * @param e The event object passed to eventOpenTrigger function.
   * @return {Card}
   */
  function buildSimpleCard(e) {
    var buttonAction = CardService.newAction()
        .setFunctionName('onAddAttendeesButtonClicked');
    var button = CardService.newTextButton()
        .setText('Add new attendee')
        .setOnClickAction(buttonAction);

    // Check the event object to determine if the user can add
    // attendees and disable the button if not.
    if (!e.calendar.capabilities.canAddAttendees) {
      button.setDisabled(true);
    }

    // ...continue creating card sections and widgets, then create a Card
    // object to add them to. Return the built Card object.
  }

  /**
   * Callback function for a button action. Adds attendees to the
   * Calendar event being edited.
   *
   * @param {Object} e The action event object.
   * @return {CalendarEventActionResponse}
   */
  function onAddAttendeesButtonClicked (e) {
    return CardService.newCalendarEventActionResponseBuilder()
        .addAttendees(["aiko@example.com", "malcom@example.com"])
        .build();
  }

Impostazione dei dati della conferenza con una funzione di callback

Questa azione imposta i dati della conferenza sull'evento aperto. Per questi dati della conferenza deve essere specificato l'ID della soluzione di conferenza, perché l'azione non è stata attivata dall'utente che ha selezionato la soluzione desiderata.

Il seguente esempio mostra come creare un pulsante che imposta i dati della conferenza per un evento in fase di modifica:

  /**
   * Build a simple card with a button that sends a notification.
   * This function is called as part of the eventOpenTrigger that builds
   * a UI when the user opens a Calendar event.
   *
   * @param e The event object passed to eventOpenTrigger function.
   * @return {Card}
   */
  function buildSimpleCard(e) {
    var buttonAction = CardService.newAction()
        .setFunctionName('onSaveConferenceOptionsButtonClicked')
        .setParameters(
          {'phone': "1555123467", 'adminEmail': "joyce@example.com"});
    var button = CardService.newTextButton()
        .setText('Add new attendee')
        .setOnClickAction(buttonAction);

    // Check the event object to determine if the user can set
    // conference data and disable the button if not.
    if (!e.calendar.capabilities.canSetConferenceData) {
      button.setDisabled(true);
    }

    // ...continue creating card sections and widgets, then create a Card
    // object to add them to. Return the built Card object.
  }

  /**
   * Callback function for a button action. Sets conference data for the
   * Calendar event being edited.
   *
   * @param {Object} e The action event object.
   * @return {CalendarEventActionResponse}
   */
  function onSaveConferenceOptionsButtonClicked(e) {
    var parameters = e.commonEventObject.parameters;

    // Create an entry point and a conference parameter.
    var phoneEntryPoint = ConferenceDataService.newEntryPoint()
      .setEntryPointType(ConferenceDataService.EntryPointType.PHONE)
      .setUri('tel:' + parameters['phone']);

    var adminEmailParameter = ConferenceDataService.newConferenceParameter()
        .setKey('adminEmail')
        .setValue(parameters['adminEmail']);

    // Create a conference data object to set to this Calendar event.
    var conferenceData = ConferenceDataService.newConferenceDataBuilder()
        .addEntryPoint(phoneEntryPoint)
        .addConferenceParameter(adminEmailParameter)
        .setConferenceSolutionId('myWebScheduledMeeting')
        .build();

    return CardService.newCalendarEventActionResponseBuilder()
        .setConferenceData(conferenceData)
        .build();
  }

Aggiungere allegati con una funzione di callback

Il seguente esempio mostra come creare un pulsante che aggiunge un allegato a un evento di Calendar in fase di modifica:

  /**
   * Build a simple card with a button that creates a new attachment.
   * This function is called as part of the eventAttachmentTrigger that
   * builds a UI when the user goes through the add-attachments flow.
   *
   * @param e The event object passed to eventAttachmentTrigger function.
   * @return {Card}
   */
  function buildSimpleCard(e) {
    var buttonAction = CardService.newAction()
        .setFunctionName('onAddAttachmentButtonClicked');
    var button = CardService.newTextButton()
        .setText('Add a custom attachment')
        .setOnClickAction(buttonAction);

    // Check the event object to determine if the user can add
    // attachments and disable the button if not.
    if (!e.calendar.capabilities.canAddAttachments) {
      button.setDisabled(true);
    }

    // ...continue creating card sections and widgets, then create a Card
    // object to add them to. Return the built Card object.
  }

  /**
   * Callback function for a button action. Adds attachments to the Calendar
   * event being edited.
   *
   * @param {Object} e The action event object.
   * @return {CalendarEventActionResponse}
   */
  function onAddAttachmentButtonClicked(e) {
    return CardService.newCalendarEventActionResponseBuilder()
             .addAttachments([
               CardService.newAttachment()
                 .setResourceUrl("https://example.com/test")
                 .setTitle("Custom attachment")
                 .setMimeType("text/html")
                 .setIconUrl("https://example.com/test.png")
             ])
        .build();
  }

Impostare l'icona dell'allegato

L'icona dell'allegato deve essere ospitata sull'infrastruttura di Google. Per informazioni dettagliate, vedi Fornire icone degli allegati.