Azioni nel calendario

Gli oggetti Action consentono di creare un comportamento interattivo 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 associata a un determinato widget utilizzando una funzione gestore 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 lato client dell'utente. Devi implementare la funzione di callback e fare in modo che restituisca un oggetto di risposta specifico.

Ad esempio, supponiamo che tu voglia un pulsante che crei e visualizzi una nuova scheda quando viene fatto clic. Per farlo, devi creare un nuovo widget dei pulsanti e utilizzare la funzione di gestione del widget setOnClickAction(action) per impostare una creazione di schede Action. Il valore Action che definisci specifica una funzione di callback di Apps Script che viene eseguita quando viene fatto clic sul pulsante. In questo caso, implementerai la funzione di callback per creare la scheda che ti interessa e restituire un oggetto ActionResponse. L'oggetto risposta 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 il calendario

I componenti aggiuntivi di Google Workspace che estendono Calendar possono includere alcune azioni aggiuntive dei widget specifiche di Calendar. Queste azioni richiedono la funzione di callback dell'azione associata per restituire 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 widget e questi oggetti risposta, tutte le seguenti condizioni devono essere vere:

  • L'azione viene attivata quando l'utente ha un evento di Calendar aperto.
  • Il campo del file 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 che aggiunga 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 delle conferenze con una funzione di callback

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

L'esempio seguente mostra come creare un pulsante che imposti i dati delle conferenze 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

L'esempio seguente 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();
  }

Impostazione dell'icona degli allegati

L'icona dell'allegato deve essere ospitata sull'infrastruttura di Google. Consulta Fornire icone degli allegati per i dettagli.