Pola End-to-End#
Gabungkan beberapa layanan dalam satu alur: baca data → panggil API → simpan → kirim notifikasi.
Contoh: Pipeline Pemrosesan Order#
Alur: Sheets (data order) → API eksternal (cek status) → Drive (buat invoice PDF) → Gmail (kirim ke pelanggan).
Example:
function prosesOrder() { const sheet = SpreadsheetApp.getActiveSheet(); const data = sheet.getDataRange().getValues().slice(1);
data.forEach((r, i) => { const [id, email, status] = r; if (status !== 'Baru') return;
// 1. Panggil API eksternal const api = UrlFetchApp.fetch('https://api.contoh.com/order/' + id); const hasil = JSON.parse(api.getContentText());
// 2. Buat invoice di Drive const folder = DriveApp.getFolderById('ID_FOLDER'); const file = folder.createFile('Invoice-' + id + '.txt', 'Total: ' + hasil.total);
// 3. Kirim email GmailApp.sendEmail(email, 'Invoice ' + id, 'Total: ' + hasil.total, { attachments: [file.getAs(MimeType.PDF)] });
// 4. Tandai selesai di Sheets sheet.getRange(i + 2, 3).setValue('Diproses'); });}Memecah Menjadi Fungsi Kecil#
Example:
function prosesOrder() { const orders = bacaBelumDiproses(); orders.forEach((o) => { const detail = cekApi(o.id); const file = buatInvoice(o, detail); kirimEmail(o.email, file); tandaiSelesai(o.baris); });}
function bacaBelumDiproses() { /* ... */ }function cekApi(id) { /* ... */ }function buatInvoice(o, d) { /* ... */ }function kirimEmail(e, f) { /* ... */ }function tandaiSelesai(baris) { /* ... */ }Menjadwalkan Alur#
Example:
function pasangPipeline() { ScriptApp.newTrigger('prosesOrder') .timeBased().everyMinutes(30).create();}| Langkah | Layanan |
|---|---|
| Baca data | SpreadsheetApp |
| Panggil API | UrlFetchApp |
| Simpan file | DriveApp |
| Kirim notifikasi | GmailApp |
| Jadwal | ScriptApp |
Note
Satu fungsi sebaiknya tidak melakukan terlalu banyak hal. Pecah jadi fungsi kecil agar mudah diuji dan di-debug.
Next#
Lanjut ke Level 5: Error Handling & Debugging.