Selasa, 18 November 2025

Google Apps Script Series 1

 


Penjelasan Setiap Library / Fungsi


A. Web App Entry Point

1. doGet(e)

Digunakan ketika browser mengakses URL API dengan metode GET.

Contoh:

function doGet(e) {
  return ContentService.createTextOutput("Hello World");
}

2. doPost(e)

Digunakan untuk menerima data dari form, JavaScript Fetch API, atau aplikasi lain.

Contoh:

function doPost(e) {
  const data = JSON.parse(e.postData.contents);
  // proses data...
}

B. ContentService → output JSON

Dipakai untuk membuat API yang mengirim data ke website.

Contoh:

return ContentService
  .createTextOutput(JSON.stringify(response))
  .setMimeType(ContentService.MimeType.JSON);

C. SpreadsheetApp → Mengelola Google Sheet

Ini adalah library paling penting untuk CRUD data.

Fungsi yang paling sering dipakai:

Fungsi

Keterangan

getActiveSpreadsheet()

Mendapatkan file sheet

getSheetByName()

Akses sheet tertentu

getRange()

Akses sel / range

appendRow()

Tambah data

getDataRange()

Ambil semua data

getValues()

Ambil nilai range

setValue()

Isi data ke sel

deleteRow()

Hapus baris

clear()

Kosongkan sheet

Contoh CRUD:

Insert Data

ss.appendRow([data.nama, data.email]);

Read Data

const rows = ss.getDataRange().getValues();

D. Utilities (Tambahan Penting)

Dipakai untuk:

·       Format tanggal

·       UUID / kode unik

·       Base64 encode/decode

·       JSON validation

Contoh:

Utilities.formatDate(new Date(), "GMT+7", "yyyy-MM-dd HH:mm:ss");

E. HtmlService (Jika membuat UI dalam GAS)

Jarang dipakai kalau UI dibuat di HTML terpisah, tapi penting untuk web app dengan UI.

function doGet() {
  return HtmlService.createHtmlOutputFromFile("index");
}


Referensi

Tidak ada komentar:

Posting Komentar

Website - Landing Page

  https://www.youtube.com/watch?v=rscFdPzVJhw&list=PLz_5rPRIvGEBDvyf-HIIDHjsPppnuXtFG Website Preview: https://webdesignmastery.github....