Class Document

เอกสาร

เอกสารที่ประกอบด้วยข้อความและองค์ประกอบต่างๆ เช่น ตารางและรายการ

เอกสารอาจเปิดหรือสร้างขึ้นโดยใช้ DocumentApp

// Open a document by ID.
var doc = DocumentApp.openById("<my-id>");

// Create and open a document.
doc = DocumentApp.create("Document Title");

วิธีการ

วิธีการประเภทการแสดงผลรายละเอียดแบบย่อ
addBookmark(position)Bookmarkเพิ่ม Bookmark ที่ Position ที่ระบุ
addEditor(emailAddress)Documentเพิ่มผู้ใช้ที่ต้องการลงในรายการเครื่องมือแก้ไขของ Document
addEditor(user)Documentเพิ่มผู้ใช้ที่ต้องการลงในรายการเครื่องมือแก้ไขของ Document
addEditors(emailAddresses)Documentเพิ่มอาร์เรย์ผู้ใช้ที่ระบุลงในรายการผู้แก้ไขสําหรับ Document
addFooter()FooterSectionเพิ่มส่วนส่วนท้ายของเอกสาร (หากมี)
addHeader()HeaderSectionเพิ่มส่วนหัวของเอกสาร ถ้าไม่มี
addNamedRange(name, range)NamedRangeเพิ่ม NamedRange ซึ่งเป็น Range ที่มีชื่อและ ID ที่จะใช้สําหรับการเรียกข้อมูลในภายหลัง
addViewer(emailAddress)Documentเพิ่มผู้ใช้ที่ต้องการลงในรายการผู้ชมสําหรับ Document
addViewer(user)Documentเพิ่มผู้ใช้ที่ต้องการลงในรายการผู้ชมสําหรับ Document
addViewers(emailAddresses)Documentเพิ่มอาร์เรย์ผู้ใช้ที่ระบุลงในรายการผู้ชมสําหรับ Document
getAs(contentType)Blobเรียกเนื้อหา Document ปัจจุบันเป็น Blob ของประเภทที่ระบุ
getBlob()Blobเรียกเนื้อหา Document ในปัจจุบันเป็น Blob
getBody()Bodyเรียกข้อมูล Body ของเอกสารที่ใช้งานอยู่
getBookmark(id)Bookmarkรับ Bookmark ที่มีรหัสที่ระบุ
getBookmarks()Bookmark[]รับวัตถุทั้งหมด Bookmark รายการในเอกสาร
getCursor()Positionรับเคอร์เซอร์ของผู้ใช้ในเอกสารที่ใช้งานอยู่
getEditors()User[]รับรายชื่อผู้แก้ไขสําหรับ Document นี้
getFooter()FooterSectionเรียกส่วนส่วนท้ายของเอกสาร (หากมี)
getFootnotes()Footnote[]เรียกองค์ประกอบ Footnote ทั้งหมดในเนื้อหาของเอกสาร
getHeader()HeaderSectionเรียกส่วนหัวของเอกสาร หากมี
getId()Stringเรียกข้อมูลตัวระบุที่ไม่ซ้ํากันของเอกสาร
getLanguage()Stringรับรหัสภาษาของเอกสาร
getName()Stringเรียกข้อมูลชื่อเอกสาร
getNamedRangeById(id)NamedRangeรับ NamedRange ที่มีรหัสที่ระบุ
getNamedRanges()NamedRange[]รับวัตถุทั้งหมด NamedRange รายการในเอกสาร
getNamedRanges(name)NamedRange[]รับออบเจ็กต์ทั้งหมด NamedRange รายการในเอกสารที่มีชื่อตามที่ระบุ
getSelection()Rangeรับตัวเลือกของผู้ใช้ในเอกสารที่ใช้งานอยู่
getSupportedLanguageCodes()String[]รับรหัสภาษาทั้งหมดที่สนับสนุนในไฟล์ Google เอกสาร
getUrl()Stringเรียกข้อมูล URL เพื่อเข้าถึงเอกสารปัจจุบัน
getViewers()User[]รับรายการผู้ชมและผู้แสดงความคิดเห็นสําหรับ Document นี้
newPosition(element, offset)Positionสร้าง Position ใหม่ ซึ่งเป็นการอ้างอิงตําแหน่งในเอกสารที่สัมพันธ์กับองค์ประกอบที่เฉพาะเจาะจง
newRange()RangeBuilderสร้างเครื่องมือสร้างที่ใช้สร้างออบเจ็กต์ Range จากองค์ประกอบเอกสาร
removeEditor(emailAddress)Documentนําผู้ใช้ที่ระบุออกจากรายการผู้แก้ไขสําหรับ Document
removeEditor(user)Documentนําผู้ใช้ที่ระบุออกจากรายการผู้แก้ไขสําหรับ Document
removeViewer(emailAddress)Documentนําผู้ใช้ที่ระบุออกจากรายการผู้ชมและผู้แสดงความคิดเห็นสําหรับ Document
removeViewer(user)Documentนําผู้ใช้ที่ระบุออกจากรายการผู้ชมและผู้แสดงความคิดเห็นสําหรับ Document
saveAndClose()voidบันทึก Document ปัจจุบัน
setCursor(position)Documentตั้งค่าเคอร์เซอร์ของผู้ใช้ในเอกสารที่ใช้งานอยู่ โดยตั้งค่า Position
setLanguage(languageCode)Documentตั้งค่ารหัสภาษาของเอกสาร
setName(name)Documentตั้งชื่อเอกสาร
setSelection(range)Documentตั้งค่าการเลือกของผู้ใช้ในเอกสารที่ใช้งานอยู่ โดยตั้งค่า Range

เอกสารโดยละเอียด

addBookmark(position)

เพิ่ม Bookmark ที่ Position ที่ระบุ

// Opens the Docs file by its ID. If you created your script from within
// a Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the ID with your own.
const doc = DocumentApp.openById('abc123456');

// Gets the document body and adds a paragraph.
const paragraph = doc.getBody().appendParagraph('My new paragraph.');

// Creates a position at the first character of the paragraph text.
const position = doc.newPosition(paragraph.getChild(0), 0);

// Adds a bookmark at the first character of the paragraph text.
const bookmark = doc.addBookmark(position);

// Logs the bookmark ID to the console.
console.log(bookmark.getId());

}

พารามิเตอร์

ชื่อTypeคำอธิบาย
positionPositionตําแหน่งของบุ๊กมาร์กใหม่

ขากลับ

Bookmark — บุ๊กมาร์กใหม่

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

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

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

addEditor(emailAddress)

เพิ่มผู้ใช้ที่ต้องการลงในรายการเครื่องมือแก้ไขของ Document หากผู้ใช้อยู่ในรายการผู้ชมแล้ว วิธีนี้จะทําให้ผู้ใช้ไม่ได้อยู่ในรายการผู้ดู

พารามิเตอร์

ชื่อTypeคำอธิบาย
emailAddressStringที่อยู่อีเมลของผู้ใช้ที่จะเพิ่ม

ขากลับ

DocumentDocument นี้สําหรับเชน

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

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

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

addEditor(user)

เพิ่มผู้ใช้ที่ต้องการลงในรายการเครื่องมือแก้ไขของ Document หากผู้ใช้อยู่ในรายการผู้ชมแล้ว วิธีนี้จะทําให้ผู้ใช้ไม่ได้อยู่ในรายการผู้ดู

พารามิเตอร์

ชื่อTypeคำอธิบาย
userUserการแสดงผู้ใช้ที่จะเพิ่ม

ขากลับ

DocumentDocument นี้สําหรับเชน

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

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

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

addEditors(emailAddresses)

เพิ่มอาร์เรย์ผู้ใช้ที่ระบุลงในรายการผู้แก้ไขสําหรับ Document หากผู้ใช้คนใดคนหนึ่งอยู่ในรายการผู้ชมแล้ว วิธีนี้โปรโมตพวกเขาจากรายการผู้ชม

พารามิเตอร์

ชื่อTypeคำอธิบาย
emailAddressesString[]อาร์เรย์ของอีเมลของผู้ใช้ที่จะเพิ่ม

ขากลับ

DocumentDocument นี้สําหรับเชน

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

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

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

addFooter()

เพิ่มส่วนส่วนท้ายของเอกสาร (หากมี)

// Opens the Docs file by its ID. If you created your script from within
// a Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the ID with your own.
const doc = DocumentApp.openById('abc123456');

// Adds a footer to the document.
const footer = doc.addFooter();

// Sets the footer text to 'This is a footer.'
footer.setText('This is a footer');

ขากลับ

FooterSection — ส่วนท้ายของเอกสาร

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

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

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

addHeader()

เพิ่มส่วนหัวของเอกสาร ถ้าไม่มี

// Opens the Docs file by its ID. If you created your script from within
// a Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the ID with your own.
const doc = DocumentApp.openById('abc123456');

// Adds a header to the document.
const header = doc.addHeader();

// Sets the header text to 'This is a header.'
header.setText('This is a header');

ขากลับ

HeaderSection — ส่วนหัวของเอกสาร

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

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

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

addNamedRange(name, range)

เพิ่ม NamedRange ซึ่งเป็น Range ที่มีชื่อและ ID ที่จะใช้สําหรับการเรียกข้อมูลในภายหลัง ชื่ออาจซ้ํากันได้ ช่วงต่างๆ ในเอกสารเดียวกัน อาจแชร์ชื่อเดียวกันได้ เช่น ชั้นเรียนใน HTML ในทางตรงกันข้าม รหัสจะไม่ซ้ํากัน ภายในเอกสาร เช่น รหัสใน HTML หลังจากเพิ่ม NamedRange ลงในเอกสารแล้ว คุณจะแก้ไขไม่ได้ คุณจะนําออกได้เท่านั้น

สคริปต์ทั้งหมดที่เข้าถึงเอกสารจะเข้าถึง NamedRange ได้ เพื่อไม่ให้เกิดข้อขัดแย้งระหว่างสคริปต์โดยไม่ตั้งใจ ให้ลองเติมคํานําหน้าชื่อที่ไม่ซ้ํากันด้วยช่วงชื่อ

// Creates a named range that includes every table in the document.
var doc = DocumentApp.getActiveDocument();
var rangeBuilder = doc.newRange();
var tables = doc.getBody().getTables();
for (var i = 0; i < tables.length; i++) {
  rangeBuilder.addElement(tables[i]);
}
doc.addNamedRange('Document tables', rangeBuilder.build());

พารามิเตอร์

ชื่อTypeคำอธิบาย
nameStringชื่อของช่วงที่ไม่ต้องซ้ํา ชื่อช่วงต้องมีความยาวระหว่าง 1-256 อักขระ
rangeRangeช่วงขององค์ประกอบที่เชื่อมโยงกับชื่อ ช่วงสามารถเป็นการเลือกที่ทํางานอยู่ ผลการค้นหา หรือสร้างขึ้นด้วยตนเองกับ newRange()

ขากลับ

NamedRangeNamedRange

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

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

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

addViewer(emailAddress)

เพิ่มผู้ใช้ที่ต้องการลงในรายการผู้ชมสําหรับ Document หากผู้ใช้อยู่ในรายชื่อผู้แก้ไข แล้ว เมธอดนี้จะไม่ส่งผลใดๆ

พารามิเตอร์

ชื่อTypeคำอธิบาย
emailAddressStringที่อยู่อีเมลของผู้ใช้ที่จะเพิ่ม

ขากลับ

DocumentDocument นี้สําหรับเชน

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

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

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

addViewer(user)

เพิ่มผู้ใช้ที่ต้องการลงในรายการผู้ชมสําหรับ Document หากผู้ใช้อยู่ในรายชื่อผู้แก้ไข แล้ว เมธอดนี้จะไม่ส่งผลใดๆ

พารามิเตอร์

ชื่อTypeคำอธิบาย
userUserการแสดงผู้ใช้ที่จะเพิ่ม

ขากลับ

DocumentDocument นี้สําหรับเชน

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

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

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

addViewers(emailAddresses)

เพิ่มอาร์เรย์ผู้ใช้ที่ระบุลงในรายการผู้ชมสําหรับ Document หากผู้ใช้คนใดคนหนึ่งอยู่ในรายการผู้แก้ไขอยู่แล้ว วิธีนี้จะไม่มีผลใดๆ

พารามิเตอร์

ชื่อTypeคำอธิบาย
emailAddressesString[]อาร์เรย์ของอีเมลของผู้ใช้ที่จะเพิ่ม

ขากลับ

DocumentDocument นี้สําหรับเชน

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

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

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

getAs(contentType)

เรียกเนื้อหา Document ปัจจุบันเป็น Blob ของประเภทที่ระบุ

// Opens the Docs file by its ID. If you created your script from within
// a Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the ID with your own.
const doc = DocumentApp.openById('abc123456');

// Gets the document as a PDF.
const pdf = doc.getAs('application/pdf');

// Logs the name of the PDF to the console.
console.log(pdf.getName());

พารามิเตอร์

ชื่อTypeคำอธิบาย
contentTypeStringประเภท MIME ที่จะแปลงเป็น ปัจจุบันรองรับเฉพาะ 'application/pdf'

ขากลับ

Blob — เอกสารปัจจุบันในรูปแบบ Blob


getBlob()

เรียกเนื้อหา Document ในปัจจุบันเป็น Blob

// Opens the Docs file by its ID. If you created your script from within
// a Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the ID with your own.
const doc = DocumentApp.openById('abc123456');

// Retrieves the current document's contents as a blob and logs it to the console.
console.log(doc.getBlob().getContentType());

ขากลับ

Blob — เอกสารปัจจุบันในรูปแบบ Blob


getBody()

เรียกข้อมูล Body ของเอกสารที่ใช้งานอยู่

เอกสารอาจมีประเภทต่างๆ ส่วน (เช่น HeaderSection, FooterSection) ส่วนที่ใช้งานของเอกสารคือ Body

เมธอดองค์ประกอบใน Document จะแทน Body ที่ทํางานอยู่

// Opens the Docs file by its ID. If you created your script from within
// a Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the ID with your own.
const doc = DocumentApp.openById('abc123456');

// Gets the document body.
const body = doc.getBody();

// Gets the body text and logs it to the console.
console.log(body.getText());

ขากลับ

Body — ส่วนเนื้อหาของเอกสารที่ใช้งานอยู่

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

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

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

getBookmark(id)

รับ Bookmark ที่มีรหัสที่ระบุ วิธีนี้จะแสดงผล null หากไม่มี Bookmark ดังกล่าว

// Opens the Docs file by its ID. If you created your script from within
// a Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the ID with your own.
const doc = DocumentApp.openById('abc123456');

// Gets the bookmark by its ID.
const bookmark = doc.getBookmark('id.xyz654321');

// If the bookmark exists, logs the character offset of its position to the console.
// otherwise, logs 'No bookmark exists with the given ID.' to the console.
if (bookmark) {
  console.log(bookmark.getPosition().getOffset());
} else {
  console.log('No bookmark exists with the given ID.');
}

พารามิเตอร์

ชื่อTypeคำอธิบาย
idStringรหัสของBookmark

ขากลับ

BookmarkBookmark ที่มีรหัสที่ระบุ หรือ null หากไม่มี Bookmark

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

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

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

getBookmarks()

รับวัตถุทั้งหมด Bookmark รายการในเอกสาร

// Opens the Docs file by its ID. If you created your script from within
// a Google Docs file, you can use DocumentApp.getActiveDocument() instead.
const doc = DocumentApp.openById('abc123456');

// Gets all of the bookmarks in the document.
const bookmarks = doc.getBookmarks();

// Logs the number of bookmarks in the document to the console.
console.log(bookmarks.length);

ขากลับ

Bookmark[] — อาร์เรย์ของออบเจ็กต์ Bookmark ในเอกสาร

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

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

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

getCursor()

รับเคอร์เซอร์ของผู้ใช้ในเอกสารที่ใช้งานอยู่ สคริปต์จะเข้าถึงเคอร์เซอร์ของผู้ใช้ที่กําลังเรียกใช้สคริปต์ และสคริปต์เข้าถึงเอกสารเท่านั้น

// Insert some text at the cursor position and make it bold.
var cursor = DocumentApp.getActiveDocument().getCursor();
if (cursor) {
  // Attempt to insert text at the cursor position. If the insertion returns null, the cursor's
  // containing element doesn't allow insertions, so show the user an error message.
  var element = cursor.insertText('ಠ‿ಠ');
  if (element) {
    element.setBold(true);
  } else {
    DocumentApp.getUi().alert('Cannot insert text here.');
  }
} else {
  DocumentApp.getUi().alert('Cannot find a cursor.');
}

ขากลับ

Position — การนําเสนอเคอร์เซอร์ของผู้ใช้ หรือ null หากผู้ใช้ไม่ได้วางเคอร์เซอร์ในเอกสารหรือสคริปต์ไม่ได้เชื่อมโยงกับเอกสาร

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

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

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

getEditors()

รับรายชื่อผู้แก้ไขสําหรับ Document นี้

ขากลับ

User[] — อาร์เรย์ของผู้ใช้ที่มีสิทธิ์แก้ไข

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

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

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

getFooter()

เรียกส่วนส่วนท้ายของเอกสาร (หากมี)

// Opens the Docs file by its ID. If you created your script from within
// a Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the ID with your own.
const doc = DocumentApp.openById('abc123456');

// Gets the text of the document's footer and logs it to the console.
console.log(doc.getFooter().getText());

ขากลับ

FooterSection — ส่วนท้ายของเอกสาร

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

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

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

getFootnotes()

เรียกองค์ประกอบ Footnote ทั้งหมดในเนื้อหาของเอกสาร

การเรียกไปยัง getFootnotes จะทําให้เกิดการทําซ้ําในองค์ประกอบของเอกสาร สําหรับเอกสารขนาดใหญ่ โปรดหลีกเลี่ยงการเรียกวิธีนี้ที่ไม่จําเป็น

// Opens the Docs file by its ID. If you created your script from within
// a Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the ID with your own.
const doc = DocumentApp.openById('abc123456');

// Gets the first footnote.
const footnote = doc.getFootnotes()[0];

// Logs footnote contents to the console.
console.log(footnote.getFootnoteContents().getText());

ขากลับ

Footnote[] — เชิงอรรถของเอกสาร

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

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

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

getHeader()

เรียกส่วนหัวของเอกสาร หากมี

// Opens the Docs file by its ID. If you created your script from within
// a Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the ID with your own.
const doc = DocumentApp.openById('abc123456');

// Gets the text of the document's header and logs it to the console.
console.log(doc.getHeader().getText());

ขากลับ

HeaderSection — ส่วนหัวของเอกสาร

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

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

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

getId()

เรียกข้อมูลตัวระบุที่ไม่ซ้ํากันของเอกสาร ระบบจะใช้รหัสเอกสารกับ DocumentApp.openById() เพื่อเปิดอินสแตนซ์เอกสารที่ต้องการ

ขากลับ

String — รหัสของเอกสาร

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

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

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

getLanguage()

รับรหัสภาษาของเอกสาร นี่คือภาษาที่แสดงในไฟล์ > ภาษาของเครื่องมือแก้ไขเอกสาร ซึ่งอาจไม่ใช่ภาษาจริงที่มีในเอกสารนี้

ขากลับ

String — ภาษาของเอกสาร หรือ null หากไม่ได้กําหนดไว้

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

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

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

getName()

เรียกข้อมูลชื่อเอกสาร

ขากลับ

String — ชื่อเอกสาร

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

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

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

getNamedRangeById(id)

รับ NamedRange ที่มีรหัสที่ระบุ วิธีนี้จะแสดงผล null หากไม่มี NamedRange ดังกล่าว ชื่ออาจซ้ํากันได้ ช่วงต่างๆ ในเอกสารเดียวกัน อาจใช้ชื่อเดียวกัน เช่นเดียวกับคลาส HTML ในทางตรงกันข้าม รหัสจะไม่ซ้ํากัน ภายในเอกสาร เช่น รหัสใน HTML

พารามิเตอร์

ชื่อTypeคำอธิบาย
idStringรหัสของช่วง ซึ่งไม่ซ้ํากันภายในเอกสาร

ขากลับ

NamedRangeNamedRange ที่มีรหัสที่กําหนด หรือ null หากไม่มีช่วงดังกล่าว

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

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

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

getNamedRanges()

รับวัตถุทั้งหมด NamedRange รายการในเอกสาร

สคริปต์ใดก็ตามที่เข้าถึงเอกสารได้สามารถเข้าถึง NamedRange หากต้องการหลีกเลี่ยง ความขัดแย้งที่ไม่ได้ตั้งใจระหว่างสคริปต์ต่างๆ ให้ใช้คํานําหน้าที่ไม่ซ้ํากันในชื่อสตริงที่ไม่ซ้ํากัน

ขากลับ

NamedRange[] — อาร์เรย์ของออบเจ็กต์ NamedRange ในเอกสาร โดยอาจมีหลายช่วงที่มีชื่อเดียวกัน

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

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

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

getNamedRanges(name)

รับออบเจ็กต์ทั้งหมด NamedRange รายการในเอกสารที่มีชื่อตามที่ระบุ ชื่อไม่จําเป็นต้องซ้ํากัน ช่วงที่ต่างกันหลายช่วงในเอกสารเดียวกันอาจใช้ชื่อเดียวกัน เช่น ชั้นเรียนใน HTML ในทางตรงกันข้าม รหัสภายในเอกสารจะไม่ซ้ํากัน เช่น รหัสใน HTML

สคริปต์ใดก็ตามที่เข้าถึงเอกสารได้สามารถเข้าถึง NamedRange หากต้องการหลีกเลี่ยง ความขัดแย้งที่ไม่ได้ตั้งใจระหว่างสคริปต์ต่างๆ ให้ใช้คํานําหน้าที่ไม่ซ้ํากันในชื่อสตริงที่ไม่ซ้ํากัน

พารามิเตอร์

ชื่อTypeคำอธิบาย
nameStringชื่อของช่วง ซึ่งไม่จําเป็นต้องเหมือนกัน

ขากลับ

NamedRange[] — อาร์เรย์ของออบเจ็กต์ NamedRange ในเอกสารที่มีชื่อหนึ่งๆ

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

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

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

getSelection()

รับตัวเลือกของผู้ใช้ในเอกสารที่ใช้งานอยู่ สคริปต์สามารถเข้าถึงการเลือกของผู้ใช้ที่กําลังเรียกใช้สคริปต์ และเมื่อสคริปต์เชื่อมโยงกับเอกสารเท่านั้น

// Display a dialog box that tells the user how many elements are included in the selection.
var selection = DocumentApp.getActiveDocument().getSelection();
if (selection) {
  var elements = selection.getRangeElements();
  DocumentApp.getUi().alert('Number of selected elements: ' + elements.length);
} else {
  DocumentApp.getUi().alert('Nothing is selected.');
}

ขากลับ

Range — การนําเสนอการเลือกของผู้ใช้ หรือ null หากผู้ใช้ไม่ได้เลือกรายการใดไว้ในเอกสาร หากเลือกเฉพาะตอนท้ายของย่อหน้า โดยเลือกเฉพาะท้ายย่อหน้าและบรรทัดใหม่ หรือหากสคริปต์ไม่ได้เชื่อมโยงกับเอกสาร

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

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

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

getSupportedLanguageCodes()

รับรหัสภาษาทั้งหมดที่สนับสนุนในไฟล์ Google เอกสาร

ขากลับ

String[] — อาร์เรย์ของรหัสภาษา

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

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

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

getUrl()

เรียกข้อมูล URL เพื่อเข้าถึงเอกสารปัจจุบัน

var doc = DocumentApp.getActiveDocument();

// Send out the link to open the document.
MailApp.sendEmail("<email-address>", doc.getName(), doc.getUrl());

ขากลับ

String — URL เพื่อเข้าถึงเอกสารปัจจุบัน

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

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

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

getViewers()

รับรายการผู้ชมและผู้แสดงความคิดเห็นสําหรับ Document นี้

ขากลับ

User[] — อาร์เรย์ของผู้ใช้ที่มีสิทธิ์ดูหรือแสดงความคิดเห็น

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

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

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

newPosition(element, offset)

สร้าง Position ใหม่ ซึ่งเป็นการอ้างอิงตําแหน่งในเอกสารที่สัมพันธ์กับองค์ประกอบที่เฉพาะเจาะจง เคอร์เซอร์ของผู้ใช้จะแสดงเป็น Position รวมถึงการใช้งานอื่นๆ ด้วย

// Append a paragraph, then place the user's cursor after the first word of the new paragraph.
var doc = DocumentApp.getActiveDocument();
var paragraph = doc.getBody().appendParagraph('My new paragraph.');
var position = doc.newPosition(paragraph.getChild(0), 2);
doc.setCursor(position);

พารามิเตอร์

ชื่อTypeคำอธิบาย
elementElementองค์ประกอบที่จะมี Position ใหม่ โดยต้องเป็นองค์ประกอบ Text หรือองค์ประกอบคอนเทนเนอร์อย่าง Paragraph
offsetIntegerสําหรับองค์ประกอบ Text จํานวนอักขระก่อน Position สําหรับองค์ประกอบอื่นๆ จํานวนขององค์ประกอบย่อยที่อยู่ก่อน Position ภายในองค์ประกอบคอนเทนเนอร์เดียวกัน

ขากลับ

PositionPosition ใหม่

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

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

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

newRange()

สร้างเครื่องมือสร้างที่ใช้สร้างออบเจ็กต์ Range จากองค์ประกอบเอกสาร

// Change the user's selection to a range that includes every table in the document.
var doc = DocumentApp.getActiveDocument();
var rangeBuilder = doc.newRange();
var tables = doc.getBody().getTables();
for (var i = 0; i < tables.length; i++) {
  rangeBuilder.addElement(tables[i]);
}
doc.setSelection(rangeBuilder.build());

ขากลับ

RangeBuilder — เครื่องมือสร้างใหม่

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

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

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

removeEditor(emailAddress)

นําผู้ใช้ที่ระบุออกจากรายการผู้แก้ไขสําหรับ Document วิธีการนี้จะไม่บล็อกผู้ใช้ไม่ให้เข้าถึง Document หากผู้ใช้เป็นสมาชิกในชั้นเรียนที่มีสิทธิ์เข้าถึงทั่วไป เช่น หากมีการแชร์ Document กับทั้งโดเมนของผู้ใช้ หรือหาก Document อยู่ในไดรฟ์ที่แชร์ที่ผู้ใช้เข้าถึงได้

สําหรับไฟล์ในไดรฟ์ การดําเนินการนี้จะนําผู้ใช้ออกจากรายชื่อผู้ดูด้วย

พารามิเตอร์

ชื่อTypeคำอธิบาย
emailAddressStringที่อยู่อีเมลของผู้ใช้ที่จะนําออก

ขากลับ

DocumentDocument นี้สําหรับเชน

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

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

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

removeEditor(user)

นําผู้ใช้ที่ระบุออกจากรายการผู้แก้ไขสําหรับ Document วิธีการนี้จะไม่บล็อกผู้ใช้ไม่ให้เข้าถึง Document หากผู้ใช้เป็นสมาชิกในชั้นเรียนที่มีสิทธิ์เข้าถึงทั่วไป เช่น หากมีการแชร์ Document กับทั้งโดเมนของผู้ใช้ หรือหาก Document อยู่ในไดรฟ์ที่แชร์ที่ผู้ใช้เข้าถึงได้

สําหรับไฟล์ในไดรฟ์ การดําเนินการนี้จะนําผู้ใช้ออกจากรายชื่อผู้ดูด้วย

พารามิเตอร์

ชื่อTypeคำอธิบาย
userUserการแสดงผู้ใช้ที่จะนําออก

ขากลับ

DocumentDocument นี้สําหรับเชน

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

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

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

removeViewer(emailAddress)

นําผู้ใช้ที่ระบุออกจากรายการผู้ชมและผู้แสดงความคิดเห็นสําหรับ Document วิธีการนี้จะไม่มีผลหากผู้ใช้เป็นผู้แก้ไข ไม่ใช่ผู้ดูหรือผู้แสดงความคิดเห็น วิธีการนี้จะไม่บล็อกผู้ใช้ไม่ให้เข้าถึง Document หากผู้ใช้เป็นสมาชิกในชั้นเรียนที่มีสิทธิ์เข้าถึงทั่วไป เช่น หากมีการแชร์ Document กับทั้งโดเมนของผู้ใช้ หรือหาก Document อยู่ในไดรฟ์ที่แชร์ที่ผู้ใช้เข้าถึงได้

สําหรับไฟล์ในไดรฟ์ การดําเนินการนี้จะนําผู้ใช้ออกจากรายชื่อผู้แก้ไขด้วย

พารามิเตอร์

ชื่อTypeคำอธิบาย
emailAddressStringที่อยู่อีเมลของผู้ใช้ที่จะนําออก

ขากลับ

DocumentDocument สําหรับการเชน

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

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

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

removeViewer(user)

นําผู้ใช้ที่ระบุออกจากรายการผู้ชมและผู้แสดงความคิดเห็นสําหรับ Document วิธีการนี้จะไม่มีผลหากผู้ใช้เป็นผู้แก้ไข ไม่ใช่ผู้ดู วิธีการนี้จะไม่บล็อกผู้ใช้ไม่ให้เข้าถึง Document หากผู้ใช้เป็นสมาชิกในชั้นเรียนที่มีสิทธิ์เข้าถึงทั่วไป เช่น หากมีการแชร์ Document กับทั้งโดเมนของผู้ใช้ หรือหาก Document อยู่ในไดรฟ์ที่แชร์ที่ผู้ใช้เข้าถึงได้

สําหรับไฟล์ในไดรฟ์ การดําเนินการนี้จะนําผู้ใช้ออกจากรายชื่อผู้แก้ไขด้วย

พารามิเตอร์

ชื่อTypeคำอธิบาย
userUserการแสดงผู้ใช้ที่จะนําออก

ขากลับ

DocumentDocument สําหรับการเชน

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

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

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

saveAndClose()

บันทึก Document ปัจจุบัน ทําให้ระบบล้างและนําการอัปเดตที่รอดําเนินการออก

มีการเรียกใช้เมธอด saveAndClose() โดยอัตโนมัติในตอนท้ายของการเรียกใช้สคริปต์สําหรับ Document ที่แก้ไขได้แต่ละรายการที่เปิดอยู่

ไม่สามารถแก้ไข Document ที่ปิดไปแล้วได้ ใช้ DocumentApp.openById() เพื่อเปิดเอกสาร ที่มีการแก้ไขอีกครั้ง

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

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

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

setCursor(position)

ตั้งค่าเคอร์เซอร์ของผู้ใช้ในเอกสารที่ใช้งานอยู่ โดยตั้งค่า Position สคริปต์สามารถเข้าถึง เคอร์เซอร์ของผู้ใช้ที่เรียกใช้สคริปต์ และเฉพาะเมื่อมีการเชื่อมโยงเอกสารเท่านั้น

// Append a paragraph, then place the user's cursor after the first word of the new paragraph.
var doc = DocumentApp.getActiveDocument();
var paragraph = doc.getBody().appendParagraph('My new paragraph.');
var position = doc.newPosition(paragraph.getChild(0), 2);
doc.setCursor(position);

พารามิเตอร์

ชื่อTypeคำอธิบาย
positionPositionตําแหน่งเคอร์เซอร์ใหม่

ขากลับ

DocumentDocument นี้ สําหรับเชน

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

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

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

setLanguage(languageCode)

ตั้งค่ารหัสภาษาของเอกสาร นี่คือภาษาที่แสดงในไฟล์ > ภาษาของเครื่องมือแก้ไขเอกสาร ซึ่งอาจไม่ใช่ภาษาจริงที่มีในเอกสารนี้ ใช้ getSupportedLanguageCodes() เพื่อรับรหัสภาษาที่ถูกต้องทั้งหมด

พารามิเตอร์

ชื่อTypeคำอธิบาย
languageCodeStringรหัสภาษา

ขากลับ

DocumentDocument นี้สําหรับเชน

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

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

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

setName(name)

ตั้งชื่อเอกสาร

พารามิเตอร์

ชื่อTypeคำอธิบาย
nameStringชื่อเอกสารใหม่

ขากลับ

Document — เอกสารปัจจุบัน

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

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

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

setSelection(range)

ตั้งค่าการเลือกของผู้ใช้ในเอกสารที่ใช้งานอยู่ โดยตั้งค่า Range สคริปต์จะเข้าถึงได้เฉพาะผู้ใช้ที่เรียกใช้สคริปต์นั้น และเฉพาะในกรณีที่สคริปต์มีสิทธิ์เข้าถึงเอกสารเท่านั้น

// Change the user's selection to a range that includes every table in the document.
var doc = DocumentApp.getActiveDocument();
var rangeBuilder = doc.newRange();
var tables = doc.getBody().getTables();
for (var i = 0; i < tables.length; i++) {
  rangeBuilder.addElement(tables[i]);
}
doc.setSelection(rangeBuilder.build());

พารามิเตอร์

ชื่อTypeคำอธิบาย
rangeRangeช่วงใหม่สําหรับเลือกองค์ประกอบ

ขากลับ

DocumentDocument นี้ สําหรับเชน

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

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

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