Class Document

เอกสาร

เอกสารที่มี Rich Text และองค์ประกอบต่างๆ เช่น ตารางและรายการ

อาจเปิดหรือสร้างเอกสารโดยใช้ 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 ที่มีชื่อและรหัสสำหรับใช้เรียกข้อมูลในภายหลัง
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 ที่มีชื่อและรหัสสำหรับใช้เรียกข้อมูลในภายหลัง ชื่อไม่จำเป็นต้องซ้ำกัน ช่วงที่แตกต่างกันหลายๆ ช่วงในเอกสารเดียวกันจะใช้ชื่อเดียวกันได้เหมือนชั้นเรียนใน 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