Krótkie wprowadzenie do automatyzacji

Zbuduj i uruchom prostą automatyzację, która utworzy dokument w Dokumentach Google i wyśle Ci e-maila z linkiem do niego.

Cele

  • Skonfiguruj skrypt.
  • Uruchom skrypt.

Wymagania wstępne

Aby korzystać z tego przykładu, musisz spełnić te wymagania wstępne:

  • konta Google (konta Google Workspace mogą wymagać zatwierdzenia przez administratora),
  • Przeglądarka z dostępem do internetu.

Konfigurowanie skryptu

Aby utworzyć automatyzację:

  1. Aby otworzyć edytor Apps Script, otwórz script.google.com. Jeśli odwiedzasz script.google.com po raz pierwszy, kliknij Wyświetl panel.
  2. Kliknij Nowy projekt.
  3. Usuń cały kod z edytora skryptów i wklej go poniżej.

    templates/standalone/helloWorld.gs
    /**
     * Creates a Google Doc and sends an email to the current user with a link to the doc.
     */
    function createAndSendDocument() {
      try {
        // Create a new Google Doc named 'Hello, world!'
        const doc = DocumentApp.create('Hello, world!');
    
        // Access the body of the document, then add a paragraph.
        doc.getBody().appendParagraph('This document was created by Google Apps Script.');
    
        // Get the URL of the document.
        const url = doc.getUrl();
    
        // Get the email address of the active user - that's you.
        const email = Session.getActiveUser().getEmail();
    
        // Get the name of the document to use as an email subject line.
        const subject = doc.getName();
    
        // Append a new string to the "url" variable to use as an email body.
        const body = 'Link to your doc: ' + url;
    
        // Send yourself an email with a link to the document.
        GmailApp.sendEmail(email, subject, body);
      } catch (err) {
        // TODO (developer) - Handle exception
        console.log('Failed with error %s', err.message);
      }
    }
  4. Kliknij Zapisz Ikona zapisania.

  5. Kliknij Projekt bez tytułu.

  6. Wpisz nazwę skryptu i kliknij Zmień nazwę.

Uruchom skrypt

Aby uruchomić skrypt, wykonaj te czynności:

  1. Kliknij Uruchom.
  2. Gdy pojawi się odpowiedni komunikat, autoryzuj skrypt. Jeśli na ekranie zgody OAuth pojawi się ostrzeżenie Ta aplikacja nie jest zweryfikowana, wybierz Zaawansowane > Otwórz {Project Name} (niebezpieczny).

  3. Po zakończeniu wykonywania skryptu poszukaj e-maila w skrzynce odbiorczej Gmaila.

  4. Otwórz tego e-maila i kliknij link, aby otworzyć utworzony dokument.

Dalsze kroki