Class 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() แสดงผลจากคลาส Range ของบริการสเปรดชีต ค่าการแจงนับที่ระบุด้านล่างเป็นค่าที่นอกเหนือจาก 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 อยู่ หรือผู้ใช้ไม่มีสิทธิ์เข้าถึง 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 ที่ระบุต้องมีเซลล์เดียว ไม่เช่นนั้นจะยกเว้นข้อยกเว้น

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