Azioni nel calendario

Gli oggetti Action consentono di creare comportamenti interattivi nei componenti aggiuntivi di Google Workspace. Definiscono cosa accade quando un utente interagisce con un widget (ad esempio un pulsante) nell'interfaccia utente del componente aggiuntivo.

Un'azione viene collegata a un determinato widget utilizzando una funzione di gestore 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 trasmesso un oggetto evento che trasporta informazioni sulle interazioni dell'utente sul lato client. Devi implementare la funzione di callback e fare in modo che restituisca un oggetto di risposta specifico.

Ad esempio, supponi di voler creare e visualizzare una nuova scheda quando viene fatto clic su un pulsante. A questo scopo, devi creare un nuovo widget per pulsanti e utilizzare la funzione di gestione del widget per pulsanti setOnClickAction(action) per impostare una creazione di schede Action. Il Action che definisci specifica una funzione di callback di Apps Script che viene eseguita quando l'utente fa clic sul pulsante. In questo caso, implementi la funzione callback per creare la scheda desiderata e restituire un oggetto ActionResponse. L'oggetto response indica al componente aggiuntivo di visualizzare la scheda creata dalla funzione di callback.

In questa pagina vengono descritte le azioni dei widget specifiche di Calendar che puoi includere nel componente aggiuntivo.

Interazioni con Calendar

I componenti aggiuntivi di Google Workspace che estendono Calendar possono includere alcuni widget aggiuntivi specifici. Queste azioni richiedono la funzione di callback dell'azione associata per restituire oggetti di risposta specializzati:

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

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

  • L'azione viene attivata quando l'utente ha un evento di Calendar aperto.
  • 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 di Calendar.

Inoltre, eventuali 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

L'esempio seguente mostra come creare un pulsante per aggiungere 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();
  }

Impostare i dati della conferenza con una funzione di callback

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

L'esempio seguente mostra come creare un pulsante che imposti i dati della conferenza per un evento da modificare:

  /**
   * 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();
  }

Aggiungi allegati con una funzione di callback

L'esempio seguente mostra come creare un pulsante che aggiunga 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();
  }

Impostazione dell'icona per gli allegati

L'icona dell'allegato deve essere ospitata sull'infrastruttura di Google. Per maggiori dettagli, consulta Fornire icone allegate.