Klasa google.script.host (interfejs API po stronie klienta)

google.script.host to asynchroniczny interfejs API JavaScript działający po stronie klienta, który może wchodzić w interakcje z oknami lub paskami bocznymi w Dokumentach, Arkuszach i Formularzach Google, które zawierają strony usługi HTML. Aby uruchamiać funkcje po stronie serwera z kodu po stronie klienta, użyj google.script.run. Więcej informacji znajdziesz w przewodniku po komunikacji z funkcjami serwera w usłudze HTML.

Właściwości

WłaściwośćOpis
originZawiera domenę hosta, dzięki czemu skrypty mogą poprawnie ustawiać ich źródło.

Metody

MetodaZwracany typKrótki opis
close() void Zamyka bieżące okno lub pasek boczny.
editor.focus() void Przełącza fokus przeglądarki z okna lub paska bocznego do edytora Dokumentów, Arkuszy lub Formularzy Google.
setHeight(height) void Ustawia wysokość bieżącego okna.
setWidth(width) void Ustawia szerokość bieżącego okna.

Szczegółowa dokumentacja

close()

Zamyka bieżące okno lub pasek boczny.

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()

Przełącza fokus przeglądarki z okna lub paska bocznego do edytora Dokumentów, Arkuszy lub Formularzy Google.

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)

Ustawia wysokość bieżącego okna.

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)" />

Parametry

NazwaTypOpis
heightIntegernowa wysokość w pikselach.

setWidth(width)

Ustawia szerokość bieżącego okna.

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)" />

Parametry

NazwaTypOpis
widthIntegernową szerokość w pikselach.