คลาส google.script.host (API ฝั่งไคลเอ็นต์)

google.script.host เป็น JavaScript API ฝั่งไคลเอ็นต์แบบไม่พร้อมกันที่สามารถโต้ตอบกับกล่องโต้ตอบหรือแถบด้านข้างใน Google เอกสาร ชีต หรือฟอร์มที่มีหน้าบริการ HTML หากต้องการเรียกใช้ฟังก์ชันฝั่งเซิร์ฟเวอร์จากโค้ดฝั่งไคลเอ็นต์ ให้ใช้ google.script.run ดูข้อมูลเพิ่มเติมได้ในคำแนะนำในการสื่อสารกับฟังก์ชันของเซิร์ฟเวอร์ในบริการ HTML

พร็อพเพอร์ตี้

พร็อพเพอร์ตี้คำอธิบาย
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)" />

พารามิเตอร์

ชื่อTypeคำอธิบาย
heightIntegerความสูงใหม่เป็นพิกเซล

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

พารามิเตอร์

ชื่อTypeคำอธิบาย
widthIntegerความกว้างใหม่เป็นพิกเซล