Web App & UI Custom (Sidebar/Dialog)
SeriLevel 4 — Integrasi & Trigger2 / 4

Web App & UI Custom (Sidebar/Dialog)

Buat antarmuka HTML (sidebar, dialog, atau Web App) yang berkomunikasi dengan Apps Script.

HTML Service#

Gunakan HtmlService untuk menampilkan HTML di dalam Google Sheets atau sebagai Web App mandiri.

Example (Code.gs):

function bukaSidebar() {
const html = HtmlService.createHtmlOutputFromFile('Sidebar')
.setTitle('Panel Cepat');
SpreadsheetApp.getUi().showSidebar(html);
}

Example (Sidebar.html):

<!DOCTYPE html>
<input id="nama" placeholder="Nama">
<button onclick="kirim()">Kirim</button>
<script>
function kirim() {
const nama = document.getElementById('nama').value;
google.script.run.prosesNama(nama);
}
</script>

Example (panggil balik ke server):

function prosesNama(nama) {
SpreadsheetApp.getActiveSheet().getRange('A1').setValue(nama);
}

Dialog#

Example:

function bukaDialog() {
const html = HtmlService.createHtmlOutputFromFile('Dialog').setWidth(400);
SpreadsheetApp.getUi().showModalDialog(html, 'Konfirmasi');
}

Web App#

Deploy sebagai URL publik. Fungsi doGet menangani permintaan HTTP.

Example:

function doGet(e) {
const html = HtmlService.createHtmlOutputFromFile('Index');
return html;
}
function doPost(e) {
const data = JSON.parse(e.postData.contents);
return ContentService.createTextOutput('OK');
}

Mengembalikan JSON (API)#

Example:

function doGet(e) {
const hasil = { total: 120, status: 'ok' };
return ContentService
.createTextOutput(JSON.stringify(hasil))
.setMimeType(ContentService.MimeType.JSON);
}
MethodDeskripsi
createHtmlOutputFromFile()Muat file HTML
google.script.runPanggil fungsi server dari JS
showSidebar() / showModalDialog()Tampilkan UI

Note

Web App butuh deploy ulang (Deploy → New deployment) setiap mengubah doGet/doPost agar perubahan aktif.

Next#

Pelajari Integrasi API Eksternal.

Lisensi

CC BY-NC-SA 4.0 Karya ini dilisensikan di bawah Lisensi Creative Commons Atribusi-NonKomersial-BerbagiSerupa 4.0 Internasional.

Butuh jasa automatisasi atau custom Apps Script?

Jangan ragu untuk berkonsultasi. Kami bantu bangun solusi sesuai kebutuhan Anda.

Konsultasi sekarang →