Lớp google.script.host (API phía máy khách)

google.script.host là một API JavaScript phía máy khách không đồng bộ có thể tương tác với hộp thoại hoặc thanh bên trong Google Tài liệu, Trang tính hoặc Biểu mẫu có chứa các trang dịch vụ HTML. Để thực thi các hàm phía máy chủ từ mã phía máy khách, hãy sử dụng google.script.run. Để biết thêm thông tin, hãy xem hướng dẫn giao tiếp với các hàm máy chủ trong dịch vụ HTML.

Thuộc tính

Tài sảnNội dung mô tả
originCung cấp miền lưu trữ để các tập lệnh có thể đặt nguồn gốc chính xác.

Phương thức

Phương thứcKiểu dữ liệu trả vềMô tả ngắn
close() void Đóng hộp thoại hoặc thanh bên hiện tại.
editor.focus() void Chuyển tiêu điểm của trình duyệt từ hộp thoại hoặc thanh bên sang trình chỉnh sửa Google Tài liệu, Trang tính hoặc Biểu mẫu.
setHeight(height) void Đặt chiều cao của hộp thoại hiện tại.
setWidth(width) void Đặt chiều rộng của hộp thoại hiện tại.

Tài liệu chi tiết

close()

Đóng hộp thoại hoặc thanh bên hiện tại.

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

Chuyển tiêu điểm của trình duyệt từ hộp thoại hoặc thanh bên sang trình chỉnh sửa Google Tài liệu, Trang tính hoặc Biểu mẫu.

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)

Đặt chiều cao của hộp thoại hiện tại.

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

Các tham số

TênLoạiNội dung mô tả
heightIntegerchiều cao mới, tính bằng pixel

setWidth(width)

Đặt chiều rộng của hộp thoại hiện tại.

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

Các tham số

TênLoạiNội dung mô tả
widthIntegerchiều rộng mới, tính bằng pixel