Panduan memulai otomatis

Buat dan jalankan otomatisasi sederhana yang membuat dokumen Google Dokumen dan mengirimkan link ke dokumen tersebut melalui email.

Tujuan

  • Siapkan skrip.
  • Jalankan skrip.

Prasyarat

Untuk menggunakan contoh 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 langkah-langkah berikut:

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

    template/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 Simpan ikon.

  5. Klik Project tanpa judul.

  6. Masukkan nama untuk skrip Anda dan klik Ganti nama.

Menjalankan skrip

Untuk menjalankan skrip tersebut, lakukan langkah-langkah berikut:

  1. Klik Jalankan.
  2. Saat diminta, izinkan skrip. Jika layar izin OAuth menampilkan peringatan, This app is not Verified, lanjutkan dengan memilih Advanced > Go to {Project Name} (unsafe).

  3. Setelah eksekusi skrip selesai, periksa kotak masuk Gmail Anda untuk melihat email.

  4. Buka email dan klik link untuk membuka dokumen yang telah dibuat.

Langkah berikutnya