Panduan memulai otomatis

Bangun dan jalankan otomatisasi sederhana yang membuat dokumen Google Dokumen dan mengirimkan link ke dokumen kepada Anda melalui email.

Tujuan

  • Menyiapkan 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 ke script.google.com, klik Lihat Dasbor.
  2. Klik Project baru.
  3. Hapus semua kode di editor skrip dan tempelkan kode di bawah ini.

    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 simpan.

  5. Klik Project tanpa judul.

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

Menjalankan skrip

Untuk menjalankan skrip, lakukan langkah-langkah berikut:

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

  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