Panduan memulai otomatisasi

Buat dan jalankan otomatisasi yang membuat dokumen Google Dokumen dan mengirimkan email berisi link ke dokumen tersebut kepada Anda.

Tujuan

  • Siapkan skrip.
  • Jalankan skrip.

Prasyarat

Untuk menggunakan sampel ini, Anda memerlukan prasyarat berikut:

  • Akun Google (akun Google Workspace mungkin memerlukan persetujuan administrator).
  • Browser web dengan akses ke internet.

Menyiapkan skrip

Untuk membuat otomatisasi, lakukan hal berikut:

  1. Untuk membuka editor Google Apps Script, buka script.google.com. Jika ini adalah pertama kalinya Anda membuka script.google.com, klik Lihat Dasbor.
  2. Klik Project baru.
  3. Hapus semua kode yang ada di editor skrip dan tempelkan kode berikut.

    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. Klik Simpan Ikon yang merepresentasikan tindakan penyimpanan.

  5. Klik Project tanpa judul.

  6. Masukkan nama untuk skrip Anda, lalu klik Ganti nama.

Jalankan skrip:

Untuk menjalankan skrip, lakukan hal berikut:

  1. Klik Run.
  2. Saat diminta, izinkan skrip. <<../samples/_snippets/oauth.md>>
  3. Setelah eksekusi skrip selesai, periksa email di kotak masuk Gmail Anda.
  4. Buka email dan klik link untuk membuka dokumen yang Anda buat.

Langkah berikutnya