เข้าถึงและสร้างไฟล์ Google ชีต คลาสนี้เป็นคลาสหลักของบริการสเปรดชีต
พร็อพเพอร์ตี้
พร็อพเพอร์ตี้ | Type | คำอธิบาย |
---|---|---|
AutoFillSeries | AutoFillSeries | การแจงนับประเภทของอนุกรมที่ใช้เพื่อคํานวณค่าที่กรอกโดยอัตโนมัติ |
BandingTheme | BandingTheme | การแจกแจงธีมแถบสีที่เป็นไปได้ |
BooleanCriteria | BooleanCriteria | การแจกแจงเกณฑ์บูลีนสำหรับการจัดรูปแบบตามเงื่อนไข |
BorderStyle | BorderStyle | การแจกแจงรูปแบบที่ถูกต้องสำหรับการตั้งค่าเส้นขอบใน Range |
ColorType | ColorType | การแจกแจงประเภทสีที่เป็นไปได้ |
CopyPasteType | CopyPasteType | การแจกแจงประเภทการวางที่เป็นไปได้ |
DataExecutionErrorCode | DataExecutionErrorCode | การแจกแจงรหัสข้อผิดพลาดที่เป็นไปได้ของการดำเนินการข้อมูล |
DataExecutionState | DataExecutionState | การแจกแจงสถานะการดำเนินการข้อมูลที่เป็นไปได้ |
DataSourceParameterType | DataSourceParameterType | การแจงนับประเภทพารามิเตอร์แหล่งข้อมูลที่เป็นไปได้ |
DataSourceRefreshScope | DataSourceRefreshScope | การแจกแจงขอบเขตการรีเฟรชแหล่งข้อมูลที่เป็นไปได้ |
DataSourceType | DataSourceType | การแจกแจงประเภทแหล่งข้อมูลที่เป็นไปได้ |
DataValidationCriteria | DataValidationCriteria | การแจกแจงที่แสดงถึงเกณฑ์การตรวจสอบข้อมูลที่สามารถตั้งค่าในช่วง |
DateTimeGroupingRuleType | DateTimeGroupingRuleType | การแจกแจงกฎการจัดกลุ่มวันที่และเวลา |
DeveloperMetadataLocationType | DeveloperMetadataLocationType | การแจงนับประเภทตำแหน่งข้อมูลเมตาของนักพัฒนาซอฟต์แวร์ที่เป็นไปได้ |
DeveloperMetadataVisibility | DeveloperMetadataVisibility | การแจกแจงระดับการเข้าถึงข้อมูลเมตาของนักพัฒนาซอฟต์แวร์ที่เป็นไปได้ |
Dimension | Dimension | การแจกแจงมิติข้อมูลที่เป็นไปได้ของสเปรดชีต |
Direction | Direction | การแจงนับเส้นทางที่เป็นไปได้ที่เคลื่อนที่ภายในสเปรดชีตโดยใช้ปุ่มลูกศร |
FrequencyType | FrequencyType | การแจกแจงประเภทความถี่ที่เป็นไปได้ |
GroupControlTogglePosition | GroupControlTogglePosition | การแจงนับตำแหน่งที่เปิด/ปิดการควบคุมกลุ่มได้ |
InterpolationType | InterpolationType | การแจกแจงประเภทการประมาณค่าการไล่ระดับสีสำหรับรูปแบบตามเงื่อนไข |
PivotTableSummarizeFunction | PivotTableSummarizeFunction | การแจกแจงฟังก์ชันที่อาจใช้เพื่อสรุปค่าในตาราง Pivot |
PivotValueDisplayType | PivotValueDisplayType | การแจกแจงวิธีการแสดงค่า Pivot |
ProtectionType | ProtectionType | การแจกแจงแสดงส่วนต่างๆ ของสเปรดชีตที่ป้องกันจากการแก้ไขได้ |
RecalculationInterval | RecalculationInterval | การแจกแจงช่วงเวลาที่เป็นไปได้ที่ใช้ในการคำนวณสเปรดชีตใหม่ |
RelativeDate | RelativeDate | การแจกแจงตัวเลือกวันที่สัมพัทธ์สำหรับการคำนวณค่าที่จะใช้ใน BooleanCriteria ตามวันที่ |
SheetType | SheetType | การแจกแจงชีตประเภทต่างๆ ที่มีอยู่ในสเปรดชีต |
SortOrder | SortOrder | การแจกแจงลำดับการจัดเรียง |
TextDirection | TextDirection | การแจกแจงเส้นทางข้อความที่ถูกต้อง |
TextToColumnsDelimiter | TextToColumnsDelimiter | การแจกแจงตัวคั่นที่กำหนดไว้ล่วงหน้าสำหรับการแยกข้อความเป็นคอลัมน์ |
ThemeColorType | ThemeColorType | การแจกแจงประเภทสีธีมที่เป็นไปได้ |
ValueType | ValueType | การแจกแจงประเภทค่าที่ Range.getValue() และ Range.getValues() แสดงผลจากคลาสช่วงของบริการสเปรดชีต ค่าแจกแจงที่ระบุไว้ด้านล่างเป็นนอกเหนือจาก Number , Boolean , Date หรือ String |
WrapStrategy | WrapStrategy | การแจกแจงกลยุทธ์ที่ใช้สำหรับการตัดเซลล์ |
วิธีการ
วิธีการ | ประเภทการแสดงผล | รายละเอียดแบบย่อ |
---|---|---|
create(name) | Spreadsheet | สร้างสเปรดชีตใหม่ที่มีชื่อตามที่ระบุ |
create(name, rows, columns) | Spreadsheet | สร้างสเปรดชีตใหม่ที่มีชื่อตามจํานวนแถวและคอลัมน์ที่ระบุ |
enableAllDataSourcesExecution() | void | เปิดการดําเนินการกับแหล่งข้อมูลทุกประเภท |
enableBigQueryExecution() | void | เปิดการดําเนินการข้อมูลสําหรับแหล่งข้อมูล BigQuery |
flush() | void | ใช้การเปลี่ยนแปลงในสเปรดชีตที่รอดำเนินการทั้งหมด |
getActive() | Spreadsheet | แสดงผลสเปรดชีตที่ใช้งานอยู่ในปัจจุบัน หรือแสดงผล null หากไม่มี |
getActiveRange() | Range | แสดงผลช่วงที่เลือกในชีตที่ใช้งานอยู่ หรือแสดงผล null หากไม่มีช่วงที่ใช้งานอยู่ |
getActiveRangeList() | RangeList | แสดงผลรายการช่วงที่ใช้งานอยู่ในชีตที่ใช้งานอยู่ หรือ null หากไม่มีการเลือกช่วงไว้ |
getActiveSheet() | Sheet | ดึงชีตที่ใช้งานอยู่ในสเปรดชีต |
getActiveSpreadsheet() | Spreadsheet | แสดงผลสเปรดชีตที่ใช้งานอยู่ในปัจจุบัน หรือแสดงผล null หากไม่มี |
getCurrentCell() | Range | แสดงผลเซลล์ปัจจุบัน (ที่ไฮไลต์ไว้) ที่เลือกไว้ในช่วงที่มีการใช้งานช่วงใดช่วงหนึ่งในชีตที่ใช้งาน หรือ null หากไม่มีเซลล์ปัจจุบัน |
getSelection() | Selection | แสดง Selection ปัจจุบันในสเปรดชีต |
getUi() | Ui | แสดงผลอินสแตนซ์ของสภาพแวดล้อมอินเทอร์เฟซผู้ใช้ของสเปรดชีตที่อนุญาตให้สคริปต์เพิ่มฟีเจอร์ต่างๆ เช่น เมนู กล่องโต้ตอบ และแถบด้านข้าง |
newCellImage() | CellImageBuilder | สร้างเครื่องมือสร้างสำหรับ CellImage |
newColor() | ColorBuilder | สร้างเครื่องมือสร้างสำหรับ Color |
newConditionalFormatRule() | ConditionalFormatRuleBuilder | สร้างเครื่องมือสร้างสำหรับกฎการจัดรูปแบบตามเงื่อนไข |
newDataSourceSpec() | DataSourceSpecBuilder | สร้างเครื่องมือสร้างสำหรับ DataSourceSpec |
newDataValidation() | DataValidationBuilder | สร้างเครื่องมือสร้างสำหรับกฎการตรวจสอบข้อมูล |
newFilterCriteria() | FilterCriteriaBuilder | สร้างเครื่องมือสร้างสำหรับ FilterCriteria |
newRichTextValue() | RichTextValueBuilder | สร้างเครื่องมือสร้างสำหรับค่า Rich Text |
newTextStyle() | TextStyleBuilder | สร้างเครื่องมือสร้างสำหรับรูปแบบข้อความ |
open(file) | Spreadsheet | เปิดสเปรดชีตที่ตรงกับออบเจ็กต์ไฟล์ที่ระบุ |
openById(id) | Spreadsheet | เปิดสเปรดชีตที่มีรหัสที่ระบุ |
openByUrl(url) | Spreadsheet | เปิดสเปรดชีตที่มี URL ที่ให้มา |
setActiveRange(range) | Range | ตั้งค่าช่วงที่ระบุเป็น active range โดยเซลล์ซ้ายบนสุดของช่วงเป็น current cell |
setActiveRangeList(rangeList) | RangeList | ตั้งค่ารายการช่วงที่ระบุเป็น active ranges |
setActiveSheet(sheet) | Sheet | ตั้งค่าชีตที่ใช้งานอยู่ในสเปรดชีต |
setActiveSheet(sheet, restoreSelection) | Sheet | ตั้งค่าชีตที่ใช้งานอยู่ในสเปรดชีต โดยมีตัวเลือกให้คืนค่ารายการที่เลือกล่าสุดภายในชีต |
setActiveSpreadsheet(newActiveSpreadsheet) | void | ตั้งค่าสเปรดชีตที่ใช้งานอยู่ |
setCurrentCell(cell) | Range | ตั้งค่าเซลล์ที่ระบุเป็น current cell |
เอกสารประกอบโดยละเอียด
create(name)
สร้างสเปรดชีตใหม่ที่มีชื่อตามที่ระบุ
// The code below creates a new spreadsheet "Finances" and logs the URL for it var ssNew = SpreadsheetApp.create("Finances"); Logger.log(ssNew.getUrl());
พารามิเตอร์
ชื่อ | Type | คำอธิบาย |
---|---|---|
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 var ssNew = SpreadsheetApp.create("Finances", 50, 5); Logger.log(ssNew.getUrl());
พารามิเตอร์
ชื่อ | Type | คำอธิบาย |
---|---|---|
name | String | ชื่อสเปรดชีต |
rows | Integer | จำนวนแถวของสเปรดชีต |
columns | Integer | จำนวนคอลัมน์สำหรับสเปรดชีต |
รีเทิร์น
Spreadsheet
— สเปรดชีตใหม่
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์จากขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets
enableAllDataSourcesExecution()
เปิดการดําเนินการกับแหล่งข้อมูลทุกประเภท
การดำเนินการข้อมูลจะมีข้อยกเว้นหากไม่ได้เปิดประเภทแหล่งข้อมูลไว้ ใช้วิธีนี้เพื่อเปิดการดำเนินการกับข้อมูลสำหรับแหล่งข้อมูลทุกประเภท
// 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
enableBigQueryExecution()
เปิดการดําเนินการข้อมูลสําหรับแหล่งข้อมูล 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
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() { var sheet = SpreadsheetApp.getActiveSheet(); for (var 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(); } }
getActive()
แสดงผลสเปรดชีตที่ใช้งานอยู่ในปัจจุบัน หรือแสดงผล 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
getActiveRange()
แสดงผลช่วงที่เลือกในชีตที่ใช้งานอยู่ หรือแสดงผล null
หากไม่มีช่วงที่ใช้งานอยู่ หากเลือกหลายช่วง วิธีนี้จะแสดงผลเฉพาะช่วงที่เลือกล่าสุดเท่านั้น
โดยทั่วไปจะหมายถึงช่วงที่ผู้ใช้เลือกไว้ในชีตที่ใช้งานอยู่ แต่ฟังก์ชันที่กำหนดเองหมายถึงช่วงที่มีการคำนวณใหม่อยู่ในเซลล์นั้น
// The code below logs the background color for the active range. var 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
getActiveRangeList()
แสดงผลรายการช่วงที่ใช้งานอยู่ในชีตที่ใช้งานอยู่ หรือ null
หากไม่มีการเลือกช่วงไว้ ช่วงที่มีการใช้งานซึ่งมีเซลล์ที่ไฮไลต์ในปัจจุบันจะอยู่ในลำดับสุดท้ายในรายการ
หากเลือกช่วงเดียวไว้ การดำเนินการนี้จะทำงานเป็นการเรียกใช้ getActiveRange()
// Returns the list of active ranges. var rangeList = SpreadsheetApp.getActiveRangeList();
รีเทิร์น
RangeList
— รายการช่วงที่มีการใช้งาน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์จากขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getActiveSheet()
ดึงชีตที่ใช้งานอยู่ในสเปรดชีต
แผ่นงานที่ใช้งานอยู่ในสเปรดชีตคือแผ่นงานที่แสดงอยู่ใน 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
getActiveSpreadsheet()
แสดงผลสเปรดชีตที่ใช้งานอยู่ในปัจจุบัน หรือแสดงผล 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
getCurrentCell()
แสดงผลเซลล์ปัจจุบัน (ที่ไฮไลต์ไว้) ที่เลือกไว้ในช่วงที่มีการใช้งานช่วงใดช่วงหนึ่งในชีตที่ใช้งาน หรือ null
หากไม่มีเซลล์ปัจจุบัน
// Returns the current highlighted cell in the one of the active ranges. var currentCell = SpreadsheetApp.getCurrentCell();
รีเทิร์น
Range
— เซลล์ปัจจุบัน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์จากขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getSelection()
แสดง Selection
ปัจจุบันในสเปรดชีต
var selection = SpreadsheetApp.getSelection(); var currentCell = selection.getCurrentCell();
รีเทิร์น
Selection
— ตัวเลือกปัจจุบัน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์จากขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getUi()
แสดงผลอินสแตนซ์ของสภาพแวดล้อมอินเทอร์เฟซผู้ใช้ของสเปรดชีตที่อนุญาตให้สคริปต์เพิ่มฟีเจอร์ต่างๆ เช่น เมนู กล่องโต้ตอบ และแถบด้านข้าง สคริปต์จะโต้ตอบกับ 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
— อินสแตนซ์ของสภาพแวดล้อมอินเทอร์เฟซผู้ใช้ของสเปรดชีตนี้
newCellImage()
สร้างเครื่องมือสร้างสำหรับ CellImage
// 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);
รีเทิร์น
CellImageBuilder
— เครื่องมือสร้างใหม่
newColor()
สร้างเครื่องมือสร้างสำหรับ Color
var rgbColor = SpreadsheetApp.newColor().setRgbColor("#FF0000").build();
รีเทิร์น
ColorBuilder
— เครื่องมือสร้างใหม่
newConditionalFormatRule()
สร้างเครื่องมือสร้างสำหรับกฎการจัดรูปแบบตามเงื่อนไข
// 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. var sheet = SpreadsheetApp.getActive().getActiveSheet(); var range = sheet.getRange('A1:B3'); var rule = SpreadsheetApp.newConditionalFormatRule() .whenNumberBetween(1, 10) .setBackground("#FF0000") .setRanges([range]) .build() var rules = sheet.getConditionalFormatRules(); rules.push(rule); sheet.setConditionalFormatRules(rules);
รีเทิร์น
ConditionalFormatRuleBuilder
— เครื่องมือสร้างใหม่
newDataSourceSpec()
สร้างเครื่องมือสร้างสำหรับ DataSourceSpec
// 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);
รีเทิร์น
DataSourceSpecBuilder
— เครื่องมือสร้างใหม่
newDataValidation()
สร้างเครื่องมือสร้างสำหรับกฎการตรวจสอบข้อมูล
var cell = SpreadsheetApp.getActive().getRange('A1'); var rule = SpreadsheetApp.newDataValidation() .requireNumberBetween(1, 100) .setAllowInvalid(false) .setHelpText('Number must be between 1 and 100.') .build(); cell.setDataValidation(rule);
รีเทิร์น
DataValidationBuilder
— เครื่องมือสร้างใหม่
newFilterCriteria()
สร้างเครื่องมือสร้างสำหรับ FilterCriteria
// 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);
รีเทิร์น
FilterCriteriaBuilder
— เครื่องมือสร้างใหม่
newRichTextValue()
สร้างเครื่องมือสร้างสำหรับค่า Rich Text
// Sets cell A1 to have the text "Hello world", with "Hello" bolded. var cell = SpreadsheetApp.getActive().getRange('A1'); var bold = SpreadsheetApp.newTextStyle().setBold(true).build(); var value = SpreadsheetApp.newRichTextValue() .setText("Hello world") .setTextStyle(0, 5, bold) .build(); cell.setRichTextValue(value);
รีเทิร์น
RichTextValueBuilder
— เครื่องมือสร้างใหม่
newTextStyle()
สร้างเครื่องมือสร้างสำหรับรูปแบบข้อความ
// Sets range A1:B3 to have red, size 22, bolded, underlined text. var range = SpreadsheetApp.getActive().getRange('A1:B3'); var style = SpreadsheetApp.newTextStyle() .setForegroundColor("red") .setFontSize(22) .setBold(true) .setUnderline(true) .build(); range.setTextStyle(style);
รีเทิร์น
TextStyleBuilder
— เครื่องมือสร้างใหม่
open(file)
เปิดสเปรดชีตที่ตรงกับออบเจ็กต์ไฟล์ที่ระบุ
// Get any starred spreadsheets from Google Drive, then open the spreadsheets and log the name // of the first sheet within each spreadsheet. var files = DriveApp.searchFiles( 'starred = true and mimeType = "' + MimeType.GOOGLE_SHEETS + '"'); while (files.hasNext()) { var spreadsheet = SpreadsheetApp.open(files.next()); var sheet = spreadsheet.getSheets()[0]; Logger.log(sheet.getName()); }
พารามิเตอร์
ชื่อ | Type | คำอธิบาย |
---|---|---|
file | File | ไฟล์ที่จะเปิด |
รีเทิร์น
Spreadsheet
— สเปรดชีต
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์จากขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets
openById(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). var ss = SpreadsheetApp.openById("abc1234567"); Logger.log(ss.getName());
พารามิเตอร์
ชื่อ | Type | คำอธิบาย |
---|---|---|
id | String | ตัวระบุที่ไม่ซ้ำกันสำหรับสเปรดชีต |
รีเทิร์น
Spreadsheet
— ออบเจ็กต์สเปรดชีตที่มีรหัสที่ระบุ
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์จากขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets
openByUrl(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). var ss = SpreadsheetApp.openByUrl( 'https://docs.google.com/spreadsheets/d/abc1234567/edit'); console.log(ss.getName());
พารามิเตอร์
ชื่อ | Type | คำอธิบาย |
---|---|---|
url | String | URL สำหรับสเปรดชีต |
รีเทิร์น
Spreadsheet
— ออบเจ็กต์สเปรดชีตที่มี URL ที่ให้มา
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์จากขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets
setActiveRange(range)
ตั้งค่าช่วงที่ระบุเป็น active range
โดยเซลล์ซ้ายบนสุดของช่วงเป็น current cell
UI ของสเปรดชีตจะแสดงชีตที่มีช่วงที่เลือกและเลือกเซลล์ที่กำหนดไว้ในช่วงที่เลือก
// The code below sets range C1:D4 in the first sheet as the active range. var range = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0].getRange('C1:D4'); SpreadsheetApp.setActiveRange(range); var selection = SpreadsheetApp.getSelection(); // Current cell: C1 var currentCell = selection.getCurrentCell(); // Active Range: C1:D4 var activeRange = selection.getActiveRange();
พารามิเตอร์
ชื่อ | Type | คำอธิบาย |
---|---|---|
range | Range | ช่วงที่จะกำหนดเป็นช่วงที่มีการใช้งาน |
รีเทิร์น
Range
— ช่วงที่ใช้งานอยู่ใหม่
setActiveRangeList(rangeList)
ตั้งค่ารายการช่วงที่ระบุเป็น active ranges
ช่วงสุดท้ายในรายการจะตั้งค่าเป็น active range
// The code below sets ranges [D4, B2:C4] in the active sheet as the active ranges. var rangeList = SpreadsheetApp.getActiveSheet().getRanges(['D4', 'B2:C4']); SpreadsheetApp.setActiveRangeList(rangeList); var selection = SpreadsheetApp.getSelection(); // Current cell: B2 var currentCell = selection.getCurrentCell(); // Active range: B2:C4 var activeRange = selection.getActiveRange(); // Active range list: [D4, B2:C4] var activeRangeList = selection.getActiveRangeList();
พารามิเตอร์
ชื่อ | Type | คำอธิบาย |
---|---|---|
rangeList | RangeList | รายการช่วงที่จะเลือก |
รีเทิร์น
RangeList
— รายการช่วงที่เลือกใหม่
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์จากขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setActiveSheet(sheet)
ตั้งค่าชีตที่ใช้งานอยู่ในสเปรดชีต UI ของ Google ชีตจะแสดงชีตที่เลือก เว้นแต่ว่าชีตนั้นจะเป็นสเปรดชีตอื่น
// The code below makes the 2nd sheet active in the active spreadsheet. var spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); SpreadsheetApp.setActiveSheet(spreadsheet.getSheets()[1]);
พารามิเตอร์
ชื่อ | Type | คำอธิบาย |
---|---|---|
sheet | Sheet | ชีตใหม่ที่ใช้งานอยู่ |
รีเทิร์น
Sheet
— ชีตที่กำหนดให้เป็นแผ่นงานใหม่ที่ใช้งานอยู่
setActiveSheet(sheet, restoreSelection)
ตั้งค่าชีตที่ใช้งานอยู่ในสเปรดชีต โดยมีตัวเลือกให้คืนค่ารายการที่เลือกล่าสุดภายในชีต UI ของ Google ชีตจะแสดงชีตที่เลือก เว้นแต่ว่าชีตนั้นจะเป็นของสเปรดชีตอื่น
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); var firstSheet = spreadsheet.getSheets()[0]; var 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 var range = spreadsheet.getActiveSheet().getSelection().getActiveRange(); Logger.log(range.getA1Notation());
พารามิเตอร์
ชื่อ | Type | คำอธิบาย |
---|---|---|
sheet | Sheet | ชีตใหม่ที่ใช้งานอยู่ |
restoreSelection | Boolean | หากเลือก true ระบบจะเลือกการเลือกล่าสุดของชีตใหม่ที่ใช้งานอยู่อีกครั้งเมื่อชีตใหม่ใช้งานได้ หากเป็น false ชีตใหม่จะใช้งานได้โดยไม่เปลี่ยนการเลือกปัจจุบัน |
รีเทิร์น
Sheet
— แผ่นงานใหม่ที่ใช้งานอยู่
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์จากขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setActiveSpreadsheet(newActiveSpreadsheet)
ตั้งค่าสเปรดชีตที่ใช้งานอยู่
// The code below makes the spreadsheet with key "1234567890" the active spreadsheet var ss = SpreadsheetApp.openById("1234567890"); SpreadsheetApp.setActiveSpreadsheet(ss);
พารามิเตอร์
ชื่อ | Type | คำอธิบาย |
---|---|---|
newActiveSpreadsheet | Spreadsheet | สเปรดชีตที่จะถูกทำให้เป็นสเปรดชีตที่ใช้งานอยู่ |
setCurrentCell(cell)
ตั้งค่าเซลล์ที่ระบุเป็น current cell
ถ้าเซลล์ที่ระบุอยู่ในช่วงที่เลือกไว้แล้ว ช่วงนั้นจะกลายเป็นช่วงที่ใช้งานอยู่โดยมีเซลล์นั้นเป็นเซลล์ปัจจุบัน
หากเซลล์ที่ระบุไม่อยู่ในช่วงที่เลือก ระบบจะนำการเลือกที่มีอยู่ออก และเซลล์นั้นจะกลายเป็นเซลล์ปัจจุบันและช่วงที่ใช้งานอยู่
หมายเหตุ: Range
ที่ระบุต้องประกอบด้วยเซลล์เดียว มิฉะนั้นเมธอดนี้จะมีการส่งข้อยกเว้น
// The code below sets the cell B5 in the first sheet as the current cell. var cell = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0].getRange('B5'); SpreadsheetApp.setCurrentCell(cell); var selection = SpreadsheetApp.getSelection(); // Current cell: B5 var currentCell = selection.getCurrentCell();
พารามิเตอร์
ชื่อ | Type | คำอธิบาย |
---|---|---|
cell | Range | เซลล์ที่จะตั้งค่าเป็นเซลล์ปัจจุบัน |
รีเทิร์น
Range
— เซลล์ปัจจุบันที่ตั้งใหม่
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์จากขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets