Class google.script.host (API sisi klien)

google.script.host adalah JavaScript API sisi klien asinkron yang dapat berinteraksi dengan dialog atau sidebar di Google Dokumen, Spreadsheet, atau Formulir yang berisi halaman layanan HTML. Untuk menjalankan fungsi sisi server dari kode sisi klien, gunakan google.script.run. Untuk mengetahui informasi lebih lanjut, lihat panduan berkomunikasi dengan fungsi server di layanan HTML.

Properti

PropertiDeskripsi
originMenyediakan domain host, sehingga skrip dapat menetapkan asalnya dengan benar.

Metode

MetodeJenis hasil yang ditampilkanDeskripsi singkat
close() void Menutup dialog atau sidebar saat ini.
editor.focus() void Mengalihkan fokus browser dari dialog atau sidebar ke editor Google Dokumen, Spreadsheet, atau Formulir.
setHeight(height) void Menyetel tinggi dialog saat ini.
setWidth(width) void Menyetel lebar dialog saat ini.

Dokumentasi mendetail

close()

Menutup dialog atau sidebar saat ini.

Code.gs

function onOpen(e) {
  SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
      .createMenu('Sidebar').addItem('Show', 'showSidebar').addToUi();
}

function showSidebar() {
  var html = HtmlService.createHtmlOutputFromFile('Index');
  SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
      .showSidebar(html);
}

Index.html

<input type="button" value="Close"
  onclick="google.script.host.close()" />

editor.focus()

Mengalihkan fokus browser dari dialog atau sidebar ke editor Google Dokumen, Spreadsheet, atau Formulir.

Code.gs

function onOpen(e) {
  SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
      .createMenu('Sidebar').addItem('Show', 'showSidebar').addToUi();
}

function showSidebar() {
  var html = HtmlService.createHtmlOutputFromFile('Index');
  SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
      .showSidebar(html);
}

Index.html

<input type="button" value="Switch focus"
  onclick="google.script.host.editor.focus()" />

setHeight(height)

Menyetel tinggi dialog saat ini.

Code.gs

function onOpen(e) {
  SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
      .createMenu('Dialog').addItem('Show', 'showDialog').addToUi();
}

function showDialog() {
  var html = HtmlService.createHtmlOutputFromFile('Index')
      .setWidth(300)
      .setHeight(200);
  SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
      .showModalDialog(html, 'Dialog title');
}

Index.html

<script>
  function resizeDialog(width, height) {
    google.script.host.setWidth(width);
    google.script.host.setHeight(height);
  }
</script>
<input type="button" value="Resize dialog"
  onclick="resizeDialog(450, 300)" />

Parameter

NamaJenisDeskripsi
heightIntegertinggi baru, dalam piksel

setWidth(width)

Menyetel lebar dialog saat ini.

Code.gs

function onOpen(e) {
  SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
      .createMenu('Dialog').addItem('Show', 'showDialog').addToUi();
}

function showDialog() {
  var html = HtmlService.createHtmlOutputFromFile('Index')
      .setWidth(300)
      .setHeight(200);
  SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
      .showModalDialog(html, 'Dialog title');
}

Index.html

<script>
  function resizeDialog(width, height) {
    google.script.host.setWidth(width);
    google.script.host.setHeight(height);
  }
</script>
<input type="button" value="Resize dialog"
  onclick="resizeDialog(450, 300)" />

Parameter

NamaJenisDeskripsi
widthIntegerlebar baru, dalam piksel