Class SpreadsheetApp

SpreadsheetApp

เข้าถึงและสร้างไฟล์ Google ชีต ชั้นเรียนนี้เป็นชั้นเรียนหลักสำหรับบริการสเปรดชีต

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

พร็อพเพอร์ตี้ประเภทคำอธิบาย
AutoFillSeriesAutoFillSeriesการแจงนับประเภทชุดข้อมูลที่ใช้ในการคำนวณค่าที่ป้อนอัตโนมัติ
BandingThemeBandingThemeการแจกแจงธีมของแถบสีที่เป็นไปได้
BooleanCriteriaBooleanCriteriaการแจงนับเกณฑ์บูลีนสำหรับการจัดรูปแบบตามเงื่อนไข
BorderStyleBorderStyleการแจงนับรูปแบบที่ถูกต้องสำหรับการตั้งค่าเส้นขอบใน Range
ColorTypeColorTypeการแจกแจงประเภทสีที่เป็นไปได้
CopyPasteTypeCopyPasteTypeการแจงนับประเภทการวางที่เป็นไปได้
DataExecutionErrorCodeDataExecutionErrorCodeการแจงนับรหัสข้อผิดพลาดของการเรียกใช้ข้อมูลที่เป็นไปได้
DataExecutionStateDataExecutionStateการแจกแจงสถานะการดำเนินการกับข้อมูลที่เป็นไปได้
DataSourceParameterTypeDataSourceParameterTypeการแจกแจงประเภทพารามิเตอร์แหล่งข้อมูลที่เป็นไปได้
DataSourceRefreshScopeDataSourceRefreshScopeการแจงนับขอบเขตการรีเฟรชแหล่งข้อมูลที่เป็นไปได้
DataSourceTypeDataSourceTypeการแจกแจงประเภทแหล่งข้อมูลที่เป็นไปได้
DataValidationCriteriaDataValidationCriteriaการแจกแจงที่แสดงถึงเกณฑ์การตรวจสอบข้อมูลที่ตั้งค่าได้ในช่วง
DateTimeGroupingRuleTypeDateTimeGroupingRuleTypeการแจงนับของกฎการจัดกลุ่มวันที่และเวลา
DeveloperMetadataLocationTypeDeveloperMetadataLocationTypeการแจงนับประเภทตำแหน่งข้อมูลเมตาของนักพัฒนาซอฟต์แวร์ที่เป็นไปได้
DeveloperMetadataVisibilityDeveloperMetadataVisibilityการแจกแจงระดับการเข้าถึงข้อมูลเมตาของนักพัฒนาซอฟต์แวร์ที่เป็นไปได้
DimensionDimensionการแจกแจงขนาดที่เป็นไปได้ของสเปรดชีต
DirectionDirectionการแจกแจงเส้นทางที่เป็นไปได้ที่สามารถย้ายภายในสเปรดชีตโดยใช้ลูกศร คีย์
FrequencyTypeFrequencyTypeการแจกแจงประเภทความถี่ที่เป็นไปได้
GroupControlTogglePositionGroupControlTogglePositionการแจกแจงตำแหน่งที่เปิด/ปิดการควบคุมกลุ่มได้
InterpolationTypeInterpolationTypeการแจกแจงประเภทการประมาณค่าในช่วงการไล่ระดับสีของรูปแบบตามเงื่อนไข
PivotTableSummarizeFunctionPivotTableSummarizeFunctionการแจกแจงฟังก์ชันที่อาจใช้ในการสรุปค่าในตาราง Pivot
PivotValueDisplayTypePivotValueDisplayTypeการแจงนับวิธีการที่ค่า Pivot อาจแสดง
ProtectionTypeProtectionTypeการแจงนับซึ่งแสดงส่วนต่างๆ ของสเปรดชีตที่ป้องกันการแก้ไขได้
RecalculationIntervalRecalculationIntervalการแจกแจงช่วงที่เป็นไปได้ซึ่งสามารถใช้ในการคำนวณใหม่ในสเปรดชีต
RelativeDateRelativeDateการแจงนับตัวเลือกวันที่สัมพัทธ์สำหรับการคำนวณค่าที่จะใช้ใน BooleanCriteria แบบอิงตามวันที่
SheetTypeSheetTypeการแจงนับชีตประเภทต่างๆ ที่สามารถมีอยู่ในสเปรดชีต
SortOrderSortOrderการแจงนับลำดับการจัดเรียง
TextDirectionTextDirectionการแจงนับเส้นทางของข้อความที่ถูกต้อง
TextToColumnsDelimiterTextToColumnsDelimiterการแจกแจงตัวคั่นที่กำหนดไว้ล่วงหน้าสำหรับแบ่งข้อความเป็นคอลัมน์
ThemeColorTypeThemeColorTypeการแจกแจงประเภทสีธีมที่เป็นไปได้
ValueTypeValueTypeการแจงนับประเภทค่าที่ Range.getValue() และ Range.getValues() แสดงผลจากคลาสช่วงของบริการสเปรดชีต ค่าการแจงนับ ที่ระบุไว้ด้านล่างนอกเหนือจาก Number, Boolean, Date หรือ String
WrapStrategyWrapStrategyการแจกแจงกลยุทธ์ที่ใช้ในการตัดเซลล์

เมธอด

วิธีการประเภทการแสดงผลรายละเอียดแบบย่อ
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());

พารามิเตอร์

ชื่อประเภทคำอธิบาย
nameStringชื่อของสเปรดชีต

รีเทิร์น

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

พารามิเตอร์

ชื่อประเภทคำอธิบาย
nameStringชื่อของสเปรดชีต
rowsIntegerจำนวนแถวสำหรับสเปรดชีต
columnsIntegerจำนวนคอลัมน์สำหรับสเปรดชีต

รีเทิร์น

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

พารามิเตอร์

ชื่อประเภทคำอธิบาย
fileFileไฟล์ที่จะเปิด

รีเทิร์น

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

พารามิเตอร์

ชื่อประเภทคำอธิบาย
idStringตัวระบุที่ไม่ซ้ำกันสำหรับสเปรดชีต

รีเทิร์น

Spreadsheet — ออบเจ็กต์สเปรดชีตที่มีรหัสที่ระบุ

การให้สิทธิ์

สคริปต์ที่ใช้เมธอดนี้ต้องมีการให้สิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

  • https://www.googleapis.com/auth/spreadsheets

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

พารามิเตอร์

ชื่อประเภทคำอธิบาย
urlStringURL ของสเปรดชีต

รีเทิร์น

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

พารามิเตอร์

ชื่อประเภทคำอธิบาย
rangeRangeช่วงที่จะกำหนดเป็นช่วงที่แอ็กทีฟ

รีเทิร์น

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

พารามิเตอร์

ชื่อประเภทคำอธิบาย
rangeListRangeListรายการช่วงที่จะเลือกได้

รีเทิร์น

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]);

พารามิเตอร์

ชื่อประเภทคำอธิบาย
sheetSheetแผ่นงานใหม่ที่ใช้งานอยู่

รีเทิร์น

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

พารามิเตอร์

ชื่อประเภทคำอธิบาย
sheetSheetแผ่นงานใหม่ที่ใช้งานอยู่
restoreSelectionBooleanหากเป็น 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);

พารามิเตอร์

ชื่อประเภทคำอธิบาย
newActiveSpreadsheetSpreadsheetสเปรดชีตที่จะกำหนดเป็นสเปรดชีตที่ใช้งานอยู่

setCurrentCell(cell)

ตั้งค่าเซลล์ที่ระบุเป็น current cell

หากมีเซลล์ที่ระบุในช่วงที่เลือกแล้ว ช่วงนั้นจะกลายเป็น ช่วงที่ใช้งานอยู่โดยมีเซลล์เป็นเซลล์ปัจจุบัน

หากเซลล์ที่ระบุไม่มีอยู่ในช่วงที่เลือก ระบบจะเลือกที่มีอยู่ และเซลล์นั้นจะกลายเป็นเซลล์ปัจจุบันและช่วงที่ใช้งานอยู่

หมายเหตุ:Range ที่ระบุต้องประกอบด้วย 1 เซลล์ มิเช่นนั้นจะใช้วิธีนี้ มีข้อยกเว้น

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

พารามิเตอร์

ชื่อประเภทคำอธิบาย
cellRangeเซลล์ที่จะตั้งค่าเป็นเซลล์ปัจจุบัน

รีเทิร์น

Range — เซลล์ปัจจุบันที่ตั้งค่าใหม่

การให้สิทธิ์

สคริปต์ที่ใช้เมธอดนี้ต้องมีการให้สิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets