Class DocumentTab

DocumentTab

Tab dokumen, yang berisi rich text dan elemen seperti tabel dan daftar.

Ambil tab dokumen menggunakan Document.getTabs()[tabIndex].asDocumentTab().

// Get a specific document tab based on the tab ID.
var documentTab = DocumentApp.openById('abc123456').getTab('t.0').asDocumentTab();

Metode

MetodeJenis hasil yang ditampilkanDeskripsi singkat
addBookmark(position)BookmarkMenambahkan Bookmark di Position yang ditentukan.
addFooter()FooterSectionMenambahkan bagian footer tab, jika belum ada.
addHeader()HeaderSectionMenambahkan bagian header tab, jika belum ada.
addNamedRange(name, range)NamedRangeMenambahkan NamedRange, yang merupakan Range yang memiliki nama dan ID untuk digunakan yang akan diambil nanti.
getBody()BodyMengambil Body tab.
getBookmark(id)BookmarkMendapatkan Bookmark dengan ID yang diberikan.
getBookmarks()Bookmark[]Mendapatkan semua objek Bookmark di tab.
getFooter()FooterSectionMengambil bagian footer tab, jika ada.
getFootnotes()Footnote[]Mengambil semua elemen Footnote dalam isi tab.
getHeader()HeaderSectionMengambil bagian header tab, jika ada.
getNamedRangeById(id)NamedRangeMendapatkan NamedRange dengan ID yang diberikan.
getNamedRanges()NamedRange[]Mendapatkan semua objek NamedRange di tab.
getNamedRanges(name)NamedRange[]Mendapatkan semua objek NamedRange di tab dengan nama tertentu.
newPosition(element, offset)PositionMembuat Position baru, yang merupakan referensi ke lokasi di tab, yang terkait dengan elemen tertentu.
newRange()RangeBuilderMembuat builder yang digunakan untuk membuat objek Range dari elemen tab.

Dokumentasi mendetail

addBookmark(position)

Menambahkan Bookmark di Position yang ditentukan.

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

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

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

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

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

Parameter

NamaJenisDeskripsi
positionPositionPosisi bookmark baru.

Pulang pergi

Bookmark — Bookmark baru.

Otorisasi

Skrip yang menggunakan metode ini memerlukan otorisasi dengan satu atau beberapa cakupan berikut:

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

addFooter()

Menambahkan bagian footer tab, jika belum ada.

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

// Adds a footer to the tab.
const footer = documentTab.addFooter();

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

Pulang pergi

FooterSection — Footer tab.

Otorisasi

Skrip yang menggunakan metode ini memerlukan otorisasi dengan satu atau beberapa cakupan berikut:

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

addHeader()

Menambahkan bagian header tab, jika belum ada.

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

// Adds a header to the tab.
const header = documentTab.addHeader();

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

Pulang pergi

HeaderSection — Header tab.

Otorisasi

Skrip yang menggunakan metode ini memerlukan otorisasi dengan satu atau beberapa cakupan berikut:

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

addNamedRange(name, range)

Menambahkan NamedRange, yang merupakan Range yang memiliki nama dan ID untuk digunakan yang akan diambil nanti. Nama tidak harus unik, bahkan lintas tab; beberapa rentang yang berbeda dalam dokumen yang sama dapat memiliki nama yang sama, mirip seperti kelas di HTML. Sebaliknya, ID adalah unik di dalam dokumen, seperti ID di HTML. Setelah menambahkan NamedRange, Anda tidak dapat mengubahnya, Anda hanya dapat menghapusnya.

Setiap skrip yang mengakses tab dapat mengakses NamedRange. Untuk menghindari hal yang tidak diinginkan konflik antar skrip, pertimbangkan untuk memberi awalan pada nama rentang dengan string unik.

// Creates a named range that includes every table in the tab with tab ID 't.0'.
var documentTab = DocumentApp.openById('abc123456').getTab('t.0').asDocumentTab();
var rangeBuilder = documentTab.newRange();
var tables = documentTab.getBody().getTables();
for (var i = 0; i < tables.length; i++) {
  rangeBuilder.addElement(tables[i]);
}
documentTab.addNamedRange('Tab t.0 tables', rangeBuilder.build());

Parameter

NamaJenisDeskripsi
nameStringNama untuk rentang, yang tidak harus unik; nama rentang harus yang berisi antara 1-256 karakter.
rangeRangeRentang elemen yang akan dikaitkan dengan nama; rentang dapat berupa hasil penelusuran atau dibuat secara manual dengan newRange().

Pulang pergi

NamedRangeNamedRange.

Otorisasi

Skrip yang menggunakan metode ini memerlukan otorisasi dengan satu atau beberapa cakupan berikut:

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

getBody()

Mengambil Body tab.

Tab dapat berisi jenis bagian yang berbeda (misalnya, HeaderSection, FooterSection). Bagian aktif untuk tab adalah Body.

Metode elemen dalam DocumentTab didelegasikan ke Body.

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

// Gets the tab body.
const body = documentTab.getBody();

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

Pulang pergi

Body — Bagian isi tab.

Otorisasi

Skrip yang menggunakan metode ini memerlukan otorisasi dengan satu atau beberapa cakupan berikut:

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

getBookmark(id)

Mendapatkan Bookmark dengan ID yang diberikan. Metode ini akan menampilkan null jika tidak ada Bookmark dalam tab ini.

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

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

// If the bookmark exists within the tab, 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.');
}

Parameter

NamaJenisDeskripsi
idStringID untuk Bookmark.

Pulang pergi

BookmarkBookmark dengan ID yang diberikan, atau null jika tidak ada Bookmark ada dalam tab.

Otorisasi

Skrip yang menggunakan metode ini memerlukan otorisasi dengan satu atau beberapa cakupan berikut:

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

getBookmarks()

Mendapatkan semua objek Bookmark di tab.

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

// Gets all of the bookmarks in the tab.
const bookmarks = documentTab.getBookmarks();

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

Pulang pergi

Bookmark[] — Array objek Bookmark di tab.

Otorisasi

Skrip yang menggunakan metode ini memerlukan otorisasi dengan satu atau beberapa cakupan berikut:

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

getFooter()

Mengambil bagian footer tab, jika ada.

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

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

Pulang pergi

FooterSection — Footer tab.

Otorisasi

Skrip yang menggunakan metode ini memerlukan otorisasi dengan satu atau beberapa cakupan berikut:

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

getFootnotes()

Mengambil semua elemen Footnote dalam isi tab.

Panggilan ke getFootnotes menyebabkan iterasi pada elemen tab. Untuk tab besar, menghindari panggilan yang tidak perlu ke metode ini.

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

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

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

Pulang pergi

Footnote[] — Catatan kaki tab.

Otorisasi

Skrip yang menggunakan metode ini memerlukan otorisasi dengan satu atau beberapa cakupan berikut:

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

getHeader()

Mengambil bagian header tab, jika ada.

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

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

Pulang pergi

HeaderSection — Header tab.

Otorisasi

Skrip yang menggunakan metode ini memerlukan otorisasi dengan satu atau beberapa cakupan berikut:

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

getNamedRangeById(id)

Mendapatkan NamedRange dengan ID yang diberikan. Metode ini akan menampilkan null jika tidak ada NamedRange ada dalam tab. Nama tidak harus unik, bahkan di berbagai tab; beberapa rentang yang berbeda dalam dokumen yang sama mungkin memiliki nama yang sama, mirip seperti sebuah kelas di HTML. Sebaliknya, ID bersifat unik dalam tab, seperti ID di HTML.

Parameter

NamaJenisDeskripsi
idStringID rentang yang bersifat unik dalam tab.

Pulang pergi

NamedRangeNamedRange dengan ID yang diberikan, atau null jika tidak ada rentang tersebut di tab.

Otorisasi

Skrip yang menggunakan metode ini memerlukan otorisasi dengan satu atau beberapa cakupan berikut:

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

getNamedRanges()

Mendapatkan semua objek NamedRange di tab.

NamedRange dapat diakses oleh skrip apa pun yang mengakses tab. Untuk menghindari konflik yang tidak diinginkan di antara skrip, pertimbangkan untuk memberi awalan pada nama rentang dengan string unik.

Pulang pergi

NamedRange[] — Array objek NamedRange di tab, mungkin termasuk beberapa rentang dengan nama yang sama.

Otorisasi

Skrip yang menggunakan metode ini memerlukan otorisasi dengan satu atau beberapa cakupan berikut:

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

getNamedRanges(name)

Mendapatkan semua objek NamedRange di tab dengan nama tertentu. Nama belum tentu unik, bahkan di seluruh tab; beberapa rentang yang berbeda dalam dokumen yang sama mungkin memiliki mirip dengan class di HTML. Sebaliknya, ID bersifat unik di dalam tab, seperti ID di HTML.

NamedRange dapat diakses oleh skrip apa pun yang mengakses tab. Untuk menghindari konflik yang tidak diinginkan di antara skrip, pertimbangkan untuk memberi awalan pada nama rentang dengan string unik.

Parameter

NamaJenisDeskripsi
nameStringNama rentang, yang belum tentu unik.

Pulang pergi

NamedRange[] — Array objek NamedRange di tab dengan nama tertentu.

Otorisasi

Skrip yang menggunakan metode ini memerlukan otorisasi dengan satu atau beberapa cakupan berikut:

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

newPosition(element, offset)

Membuat Position baru, yang merupakan referensi ke lokasi di tab, yang terkait dengan elemen tertentu. Kursor pengguna direpresentasikan sebagai Position, di antara penggunaan lainnya.

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

Parameter

NamaJenisDeskripsi
elementElementElemen yang berisi Position yang baru dibuat untuk; ini harus elemen Text atau elemen container seperti Paragraph.
offsetIntegerUntuk elemen Text, jumlah karakter sebelum Position; untuk elemen lainnya, jumlah elemen turunan sebelum Position dalam elemen container yang sama.

Pulang pergi

PositionPosition baru.

Otorisasi

Skrip yang menggunakan metode ini memerlukan otorisasi dengan satu atau beberapa cakupan berikut:

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

newRange()

Membuat builder yang digunakan untuk membuat objek Range dari elemen tab.

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

Pulang pergi

RangeBuilder — Builder baru.

Otorisasi

Skrip yang menggunakan metode ini memerlukan otorisasi dengan satu atau beberapa cakupan berikut:

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