เข้าถึงและสร้างไฟล์ Google ชีต คลาสนี้เป็นคลาสหลักสำหรับบริการสเปรดชีต
พร็อพเพอร์ตี้
| พร็อพเพอร์ตี้ | ประเภท | คำอธิบาย |
|---|---|---|
Auto | Auto | การแจงนับประเภทของชุดข้อมูลที่ใช้ในการคำนวณค่าที่ป้อนอัตโนมัติ |
Banding | Banding | การแจงนับธีมการแบนด์ที่เป็นไปได้ |
Boolean | Boolean | การแจงนับเกณฑ์บูลีนของการจัดรูปแบบตามเงื่อนไข |
Border | Border | การแจงนับรูปแบบที่ใช้ได้สำหรับการตั้งค่าเส้นขอบใน Range |
Color | Color | การแจงนับประเภทสีที่เป็นไปได้ |
Copy | Copy | การแจงนับประเภทการวางที่เป็นไปได้ |
Data | Data | การแจงนับรหัสข้อผิดพลาดในการเรียกใช้ข้อมูลที่อาจเกิดขึ้น |
Data | Data | การแจงนับสถานะการเรียกใช้ข้อมูลที่เป็นไปได้ |
Data | Data | การแจงนับประเภทพารามิเตอร์ของแหล่งข้อมูลที่เป็นไปได้ |
Data | Data | การแจงนับขอบเขตการรีเฟรชแหล่งข้อมูลที่เป็นไปได้ |
Data | Data | การแจงนับประเภทแหล่งข้อมูลที่เป็นไปได้ |
Data | Data | การแจงนับที่แสดงเกณฑ์การตรวจสอบข้อมูลที่ตั้งค่าได้ในช่วง |
Date | Date | การแจงนับกฎการจัดกลุ่มวันที่และเวลา |
Developer | Developer | การแจงนับประเภทตำแหน่งข้อมูลเมตาของนักพัฒนาซอฟต์แวร์ที่เป็นไปได้ |
Developer | Developer | การแจงนับการเปิดเผยข้อมูลเมตาของนักพัฒนาซอฟต์แวร์ที่เป็นไปได้ |
Dimension | Dimension | การแจงนับมิติข้อมูลที่เป็นไปได้ของสเปรดชีต |
Direction | Direction | การแจงนับทิศทางที่เป็นไปได้ที่ผู้ใช้สามารถเลื่อนภายในสเปรดชีตได้โดยใช้ปุ่มลูกศร |
Frequency | Frequency | การแจงนับประเภทความถี่ที่เป็นไปได้ |
Group | Group | การแจงนับตำแหน่งที่สลับการควบคุมกลุ่มอยู่ได้ |
Interpolation | Interpolation | การแจงนับประเภทการประมาณค่าการไล่ระดับสีของการจัดรูปแบบตามเงื่อนไข |
Pivot | Pivot | การแจงนับฟังก์ชันที่อาจใช้เพื่อสรุปค่าในตาราง Pivot |
Pivot | Pivot | การแจงนับวิธีที่อาจแสดงค่า Pivot |
Protection | Protection | การแจงนับที่แสดงส่วนต่างๆ ของสเปรดชีตที่ป้องกันไม่ให้แก้ไขได้ |
Recalculation | Recalculation | การแจงนับช่วงเวลาที่เป็นไปได้ซึ่งใช้ในการคำนวณสเปรดชีตใหม่ได้ |
Relative | Relative | การแจงนับตัวเลือกวันที่แบบสัมพัทธ์สําหรับการคํานวณค่าที่จะใช้ใน Boolean ที่อิงตามวันที่ |
Sheet | Sheet | การแจงนับชีตประเภทต่างๆ ที่อาจมีอยู่ในสเปรดชีต |
Sort | Sort | การแจงนับลำดับการจัดเรียง |
Text | Text | การแจงนับทิศทางข้อความที่ถูกต้อง |
Text | Text | การแจงนับตัวคั่นที่กำหนดไว้ล่วงหน้าสำหรับการแยกข้อความเป็นคอลัมน์ |
Theme | Theme | การแจงนับประเภทสีธีมที่เป็นไปได้ |
Value | Value | การแจงนับประเภทค่าที่แสดงผลโดย Range.getValue() และ Range.getValues() จากคลาส Range ของบริการสเปรดชีต ค่าการแจกแจง
ที่ระบุไว้ด้านล่างนี้จะเพิ่มจาก Number, Boolean, Date หรือ String |
Wrap | Wrap | การแจงนับกลยุทธ์ที่ใช้ในการตัดข้อความในเซลล์ |
เมธอด
| วิธีการ | ประเภทการแสดงผล | รายละเอียดแบบย่อ |
|---|---|---|
create(name) | Spreadsheet | สร้างสเปรดชีตใหม่โดยใช้ชื่อที่ระบุ |
create(name, rows, columns) | Spreadsheet | สร้างสเปรดชีตใหม่โดยใช้ชื่อที่ระบุและจำนวนแถวและคอลัมน์ที่ระบุ |
enable | void | เปิดการเรียกใช้ข้อมูลสำหรับแหล่งข้อมูลทุกประเภท |
enable | void | เปิดการดำเนินการข้อมูลสำหรับแหล่งข้อมูล BigQuery |
enable | void | เปิดการเรียกใช้ข้อมูลสําหรับแหล่งข้อมูล Looker |
flush() | void | ใช้การเปลี่ยนแปลงสเปรดชีตที่รอดำเนินการทั้งหมด |
get | Spreadsheet | แสดงสเปรดชีตที่ใช้งานอยู่ หรือ null หากไม่มี |
get | Range | แสดงช่วงที่เลือกในชีตที่ใช้งานอยู่ หรือ null หากไม่มีช่วงที่ใช้งานอยู่ |
get | Range | แสดงรายการช่วงที่ใช้งานอยู่ในชีตที่ใช้งานอยู่ หรือ null หากไม่มีการเลือกช่วง
|
get | Sheet | รับชีตที่ใช้งานอยู่ในสเปรดชีต |
get | Spreadsheet | แสดงสเปรดชีตที่ใช้งานอยู่ หรือ null หากไม่มี |
get | Range|null | แสดงเซลล์ปัจจุบัน (ไฮไลต์) ที่เลือกในหนึ่งในช่วงที่ใช้งานอยู่ใน
ชีตที่ใช้งานอยู่หรือ null หากไม่มีเซลล์ปัจจุบัน |
get | Selection | แสดงผล Selection ปัจจุบันในสเปรดชีต |
get | Ui | แสดงอินสแตนซ์ของสภาพแวดล้อมอินเทอร์เฟซผู้ใช้ของสเปรดชีตที่อนุญาตให้สคริปต์ เพิ่มฟีเจอร์ต่างๆ เช่น เมนู กล่องโต้ตอบ และแถบด้านข้าง |
new | Cell | สร้างเครื่องมือสร้างสำหรับ Cell |
new | Color | สร้างเครื่องมือสร้างสำหรับ Color |
new | Conditional | สร้างเครื่องมือสร้างสำหรับกฎการจัดรูปแบบตามเงื่อนไข |
new | Data | สร้างเครื่องมือสร้างสำหรับ Data |
new | Data | สร้างเครื่องมือสร้างสำหรับกฎการตรวจสอบข้อมูล |
new | Filter | สร้างเครื่องมือสร้างสำหรับ Filter |
new | Rich | สร้างเครื่องมือสร้างสำหรับค่าข้อความที่มีรูปแบบ |
new | Text | สร้างตัวสร้างสำหรับรูปแบบข้อความ |
open(file) | Spreadsheet | เปิดสเปรดชีตที่สอดคล้องกับออบเจ็กต์ File ที่ระบุ |
open | Spreadsheet | เปิดสเปรดชีตที่มีรหัสที่ระบุ |
open | Spreadsheet | เปิดสเปรดชีตด้วย URL ที่ระบุ |
set | Range | ตั้งค่าช่วงที่ระบุเป็น active range โดยมีเซลล์ด้านบนซ้ายในช่วงเป็น current cell |
set | Range | ตั้งค่ารายการช่วงที่ระบุเป็น active ranges |
set | Sheet | ตั้งค่าชีตที่ใช้งานอยู่ในสเปรดชีต |
set | Sheet | ตั้งค่าชีตที่ใช้งานอยู่ในสเปรดชีต โดยมีตัวเลือกในการกู้คืนการเลือกครั้งล่าสุด ภายในชีตนั้น |
set | void | ตั้งค่าสเปรดชีตที่ใช้งานอยู่ |
set | Range | ตั้งค่าเซลล์ที่ระบุเป็น current cell |
เอกสารโดยละเอียด
create(name)
สร้างสเปรดชีตใหม่โดยใช้ชื่อที่ระบุ
// The code below creates a new spreadsheet "Finances" and logs the URL for it const ssNew = SpreadsheetApp.create('Finances'); Logger.log(ssNew.getUrl());
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
name | String | ชื่อของสเปรดชีต |
รีเทิร์น
Spreadsheet - สเปรดชีตใหม่
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets
create(name, rows, columns)
สร้างสเปรดชีตใหม่โดยใช้ชื่อที่ระบุและจำนวนแถวและคอลัมน์ที่ระบุ
// The code below creates a new spreadsheet "Finances" with 50 rows and 5 // columns and logs the URL for it const ssNew = SpreadsheetApp.create('Finances', 50, 5); Logger.log(ssNew.getUrl());
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
name | String | ชื่อของสเปรดชีต |
rows | Integer | จำนวนแถวสำหรับสเปรดชีต |
columns | Integer | จำนวนคอลัมน์สำหรับสเปรดชีต |
รีเทิร์น
Spreadsheet - สเปรดชีตใหม่
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets
enable All Data Sources Execution()
เปิดการเรียกใช้ข้อมูลสำหรับแหล่งข้อมูลทุกประเภท
การดำเนินการข้อมูลจะส่งข้อยกเว้นหากไม่ได้เปิดประเภทแหล่งข้อมูล ใช้วิธีนี้ เพื่อเปิดการเรียกใช้ข้อมูลสำหรับแหล่งข้อมูลทุกประเภท
// Turns data execution on for all types of data sources. SpreadsheetApp.enableAllDataSourcesExecution(); // Opens the spreadsheet file by its ID. If you created your script from a // Google Sheets file, use SpreadsheetApp.getActiveSpreadsheet(). // TODO(developer): Replace the ID with your own. const ss = SpreadsheetApp.openById('abc123456'); // Gets the first data source sheet in the spreadsheet and refreshes the data. ss.getDataSourceSheets()[0].refreshData();
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/bigquery.readonly
enable Big Query Execution()
เปิดการดำเนินการข้อมูลสำหรับแหล่งข้อมูล BigQuery
การดำเนินการข้อมูลสำหรับแหล่งข้อมูล BigQuery จะทําให้เกิดข้อยกเว้นหากไม่ได้เปิดไว้
// Turns data execution on for BigQuery data sources. SpreadsheetApp.enableBigQueryExecution(); // Opens the spreadsheet file by its ID. If you created your script from a // Google Sheets file, use SpreadsheetApp.getActiveSpreadsheet(). // TODO(developer): Replace the ID with your own. const ss = SpreadsheetApp.openById('abc123456'); // Gets the first data source sheet in the spreadsheet and refreshes the // BigQuery data. ss.getDataSourceSheets()[0].refreshData();
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/bigquery.readonly
enable Looker Execution()
เปิดการเรียกใช้ข้อมูลสําหรับแหล่งข้อมูล Looker
การเรียกใช้ข้อมูลสำหรับแหล่งข้อมูล Looker จะทำให้เกิดข้อยกเว้นหากไม่ได้เปิดไว้
// Turns data execution on for Looker data sources. SpreadsheetApp.enableLookerExecution(); // Opens the spreadsheet file by its ID. If you created your script from a // Google Sheets file, use SpreadsheetApp.getActiveSpreadsheet(). // TODO(developer): Replace the ID with your own. const ss = SpreadsheetApp.openById('abc123456'); // Gets the first data source sheet in the spreadsheet and refreshes the // associated Looker data. ss.getDataSourceSheets()[0].refreshData();
flush()
ใช้การเปลี่ยนแปลงสเปรดชีตที่รอดำเนินการทั้งหมด
บางครั้งระบบจะรวมการดำเนินการในสเปรดชีตเข้าด้วยกันเพื่อปรับปรุงประสิทธิภาพ เช่น เมื่อ ทำการเรียก Range.getValue() หลายครั้ง แต่ในบางครั้งคุณอาจต้องการตรวจสอบว่าการเปลี่ยนแปลงที่รอดำเนินการทั้งหมดเกิดขึ้นทันที เช่น เพื่อแสดงข้อมูลแก่ผู้ใช้ขณะที่สคริปต์กำลังทำงาน
// The code below changes the background color of cells A1 and B1 twenty times. // You should be able to see the updates live in the spreadsheet. If flush() is // not called, the updates may be applied live or may all be applied at once // when the script completes. function colors() { const sheet = SpreadsheetApp.getActiveSheet(); for (let i = 0; i < 20; i++) { if (i % 2 === 0) { sheet.getRange('A1').setBackground('green'); sheet.getRange('B1').setBackground('red'); } else { sheet.getRange('A1').setBackground('red'); sheet.getRange('B1').setBackground('green'); } SpreadsheetApp.flush(); } }
get Active()
แสดงสเปรดชีตที่ใช้งานอยู่ หรือ null หากไม่มี
ฟังก์ชันที่ทำงานในบริบทของสเปรดชีตจะรับการอ้างอิงไปยังออบเจ็กต์สเปรดชีตที่เกี่ยวข้องได้โดยการเรียกใช้ฟังก์ชันนี้
// The code below logs the URL for the active spreadsheet. Logger.log(SpreadsheetApp.getActive().getUrl());
รีเทิร์น
Spreadsheet — ออบเจ็กต์สเปรดชีตที่ใช้งานอยู่
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
get Active Range()
แสดงช่วงที่เลือกในชีตที่ใช้งานอยู่ หรือ null หากไม่มีช่วงที่ใช้งานอยู่ หากเลือกหลายช่วง วิธีนี้จะแสดงเฉพาะช่วงที่เลือกสุดท้าย
โดยทั่วไปแล้วหมายถึงช่วงที่ผู้ใช้เลือกในชีตที่ใช้งานอยู่ แต่ในฟังก์ชันที่กำหนดเองจะหมายถึงเซลล์ที่กำลังคำนวณใหม่
// The code below logs the background color for the active range. const colorObject = SpreadsheetApp.getActiveRange().getBackgroundObject(); // Assume the color has ColorType.RGB. Logger.log(colorObject.asRgbColor().asHexString());
รีเทิร์น
Range - ช่วงที่ใช้งานอยู่
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
get Active Range List()
แสดงรายการช่วงที่ใช้งานอยู่ในชีตที่ใช้งานอยู่ หรือ null หากไม่มีการเลือกช่วง
ระบบจะวางช่วงที่ใช้งานอยู่ซึ่งมีเซลล์ที่ไฮไลต์ปัจจุบันไว้สุดท้ายในรายการ
หากเลือกช่วงเดียว ฟังก์ชันนี้จะทำงานเหมือนget
// Returns the list of active ranges. const rangeList = SpreadsheetApp.getActiveRangeList();
รีเทิร์น
Range - รายการช่วงที่ใช้งานอยู่
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
get Active Sheet()
รับชีตที่ใช้งานอยู่ในสเปรดชีต
ชีตที่ใช้งานในสเปรดชีตคือชีตที่แสดงใน UI ของสเปรดชีต
// The code below logs the name of the active sheet. Logger.log(SpreadsheetApp.getActiveSheet().getName());
รีเทิร์น
Sheet - ออบเจ็กต์ชีตที่ใช้งานอยู่
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
get Active Spreadsheet()
แสดงสเปรดชีตที่ใช้งานอยู่ หรือ null หากไม่มี
ฟังก์ชันที่ทำงานในบริบทของสเปรดชีตจะรับการอ้างอิงไปยังออบเจ็กต์ Spreadsheet ที่เกี่ยวข้องได้โดยการเรียกใช้ฟังก์ชันนี้
// The code below logs the URL for the active spreadsheet. Logger.log(SpreadsheetApp.getActiveSpreadsheet().getUrl());
รีเทิร์น
Spreadsheet — ออบเจ็กต์สเปรดชีตที่ใช้งานอยู่
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
get Current Cell()
แสดงเซลล์ปัจจุบัน (ไฮไลต์) ที่เลือกในหนึ่งในช่วงที่ใช้งานอยู่ใน
ชีตที่ใช้งานอยู่หรือ null หากไม่มีเซลล์ปัจจุบัน
// Returns the current highlighted cell in the one of the active ranges. const currentCell = SpreadsheetApp.getCurrentCell();
รีเทิร์น
Range|null - เซลล์ปัจจุบัน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
get Selection()
แสดงผล Selection ปัจจุบันในสเปรดชีต
const selection = SpreadsheetApp.getSelection(); const currentCell = selection.getCurrentCell();
รีเทิร์น
Selection — รายการที่เลือกในปัจจุบัน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
get Ui()
แสดงอินสแตนซ์ของสภาพแวดล้อมอินเทอร์เฟซผู้ใช้ของสเปรดชีตที่อนุญาตให้สคริปต์ เพิ่มฟีเจอร์ต่างๆ เช่น เมนู กล่องโต้ตอบ และแถบด้านข้าง สคริปต์จะโต้ตอบกับ UI ได้เฉพาะใน อินสแตนซ์ปัจจุบันของสเปรดชีตที่เปิดอยู่ และเฉพาะในกรณีที่สคริปต์เชื่อมโยงกับสเปรดชีต ดูข้อมูลเพิ่มเติมได้ที่ คู่มือเกี่ยวกับเมนูและกล่องโต้ตอบและแถบด้านข้าง
// Add a custom menu to the active spreadsheet, including a separator and a // sub-menu. function onOpen(e) { SpreadsheetApp.getUi() .createMenu('My Menu') .addItem('My menu item', 'myFunction') .addSeparator() .addSubMenu( SpreadsheetApp.getUi() .createMenu('My sub-menu') .addItem('One sub-menu item', 'mySecondFunction') .addItem('Another sub-menu item', 'myThirdFunction'), ) .addToUi(); }
รีเทิร์น
Ui — อินสแตนซ์ของสภาพแวดล้อมอินเทอร์เฟซผู้ใช้ของสเปรดชีตนี้
new Cell Image()
สร้างเครื่องมือสร้างสำหรับ Cell
// Opens the spreadsheet file by its URL. If you created your script from within // a Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet() // instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl( 'https://docs.google.com/spreadsheets/d/abc123456/edit', ); // Gets Sheet1 by its name. const sheet = ss.getSheetByName('Sheet1'); // Gets cell A1 on Sheet1. const range = sheet.getRange('A1'); // Builds an image using a source URL. const cellImage = SpreadsheetApp.newCellImage() .setSourceUrl( 'https://www.gstatic.com/images/branding/productlogos/apps_script/v10/web-64dp/logo_apps_script_color_1x_web_64dp.png', ) .build(); // Sets the image in cell A1. range.setValue(cellImage);
รีเทิร์น
Cell - เครื่องมือสร้างใหม่
new Color()
สร้างเครื่องมือสร้างสำหรับ Color
const rgbColor = SpreadsheetApp.newColor().setRgbColor('#FF0000').build();
รีเทิร์น
Color - เครื่องมือสร้างใหม่
new Conditional Format Rule()
สร้างเครื่องมือสร้างสำหรับกฎการจัดรูปแบบตามเงื่อนไข
// Adds a conditional format rule to a sheet that causes all cells in range // A1:B3 to turn red if they contain a number between 1 and 10. const sheet = SpreadsheetApp.getActive().getActiveSheet(); const range = sheet.getRange('A1:B3'); const rule = SpreadsheetApp.newConditionalFormatRule() .whenNumberBetween(1, 10) .setBackground('#FF0000') .setRanges([range]) .build(); const rules = sheet.getConditionalFormatRules(); rules.push(rule); sheet.setConditionalFormatRules(rules);
รีเทิร์น
Conditional ซึ่งเป็นเครื่องมือสร้างใหม่
new Data Source Spec()
สร้างเครื่องมือสร้างสำหรับ Data
// Opens the spreadsheet file by its URL. If you created your script from within // a Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet() // instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl( 'https://docs.google.com/spreadsheets/d/abc123456/edit', ); // Enables BigQuery. SpreadsheetApp.enableBigQueryExecution(); // Builds a data source specification. // TODO (developer): Update the project ID to your own Google Cloud project ID. const dataSourceSpec = SpreadsheetApp.newDataSourceSpec() .asBigQuery() .setProjectId('project-id-1') .setTableProjectId('bigquery-public-data') .setDatasetId('ncaa_basketball') .setTableId('mbb_historical_teams_games') .build(); // Adds the data source and its data to the spreadsheet. ss.insertDataSourceSheet(dataSourceSpec);
รีเทิร์น
Data - เครื่องมือสร้างใหม่
new Data Validation()
สร้างเครื่องมือสร้างสำหรับกฎการตรวจสอบข้อมูล
const cell = SpreadsheetApp.getActive().getRange('A1'); const rule = SpreadsheetApp.newDataValidation() .requireNumberBetween(1, 100) .setAllowInvalid(false) .setHelpText('Number must be between 1 and 100.') .build(); cell.setDataValidation(rule);
รีเทิร์น
Data - เครื่องมือสร้างใหม่
new Filter Criteria()
สร้างเครื่องมือสร้างสำหรับ Filter
// Opens the spreadsheet file by its URL. If you created your script from within // a Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet() // instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl( 'https://docs.google.com/spreadsheets/d/abc123456/edit', ); // Gets Sheet1 by its name. const sheet = ss.getSheetByName('Sheet1'); // Sets the range to A1:D20. const range = sheet.getRange('A1:D20'); // Creates a filter and applies it to the specified range. range.createFilter(); // Gets the current filter for the range and creates filter criteria that only // shows cells that aren't empty. const filter = range.getFilter(); const criteria = SpreadsheetApp.newFilterCriteria().whenCellNotEmpty().build(); // Sets the criteria to column C. filter.setColumnFilterCriteria(3, criteria);
รีเทิร์น
Filter - เครื่องมือสร้างใหม่
new Rich Text Value()
สร้างเครื่องมือสร้างสำหรับค่าข้อความที่มีรูปแบบ
// Sets cell A1 to have the text "Hello world", with "Hello" bolded. const cell = SpreadsheetApp.getActive().getRange('A1'); const bold = SpreadsheetApp.newTextStyle().setBold(true).build(); const value = SpreadsheetApp.newRichTextValue() .setText('Hello world') .setTextStyle(0, 5, bold) .build(); cell.setRichTextValue(value);
รีเทิร์น
Rich - เครื่องมือสร้างใหม่
new Text Style()
สร้างตัวสร้างสำหรับรูปแบบข้อความ
// Sets range A1:B3 to have red, size 22, bolded, underlined text. const range = SpreadsheetApp.getActive().getRange('A1:B3'); const style = SpreadsheetApp.newTextStyle() .setForegroundColor('red') .setFontSize(22) .setBold(true) .setUnderline(true) .build(); range.setTextStyle(style);
รีเทิร์น
Text - เครื่องมือสร้างใหม่
open(file)
เปิดสเปรดชีตที่สอดคล้องกับออบเจ็กต์ File ที่ระบุ
// Get any starred spreadsheets from Google Drive, then open the spreadsheets // and log the name of the first sheet within each spreadsheet. const files = DriveApp.searchFiles( `starred = true and mimeType = "${MimeType.GOOGLE_SHEETS}"`, ); while (files.hasNext()) { const spreadsheet = SpreadsheetApp.open(files.next()); const sheet = spreadsheet.getSheets()[0]; Logger.log(sheet.getName()); }
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
file | File | ไฟล์ที่จะเปิด |
รีเทิร์น
Spreadsheet - สเปรดชีต
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets
open By Id(id)
เปิดสเปรดชีตที่มีรหัสที่ระบุ คุณสามารถดึงรหัสของสเปรดชีตจาก URL ของสเปรดชีตนั้น ตัวอย่างเช่น รหัสสเปรดชีตใน URL https://docs.google.com/spreadsheets/d/abc1234567/edit#gid=0 คือ "abc1234567"
// The code below opens a spreadsheet using its ID and logs the name for it. // Note that the spreadsheet is NOT physically opened on the client side. // It is opened on the server only (for modification by the script). const ss = SpreadsheetApp.openById('abc1234567'); Logger.log(ss.getName());
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
id | String | ตัวระบุที่ไม่ซ้ำกันสำหรับสเปรดชีต |
รีเทิร์น
Spreadsheet — ออบเจ็กต์สเปรดชีตที่มีรหัสที่ระบุ
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets
open By Url(url)
เปิดสเปรดชีตด้วย URL ที่ระบุ ส่งข้อยกเว้นการเขียนสคริปต์หากไม่มี URL หรือผู้ใช้ไม่มีสิทธิ์เข้าถึง
// Opens a spreadsheet by its URL and logs its name. // Note that the spreadsheet doesn't physically open on the client side. // It opens on the server only (for modification by the script). const ss = SpreadsheetApp.openByUrl( 'https://docs.google.com/spreadsheets/d/abc1234567/edit', ); console.log(ss.getName());
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
url | String | URL ของสเปรดชีต |
รีเทิร์น
Spreadsheet - ออบเจ็กต์สเปรดชีตที่มี URL ที่ระบุ
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets
set Active Range(range)
ตั้งค่าช่วงที่ระบุเป็น active range โดยมีเซลล์ด้านบนซ้ายในช่วงเป็น current cell
UI ของสเปรดชีตจะแสดงชีตที่มีช่วงที่เลือกและเลือกเซลล์ที่กำหนดไว้ในช่วงที่เลือก
// The code below sets range C1:D4 in the first sheet as the active range. const range = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0].getRange('C1:D4'); SpreadsheetApp.setActiveRange(range); const selection = SpreadsheetApp.getSelection(); // Current cell: C1 const currentCell = selection.getCurrentCell(); // Active Range: C1:D4 const activeRange = selection.getActiveRange();
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
range | Range | ช่วงที่จะทำให้เป็นช่วงที่ใช้งานอยู่ |
รีเทิร์น
Range - ช่วงที่ใช้งานใหม่
set Active Range List(rangeList)
ตั้งค่ารายการช่วงที่ระบุเป็น active ranges ระบบจะตั้งค่าช่วงสุดท้ายในรายการเป็น active range
// The code below sets ranges [D4, B2:C4] in the active sheet as the active // ranges. const rangeList = SpreadsheetApp.getActiveSheet().getRanges(['D4', 'B2:C4']); SpreadsheetApp.setActiveRangeList(rangeList); const selection = SpreadsheetApp.getSelection(); // Current cell: B2 const currentCell = selection.getCurrentCell(); // Active range: B2:C4 const activeRange = selection.getActiveRange(); // Active range list: [D4, B2:C4] const activeRangeList = selection.getActiveRangeList();
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
range | Range | รายการช่วงที่จะเลือก |
รีเทิร์น
Range - รายการช่วงที่เลือกใหม่
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
set Active Sheet(sheet)
ตั้งค่าชีตที่ใช้งานอยู่ในสเปรดชีต UI ของ Google ชีตจะแสดงชีตที่เลือก เว้นแต่ ชีตนั้นจะอยู่ในสเปรดชีตอื่น
// The code below makes the 2nd sheet active in the active spreadsheet. const spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); SpreadsheetApp.setActiveSheet(spreadsheet.getSheets()[1]);
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
sheet | Sheet | ชีตใหม่ที่ใช้งานอยู่ |
รีเทิร์น
Sheet — ชีตที่ได้รับการตั้งค่าให้เป็นชีตที่ใช้งานอยู่ใหม่
set Active Sheet(sheet, restoreSelection)
ตั้งค่าชีตที่ใช้งานอยู่ในสเปรดชีต โดยมีตัวเลือกในการกู้คืนการเลือกครั้งล่าสุด ภายในชีตนั้น UI ของ Google ชีตจะแสดงชีตที่เลือก เว้นแต่ชีตนั้นจะอยู่ในสเปรดชีตอื่น
const spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); const firstSheet = spreadsheet.getSheets()[0]; const secondSheet = spreadsheet.getSheets()[1]; // Set the first sheet as the active sheet and select the range D4:F4. spreadsheet.setActiveSheet(firstSheet).getRange('D4:F4').activate(); // Switch to the second sheet to do some work. spreadsheet.setActiveSheet(secondSheet); // Switch back to first sheet, and restore its selection. spreadsheet.setActiveSheet(firstSheet, true); // The selection of first sheet is restored, and it logs D4:F4 const range = spreadsheet.getActiveSheet().getSelection().getActiveRange(); Logger.log(range.getA1Notation());
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
sheet | Sheet | ชีตใหม่ที่ใช้งานอยู่ |
restore | Boolean | หากเป็น true ระบบจะเลือกแผ่นงานใหม่ที่ใช้งานอยู่ล่าสุดอีกครั้งเมื่อแผ่นงานใหม่ใช้งานได้ หากเป็น false แผ่นงานใหม่จะใช้งานได้โดยไม่เปลี่ยนการเลือกปัจจุบัน |
รีเทิร์น
Sheet - ชีตใหม่ที่ใช้งานอยู่
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
set Active Spreadsheet(newActiveSpreadsheet)
ตั้งค่าสเปรดชีตที่ใช้งานอยู่
// The code below makes the spreadsheet with key "1234567890" the active // spreadsheet const ss = SpreadsheetApp.openById('1234567890'); SpreadsheetApp.setActiveSpreadsheet(ss);
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
new | Spreadsheet | สเปรดชีตที่จะทำให้เป็นสเปรดชีตที่ใช้งานอยู่ |
set Current Cell(cell)
ตั้งค่าเซลล์ที่ระบุเป็น current cell
หากเซลล์ที่ระบุอยู่ในช่วงที่เลือกอยู่แล้ว ช่วงนั้นจะกลายเป็นช่วงที่ใช้งานอยู่โดยมีเซลล์เป็นเซลล์ปัจจุบัน
หากเซลล์ที่ระบุไม่อยู่ในช่วงที่เลือก ระบบจะนำการเลือกที่มีอยู่ออก และเซลล์นั้นจะกลายเป็นเซลล์ปัจจุบันและช่วงที่ใช้งานอยู่
หมายเหตุ: Range ที่ระบุต้องประกอบด้วยเซลล์ 1 เซลล์ ไม่เช่นนั้นวิธีการนี้จะ
ส่งข้อยกเว้น
// The code below sets the cell B5 in the first sheet as the current cell. const cell = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0].getRange('B5'); SpreadsheetApp.setCurrentCell(cell); const selection = SpreadsheetApp.getSelection(); // Current cell: B5 const currentCell = selection.getCurrentCell();
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
cell | Range | เซลล์ที่จะตั้งเป็นเซลล์ปัจจุบัน |
รีเทิร์น
Range — เซลล์ปัจจุบันที่ตั้งค่าใหม่
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets