Rispondi al feedback

Livello di programmazione: principiante
Durata: 15 minuti
Tipo di progetto: automazione con un menu personalizzato e un attivatore basato su eventi

Obiettivi

  • Scopri cosa fa la soluzione.
  • Scoprire cosa fanno i servizi Apps Script con questa soluzione.
  • Configura lo script.
  • Esegui lo script.

Informazioni su questa soluzione

Crea automaticamente bozze di risposte email al feedback di Moduli Google. Questa soluzione è incentrata sul feedback sul corso da parte degli studenti, ma puoi applicarla a qualsiasi caso d'uso per il quale ricevi feedback tramite Moduli Google.

Risposte all'invio di moduli inviate da Gmail

Come funziona

Lo script installa un trigger basato su eventi che viene eseguito ogni volta che un utente invia un modulo. A ogni invio di un modulo, lo script crea una bozza di email in Gmail. L'email è indirizzata alla persona che ha inviato il modulo e include le risposte e un messaggio di ringraziamento generico. Puoi modificare l'email prima di inviarla.

Servizi Apps Script

Questa soluzione utilizza i seguenti servizi:

Prerequisiti

Per utilizzare questo esempio, devi disporre dei seguenti prerequisiti:

  • Un Account Google (gli account Google Workspace potrebbero richiedere l'approvazione dell'amministratore).
  • Un browser web con accesso a Internet.

Configura lo script

Fai clic sul pulsante seguente per creare una copia del foglio di lavoro di esempio Rispondi al feedback. Il progetto Apps Script per questa soluzione è allegato al foglio di lavoro.
Crea una copia

Esegui lo script

  1. Fai clic su Strumento di risposta a moduli > Attiva risposte bozza automatiche. Potresti dover aggiornare la pagina per visualizzare questo menu personalizzato.
  2. Quando richiesto, autorizza lo script. Se nella schermata per il consenso OAuth viene visualizzato l'avviso Questa app non è verificata, continua selezionando Avanzate > Vai a {Nome progetto} (non sicuro).

  3. Fai clic su Strumento di risposta tramite modulo > di nuovo Attiva risposte automatiche in bozza.

  4. Fai clic su Strumenti > Gestisci modulo > Vai a modulo pubblicato.

  5. Compila il modulo e fai clic su Invia.

  6. Apri Gmail e controlla le bozze. Dovresti avere una nuova bozza con la risposta del modulo.

Rivedi il codice

Per esaminare il codice di Apps Script per questa soluzione, fai clic su Visualizza codice sorgente qui sotto:

Visualizza codice sorgente

Code.gs

solutions/automations/course-feedback-response/Code.js
// To learn how to use this script, refer to the documentation:
// https://developers.google.com/apps-script/samples/automations/course-feedback-response

/*
Copyright 2022 Google LLC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

/**
 * Creates custom menu for user to run scripts.
 */
function onOpen() {
  let ui = SpreadsheetApp.getUi();
  ui.createMenu('Form Reply Tool')
      .addItem('Enable auto draft replies', 'installTrigger')
      .addToUi();
}

/**
 * Installs a trigger on the Spreadsheet for when a Form response is submitted.
 */
function installTrigger() {
  ScriptApp.newTrigger('onFormSubmit')
      .forSpreadsheet(SpreadsheetApp.getActive())
      .onFormSubmit()
      .create();
}

/**
 * Creates a draft email for every response on a form
 *
 * @param {Object} event - Form submit event
 */
function onFormSubmit(e) {
  let responses = e.namedValues;

  // parse form response data
  let timestamp = responses.Timestamp[0];
  let email = responses['Email address'][0].trim();

  // create email body
  let emailBody = createEmailBody(responses);

  // create draft email
  createDraft(timestamp, email, emailBody);
}

/**
 * Creates email body and includes feedback from Google Form.
 *
 * @param {string} responses - The form response data
 * @return {string} - The email body as an HTML string
 */
function createEmailBody(responses) {
  // parse form response data
  let name = responses.Name[0].trim();
  let industry = responses['What industry do you work in?'][0];
  let source = responses['How did you find out about this course?'][0];
  let rating = responses['On a scale of 1 - 5 how would you rate this course?'][0];
  let productFeedback = responses['What could be different to make it a 5 rating?'][0];
  let otherFeedback = responses['Any other feedback?'][0];

  // create email body
  let htmlBody = 'Hi ' + name + ',<br><br>' +
    'Thanks for responding to our course feedback questionnaire.<br><br>' +
      'It\'s really useful to us to help improve this course.<br><br>' +
        'Have a great day!<br><br>' +
          'Thanks,<br>' +
            'Course Team<br><br>' +
              '****************************************************************<br><br>' +
                '<i>Your feedback:<br><br>' +
                  'What industry do you work in?<br><br>' +
                    industry + '<br><br>' +
                      'How did you find out about this course?<br><br>' +
                        source + '<br><br>' +
                          'On a scale of 1 - 5 how would you rate this course?<br><br>' +
                            rating + '<br><br>' +
                              'What could be different to make it a 5 rating?<br><br>' +
                                productFeedback + '<br><br>' +
                                  'Any other feedback?<br><br>' +
                                    otherFeedback + '<br><br></i>';

  return htmlBody;
}

/**
 * Create a draft email with the feedback
 *
 * @param {string} timestamp Timestamp for the form response
 * @param {string} email Email address from the form response
 * @param {string} emailBody The email body as an HTML string
 */
function createDraft(timestamp, email, emailBody) {
  console.log('draft email create process started');

  // create subject line
  let subjectLine = 'Thanks for your course feedback! ' + timestamp;

  // create draft email
  GmailApp.createDraft(
      email,
      subjectLine,
      '',
      {
        htmlBody: emailBody,
      }
  );
}

Collaboratori

Questo campione è stato creato da Ben Collins, docente presso benlcollins.com e Google Developer Expert.

Questo campione è gestito da Google con l'aiuto degli Esperti Google Developers.

Passaggi successivi