คลาส google.script.host (API ฝั่งไคลเอ็นต์)
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
พร็อพเพอร์ตี้
พร็อพเพอร์ตี้ | คำอธิบาย |
origin | ระบุโดเมนโฮสต์ เพื่อให้สคริปต์ตั้งค่า
เริ่มต้นอย่างถูกต้อง |
เมธอด
วิธีการ | ประเภทการแสดงผล | รายละเอียดแบบย่อ |
close() |
void |
ปิดกล่องโต้ตอบหรือแถบด้านข้างปัจจุบัน |
editor.focus() |
void |
เปลี่ยนโฟกัสของเบราว์เซอร์จากกล่องโต้ตอบหรือแถบด้านข้างเป็นเครื่องมือแก้ไขของ Google เอกสาร ชีต หรือฟอร์ม |
setHeight(height) |
void |
ตั้งค่าความสูงของกล่องโต้ตอบปัจจุบัน |
setWidth(width) |
void |
ตั้งค่าความกว้างของกล่องโต้ตอบปัจจุบัน |
เอกสารโดยละเอียด
close()
ปิดกล่องโต้ตอบหรือแถบด้านข้างปัจจุบัน
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()
เปลี่ยนโฟกัสของเบราว์เซอร์จากกล่องโต้ตอบหรือแถบด้านข้างเป็นเครื่องมือแก้ไขของ 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)
ตั้งค่าความสูงของกล่องโต้ตอบปัจจุบัน
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)" />
พารามิเตอร์
ชื่อ | ประเภท | คำอธิบาย |
height | Integer | ความสูงใหม่เป็นพิกเซล |
setWidth(width)
ตั้งค่าความกว้างของกล่องโต้ตอบปัจจุบัน
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)" />
พารามิเตอร์
ชื่อ | ประเภท | คำอธิบาย |
width | Integer | ความกว้างใหม่เป็นพิกเซล |
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-26 UTC
[null,null,["อัปเดตล่าสุด 2025-07-26 UTC"],[[["\u003cp\u003e\u003ccode\u003egoogle.script.host\u003c/code\u003e is an asynchronous client-side JavaScript API used to interact with dialogs or sidebars in Google Docs, Sheets, or Forms containing HTML service pages.\u003c/p\u003e\n"],["\u003cp\u003eTo execute server-side functions from client-side code within these dialogs or sidebars, utilize \u003ccode\u003egoogle.script.run\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eThe API provides methods like \u003ccode\u003eclose()\u003c/code\u003e, \u003ccode\u003eeditor.focus()\u003c/code\u003e, \u003ccode\u003esetHeight(height)\u003c/code\u003e, and \u003ccode\u003esetWidth(width)\u003c/code\u003e for controlling dialog and sidebar behavior.\u003c/p\u003e\n"],["\u003cp\u003eWhile \u003ccode\u003egoogle.script.host\u003c/code\u003e focuses on client-side interactions within HTML service pages, server-side communication is handled through \u003ccode\u003egoogle.script.run\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003egoogle.script.host\u003c/code\u003e enables developers to create dynamic and interactive user interfaces within Google Workspace applications by seamlessly integrating client-side JavaScript with server-side Apps Script functionality.\u003c/p\u003e\n"]]],[],null,["# Class google.script.host (Client-side API)\n\n`google.script.host` is an asynchronous client-side JavaScript API that can interact\nwith dialogs or sidebars in Google Docs, Sheets, or Forms that contain\n[HTML-service pages](/apps-script/guides/html). To execute server-side functions from\nclient-side code, use [`google.script.run`](/apps-script/guides/html/reference/run). For more information, see\nthe\n[guide to communicating with server functions](/apps-script/guides/html/communication)\nin HTML service. \n\n### Properties\n\n| Property | Description |\n|----------|----------------------------------------------------------------------|\n| `origin` | Provides the host domain, so scripts can set their origin correctly. |\n\n### Methods\n\n| Method | Return type | Brief description |\n|------------------------------------------|-------------|------------------------------------------------------------------------------------------------|\n| [close()](#close()) | `void` | Closes the current dialog or sidebar. |\n| [editor.focus()](#editor.focus()) | `void` | Switches browser focus from the dialog or sidebar to the Google Docs, Sheets, or Forms editor. |\n| [setHeight(height)](#setHeight(Integer)) | `void` | Sets the height of the current dialog. |\n| [setWidth(width)](#setWidth(Integer)) | `void` | Sets the width of the current dialog. |\n\nDetailed documentation\n----------------------\n\n### `close()`\n\nCloses the current dialog or sidebar. \n\n### Code.gs\n\n```html\nfunction onOpen(e) {\n SpreadsheetApp.getUi() // Or DocumentApp or FormApp.\n .createMenu('Sidebar').addItem('Show', 'showSidebar').addToUi();\n}\n\nfunction showSidebar() {\n var html = HtmlService.createHtmlOutputFromFile('Index');\n SpreadsheetApp.getUi() // Or DocumentApp or FormApp.\n .showSidebar(html);\n}\n```\n\n### Index.html\n\n```html\n\u003cinput type=\"button\" value=\"Close\"\n onclick=\"google.script.host.close()\" /\u003e\n```\n\n*** ** * ** ***\n\n### `editor.focus()`\n\nSwitches browser focus from the dialog or sidebar to the Google Docs, Sheets, or Forms editor. \n\n### Code.gs\n\n```html\nfunction onOpen(e) {\n SpreadsheetApp.getUi() // Or DocumentApp or FormApp.\n .createMenu('Sidebar').addItem('Show', 'showSidebar').addToUi();\n}\n\nfunction showSidebar() {\n var html = HtmlService.createHtmlOutputFromFile('Index');\n SpreadsheetApp.getUi() // Or DocumentApp or FormApp.\n .showSidebar(html);\n}\n```\n\n### Index.html\n\n```html\n\u003cinput type=\"button\" value=\"Switch focus\"\n onclick=\"google.script.host.editor.focus()\" /\u003e\n```\n\n*** ** * ** ***\n\n### `setHeight(height)`\n\nSets the height of the current dialog. \n\n### Code.gs\n\n```html\nfunction onOpen(e) {\n SpreadsheetApp.getUi() // Or DocumentApp or FormApp.\n .createMenu('Dialog').addItem('Show', 'showDialog').addToUi();\n}\n\nfunction showDialog() {\n var html = HtmlService.createHtmlOutputFromFile('Index')\n .setWidth(300)\n .setHeight(200);\n SpreadsheetApp.getUi() // Or DocumentApp or FormApp.\n .showModalDialog(html, 'Dialog title');\n}\n```\n\n### Index.html\n\n```html\n\u003cscript\u003e\n function resizeDialog(width, height) {\n google.script.host.setWidth(width);\n google.script.host.setHeight(height);\n }\n\u003c/script\u003e\n\u003cinput type=\"button\" value=\"Resize dialog\"\n onclick=\"resizeDialog(450, 300)\" /\u003e\n```\n\n#### Parameters\n\n| Name | Type | Description |\n|----------|-----------|---------------------------|\n| `height` | `Integer` | the new height, in pixels |\n\n*** ** * ** ***\n\n### `setWidth(width)`\n\nSets the width of the current dialog. \n\n### Code.gs\n\n```html\nfunction onOpen(e) {\n SpreadsheetApp.getUi() // Or DocumentApp or FormApp.\n .createMenu('Dialog').addItem('Show', 'showDialog').addToUi();\n}\n\nfunction showDialog() {\n var html = HtmlService.createHtmlOutputFromFile('Index')\n .setWidth(300)\n .setHeight(200);\n SpreadsheetApp.getUi() // Or DocumentApp or FormApp.\n .showModalDialog(html, 'Dialog title');\n}\n```\n\n### Index.html\n\n```html\n\u003cscript\u003e\n function resizeDialog(width, height) {\n google.script.host.setWidth(width);\n google.script.host.setHeight(height);\n }\n\u003c/script\u003e\n\u003cinput type=\"button\" value=\"Resize dialog\"\n onclick=\"resizeDialog(450, 300)\" /\u003e\n```\n\n#### Parameters\n\n| Name | Type | Description |\n|---------|-----------|--------------------------|\n| `width` | `Integer` | the new width, in pixels |"]]