Wysyłanie wybranych treści

Poziom kodowania: początkujący
Czas trwania: 20 minut
Typ projektu: automatyzacja z wyzwalaczem opartym na zdarzeniach

Cele

  • Dowiedz się, co robi rozwiązanie.
  • Dowiedz się, jakie funkcje pełnią usługi Apps Script w rozwiązaniu.
  • Skonfiguruj skrypt.
  • Uruchom skrypt.

Informacje o rozwiązaniu

Jeśli masz różne typy treści, które chcesz oferować odbiorcom, możesz pozwolić użytkownikom wybierać, jakie treści mają od Ciebie otrzymywać, za pomocą Formularzy Google. To rozwiązanie umożliwia użytkownikom wybieranie interesujących ich tematów, a następnie automatyczne wysyłanie im e-maili z wybranymi treściami.

Demonstracja wysyłania treści za pomocą Formularzy Google i Gmaila

Jak to działa

Skrypt instaluje wyzwalacz oparty na zdarzeniach, który jest uruchamiany za każdym razem, gdy użytkownik prześle formularz. Po przesłaniu każdego formularza skrypt tworzy i wysyła e-maila z szablonu Dokumentów Google. E-mail zawiera imię i nazwisko użytkownika oraz wybrane przez niego treści. Oferowane treści mogą być dowolnego typu, o ile odwołuje się do nich adres URL.

Usługi Apps Script

To rozwiązanie korzysta z tych usług:

  • Usługa skryptów – instaluje wyzwalacz oparty na zdarzeniach, który uruchamia się za każdym razem, gdy ktoś prześle formularz.
  • Usługa dokumentów – otwiera szablon Dokumentów, którego skrypt używa do tworzenia e-maila.
  • Usługa poczty – tworzy i wysyła e-maila z nazwą użytkownika i wybranymi treściami.
  • Usługa arkuszy kalkulacyjnych – dodaje potwierdzenie do arkusza Odpowiedzi na formularz po wysłaniu e-maila przez skrypt.

Wymagania wstępne

Aby użyć tego przykładu, musisz spełnić te wymagania wstępne:

  • Konto Google (w przypadku kont Google Workspace może być wymagana zgoda administratora).
  • przeglądarkę internetową z dostępem do internetu,

Konfigurowanie skryptu

  1. Kliknij poniższy przycisk, aby utworzyć kopię arkusza kalkulacyjnego Wysyłanie wyselekcjonowanych treści. Projekt Apps Script dla tego rozwiązania jest dołączony do arkusza kalkulacyjnego.
    Utwórz kopię

  2. W skopiowanym arkuszu kalkulacyjnym kliknij Rozszerzenia > Apps Script.

  3. W menu funkcji wybierz installTrigger.

  4. Kliknij Wykonaj.

  5. Gdy pojawi się odpowiedni komunikat, autoryzuj skrypt. Jeśli na ekranie zgody OAuth wyświetla się ostrzeżenie Ta aplikacja nie została zweryfikowana, kliknij kolejno Zaawansowane > Otwórz {Project Name} (niebezpieczne).

Ważne: jeśli uruchomisz funkcję installTrigger więcej niż raz, skrypt utworzy wiele wyzwalaczy, z których każdy będzie wysyłać e-maila, gdy użytkownik prześle formularz. Aby usunąć dodatkowe reguły i uniknąć duplikatów e-maili, kliknij Reguły . Kliknij prawym przyciskiem każdy dodatkowy wyzwalacz i wybierz Usuń wyzwalacz.

Uruchamianie skryptu

  1. Wróć do arkusza kalkulacyjnego i kliknij Narzędzia > Zarządzaj formularzem > Otwórz aktywny formularz.
  2. Wypełnij formularz i kliknij Prześlij.
  3. Sprawdź skrzynkę odbiorczą. Powinien w niej być e-mail z linkami do wybranych przez Ciebie treści.

Sprawdź kod

Aby sprawdzić kod Apps Script tego rozwiązania, kliknij poniżej Wyświetl kod źródłowy:

Pokaż kod źródłowy

Code.gs

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

/*
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.
*/

// To use your own template doc, update the below variable with the URL of your own Google Doc template.
// Make sure you update the sharing settings so that 'anyone'  or 'anyone in your organization' can view.
const EMAIL_TEMPLATE_DOC_URL = 'https://docs.google.com/document/d/1enes74gWsMG3dkK3SFO08apXkr0rcYBd3JHKOb2Nksk/edit?usp=sharing';
// Update this variable to customize the email subject.
const EMAIL_SUBJECT = 'Hello, here is the content you requested';

// Update this variable to the content titles and URLs you want to offer. Make sure you update the form so that the content titles listed here match the content titles you list in the form.
const topicUrls = {
  'Google Calendar how-to videos': 'https://www.youtube.com/playlist?list=PLU8ezI8GYqs7IPb_UdmUNKyUCqjzGO9PJ',
  'Google Drive how-to videos': 'https://www.youtube.com/playlist?list=PLU8ezI8GYqs7Y5d1cgZm2Obq7leVtLkT4',
  'Google Docs how-to videos': 'https://www.youtube.com/playlist?list=PLU8ezI8GYqs4JKwZ-fpBP-zSoWPL8Sit7',
  'Google Sheets how-to videos': 'https://www.youtube.com/playlist?list=PLU8ezI8GYqs61ciKpXf_KkV7ZRbRHVG38',
};

/**
 * Installs a trigger on the spreadsheet for when someone submits a form.
 */
function installTrigger() {
  ScriptApp.newTrigger('onFormSubmit')
      .forSpreadsheet(SpreadsheetApp.getActive())
      .onFormSubmit()
      .create();
}

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

  // If the question title is a label, it can be accessed as an object field.
  // If it has spaces or other characters, it can be accessed as a dictionary.
  let timestamp = responses.Timestamp[0];
  let email = responses['Email address'][0].trim();
  let name = responses.Name[0].trim();
  let topicsString = responses.Topics[0].toLowerCase();

  // Parse topics of interest into a list (since there are multiple items
  // that are saved in the row as blob of text).
  let topics = Object.keys(topicUrls).filter(function(topic) {
    // indexOf searches for the topic in topicsString and returns a non-negative
    // index if the topic is found, or it will return -1 if it's not found.
    return topicsString.indexOf(topic.toLowerCase()) != -1;
  });

  // If there is at least one topic selected, send an email to the recipient.
  let status = '';
  if (topics.length > 0) {
    MailApp.sendEmail({
      to: email,
      subject: EMAIL_SUBJECT,
      htmlBody: createEmailBody(name, topics),
    });
    status = 'Sent';
  }
  else {
    status = 'No topics selected';
  }

  // Append the status on the spreadsheet to the responses' row.
  let sheet = SpreadsheetApp.getActiveSheet();
  let row = sheet.getActiveRange().getRow();
  let column = e.values.length + 1;
  sheet.getRange(row, column).setValue(status);

  console.log("status=" + status + "; responses=" + JSON.stringify(responses));
}

/**
 * Creates email body and includes the links based on topic.
 *
 * @param {string} recipient - The recipient's email address.
 * @param {string[]} topics - List of topics to include in the email body.
 * @return {string} - The email body as an HTML string.
 */
function createEmailBody(name, topics) {
  let topicsHtml = topics.map(function(topic) {
  let url = topicUrls[topic];
    return '<li><a href="' + url + '">' + topic + '</a></li>';
  }).join('');
  topicsHtml = '<ul>' + topicsHtml + '</ul>';

  // Make sure to update the emailTemplateDocId at the top.
  let docId = DocumentApp.openByUrl(EMAIL_TEMPLATE_DOC_URL).getId();
  let emailBody = docToHtml(docId);
  emailBody = emailBody.replace(/{{NAME}}/g, name);
  emailBody = emailBody.replace(/{{TOPICS}}/g, topicsHtml);
  return emailBody;
}

/**
 * Downloads a Google Doc as an HTML string.
 * 
 * @param {string} docId - The ID of a Google Doc to fetch content from.
 * @return {string} The Google Doc rendered as an HTML string.
 */
function docToHtml(docId) {

  // Downloads a Google Doc as an HTML string.
  let url = "https://docs.google.com/feeds/download/documents/export/Export?id=" +
            docId + "&exportFormat=html";
  let param = {
    method: "get",
    headers: {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
    muteHttpExceptions: true,
  };
  return UrlFetchApp.fetch(url, param).getContentText();
}

Współtwórcy

Ten przykład jest obsługiwany przez Google przy pomocy ekspertów Google ds. programowania.

Dalsze kroki