Class DocumentTab

הכרטיסייה Document

כרטיסיית מסמך שמכילה טקסט עשיר ורכיבים כמו טבלאות ורשימות.

כדי לאחזר כרטיסייה של מסמך, משתמשים ב-Document.getTabs()[tabIndex].asDocumentTab().

// Get a specific document tab based on the tab ID.
// TODO(developer): Replace the IDs with your own.
const documentTab =
    DocumentApp.openById('123abc').getTab('123abc').asDocumentTab();

Methods

שיטהסוג הערך שמוחזרתיאור קצר
addBookmark(position)Bookmarkהפונקציה מוסיפה Bookmark ב-Position שצוין.
addFooter()FooterSectionמוסיף קטע של כותרת תחתונה לכרטיסייה, אם לא קיים כזה.
addHeader()HeaderSectionמוסיף קטע של כותרת לכרטיסייה, אם לא קיים כזה.
addNamedRange(name, range)NamedRangeמוסיף NamedRange, שהוא Range עם שם ומזהה לשימוש באחזור מאוחר יותר.
getBody()Bodyמאחזר את Body של הכרטיסייה.
getBookmark(id)Bookmark|nullמקבל את Bookmark עם המזהה הנתון.
getBookmarks()Bookmark[]מקבל את כל האובייקטים של Bookmark בכרטיסייה.
getFooter()FooterSection|nullמאחזר את קטע הכותרת התחתונה של הכרטיסייה, אם קיים.
getFootnotes()Footnote[]|nullהפונקציה מאחזרת את כל רכיבי Footnote בגוף הכרטיסייה.
getHeader()HeaderSection|nullמחזירה את קטע הכותרת של הכרטיסייה, אם קיים.
getNamedRangeById(id)NamedRange|nullמקבל את NamedRange עם המזהה הנתון.
getNamedRanges()NamedRange[]מקבל את כל האובייקטים של NamedRange בכרטיסייה.
getNamedRanges(name)NamedRange[]מחזירה את כל האובייקטים מסוג NamedRange בכרטיסייה עם השם שצוין.
newPosition(element, offset)Positionיוצרת אובייקט Position חדש, שהוא הפניה למיקום בכרטיסייה, ביחס לרכיב ספציפי.
newRange()RangeBuilderיוצרת כלי לבנייה שמשמש ליצירת אובייקטים מסוג Range מרכיבי כרטיסיות.

תיעוד מפורט

addBookmark(position)

הפונקציה מוסיפה Bookmark ב-Position שצוין.

// Opens the Docs file and retrieves the tab by its IDs. If you created your
// script from within a Google Docs file, you can use
// DocumentApp.getActiveDocument().getActiveTab() instead.
// TODO(developer): Replace the IDs with your own.
const documentTab =
    DocumentApp.openById('123abc').getTab('123abc').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());

פרמטרים

שםסוגתיאור
positionPositionהמיקום של הסימנייה החדשה.

חזרה

Bookmark – הסימנייה החדשה.

אישור

סקריפטים שמשתמשים בשיטה הזו דורשים הרשאה עם אחת או יותר מההיקפים הבאים:

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

addFooter()

מוסיף קטע של כותרת תחתונה לכרטיסייה, אם לא קיים כזה.

// Opens the Docs file and retrieves the tab by its IDs. If you created your
// script from within a Google Docs file, you can use
// DocumentApp.getActiveDocument().getActiveTab() instead.
// TODO(developer): Replace the IDs with your own.
const documentTab =
    DocumentApp.openById('123abc').getTab('123abc').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');

חזרה

FooterSection — הכותרת התחתונה של הכרטיסייה.

אישור

סקריפטים שמשתמשים בשיטה הזו דורשים הרשאה עם אחת או יותר מההיקפים הבאים:

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

addHeader()

מוסיף קטע של כותרת לכרטיסייה, אם לא קיים כזה.

// Opens the Docs file and retrieves the tab by its IDs. If you created your
// script from within a Google Docs file, you can use
// DocumentApp.getActiveDocument().getActiveTab() instead.
// TODO(developer): Replace the IDs with your own.
const documentTab =
    DocumentApp.openById('123abc').getTab('123abc').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');

חזרה

HeaderSection — כותרת הכרטיסייה.

אישור

סקריפטים שמשתמשים בשיטה הזו דורשים הרשאה עם אחת או יותר מההיקפים הבאים:

  • 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 a tab by its ID.
// TODO(developer): Replace the IDs with your own.
const documentTab =
    DocumentApp.openById('123abc').getTab('123abc').asDocumentTab();
const rangeBuilder = documentTab.newRange();
const tables = documentTab.getBody().getTables();
for (let i = 0; i < tables.length; i++) {
  rangeBuilder.addElement(tables[i]);
}
documentTab.addNamedRange('Tab t.0 tables', rangeBuilder.build());

פרמטרים

שםסוגתיאור
nameStringהשם של הטווח, לא חייב להיות ייחודי. שמות הטווחים צריכים להיות באורך של 1 עד 256 תווים.
rangeRangeטווח הרכיבים שאליו משויך השם. הטווח יכול להיות תוצאת חיפוש או טווח שנבנה באופן ידני באמצעות newRange().

חזרה

NamedRange — The NamedRange.

אישור

סקריפטים שמשתמשים בשיטה הזו דורשים הרשאה עם אחת או יותר מההיקפים הבאים:

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

getBody()

מאחזר את Body של הכרטיסייה.

כרטיסיות יכולות להכיל סוגים שונים של קטעים (לדוגמה, HeaderSection, FooterSection). הקטע הפעיל בכרטיסייה הוא Body.

השיטות של רכיב DocumentTab מוקצות ל-Body.

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

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

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

חזרה

Body – קטע ה-body של הכרטיסייה.

אישור

סקריפטים שמשתמשים בשיטה הזו דורשים הרשאה עם אחת או יותר מההיקפים הבאים:

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

getBookmark(id)

מקבל את Bookmark עם המזהה הנתון. הפונקציה מחזירה null אם לא קיים Bookmark כזה בכרטיסייה.

// Opens the Docs file and retrieves the tab by its IDs. If you created your
// script from within a Google Docs file, you can use
// DocumentApp.getActiveDocument().getActiveTab() instead.
// TODO(developer): Replace the IDs with your own.
const documentTab =
    DocumentApp.openById('123abc').getTab('123abc').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.');
}

פרמטרים

שםסוגתיאור
idStringהמזהה של Bookmark.

חזרה

Bookmark|null – ה-Bookmark עם המזהה הנתון, או null אם לא קיים Bookmark כזה בכרטיסייה.

אישור

סקריפטים שמשתמשים בשיטה הזו דורשים הרשאה עם אחת או יותר מההיקפים הבאים:

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

getBookmarks()

מקבל את כל האובייקטים של Bookmark בכרטיסייה.

// Opens the Docs file and retrieves the tab by its IDs. If you created your
// script from within a Google Docs file, you can use
// DocumentApp.getActiveDocument().getActiveTab() instead.
// TODO(developer): Replace the IDs with your own.
const documentTab =
    DocumentApp.openById('123abc').getTab('123abc').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);

חזרה

Bookmark[] — מערך של אובייקטים מסוג Bookmark בכרטיסייה.

אישור

סקריפטים שמשתמשים בשיטה הזו דורשים הרשאה עם אחת או יותר מההיקפים הבאים:

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

getFooter()

מאחזר את קטע הכותרת התחתונה של הכרטיסייה, אם קיים.

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

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

חזרה

FooterSection|null — הכותרת התחתונה של הכרטיסייה.

אישור

סקריפטים שמשתמשים בשיטה הזו דורשים הרשאה עם אחת או יותר מההיקפים הבאים:

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

getFootnotes()

הפונקציה מאחזרת את כל רכיבי Footnote בגוף הכרטיסייה.

קריאות ל-getFootnotes גורמות לאיטרציה על רכיבי הכרטיסייה. אם מדובר בכרטיסיות גדולות, מומלץ להימנע מקריאות מיותרות לשיטה הזו.

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

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

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

חזרה

Footnote[]|null — הערות השוליים של הכרטיסייה.

אישור

סקריפטים שמשתמשים בשיטה הזו דורשים הרשאה עם אחת או יותר מההיקפים הבאים:

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

getHeader()

מחזירה את קטע הכותרת של הכרטיסייה, אם קיים.

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

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

חזרה

HeaderSection|null — הכותרת של הכרטיסייה.

אישור

סקריפטים שמשתמשים בשיטה הזו דורשים הרשאה עם אחת או יותר מההיקפים הבאים:

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

getNamedRangeById(id)

מקבל את NamedRange עם המזהה הנתון. השיטה הזו מחזירה null אם לא קיים NamedRange בכרטיסייה. השמות לא בהכרח ייחודיים, גם לא בין כרטיסיות שונות. יכול להיות שלכמה טווחי תאים שונים באותו מסמך יהיה אותו שם, בדומה למחלקה ב-HTML. לעומת זאת, מזהים הם ייחודיים בתוך הכרטיסייה, כמו מזהה ב-HTML.

פרמטרים

שםסוגתיאור
idStringהמזהה של הטווח, שהוא ייחודי בגיליון.

חזרה

NamedRange|null – ה-NamedRange עם המזהה שצוין, או null אם לא קיים טווח כזה בכרטיסייה.

אישור

סקריפטים שמשתמשים בשיטה הזו דורשים הרשאה עם אחת או יותר מההיקפים הבאים:

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

getNamedRanges()

מקבל את כל האובייקטים של NamedRange בכרטיסייה.

כל סקריפט שיש לו גישה לכרטיסייה יכול לגשת ל-NamedRange. כדי למנוע קונפליקטים לא מכוונים בין סקריפטים, כדאי להוסיף מחרוזת ייחודית בתחילת השמות של טווחי התאים.

חזרה

NamedRange[] – מערך של אובייקטים מסוג NamedRange בכרטיסייה, שיכול לכלול כמה טווחים עם אותו שם.

אישור

סקריפטים שמשתמשים בשיטה הזו דורשים הרשאה עם אחת או יותר מההיקפים הבאים:

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

getNamedRanges(name)

מחזירה את כל האובייקטים מסוג NamedRange בכרטיסייה עם השם שצוין. השמות לא בהכרח ייחודיים, גם לא בין כרטיסיות שונות. יכול להיות שלכמה טווחי תאים שונים באותו מסמך יהיה אותו שם, בדומה למחלקה ב-HTML. לעומת זאת, מזהים הם ייחודיים בתוך הכרטיסייה, כמו מזהה ב-HTML.

כל סקריפט שיש לו גישה לכרטיסייה יכול לגשת ל-NamedRange. כדי למנוע קונפליקטים לא מכוונים בין סקריפטים, כדאי להוסיף מחרוזת ייחודית בתחילת השמות של טווחי התאים.

פרמטרים

שםסוגתיאור
nameStringהשם של הטווח, שלא חייב להיות ייחודי.

חזרה

NamedRange[] – מערך של אובייקטים מסוג NamedRange בכרטיסייה עם השם שצוין.

אישור

סקריפטים שמשתמשים בשיטה הזו דורשים הרשאה עם אחת או יותר מההיקפים הבאים:

  • 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.
// TODO(developer): Replace the IDs with your own.
const doc = DocumentApp.openById('123abc');
const documentTab = doc.getTab('123abc').asDocumentTab();
const paragraph = documentTab.getBody().appendParagraph('My new paragraph.');
const position = documentTab.newPosition(paragraph.getChild(0), 2);
doc.setCursor(position);

פרמטרים

שםסוגתיאור
elementElementהרכיב שמכיל את Position החדש שנוצר; זה חייב להיות רכיב Text או רכיב קונטיינר כמו Paragraph.
offsetIntegerעבור רכיבי Text, מספר התווים לפני Position; עבור רכיבים אחרים, מספר רכיבי הצאצא לפני Position בתוך אותו רכיב קונטיינר.

חזרה

Position — הגרסה החדשה של Position.

אישור

סקריפטים שמשתמשים בשיטה הזו דורשים הרשאה עם אחת או יותר מההיקפים הבאים:

  • 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 tab.
// TODO(developer): Replace the IDs with your own.
const doc = DocumentApp.openById('123abc');
const documentTab = doc.getTab('123abc').asDocumentTab();
const rangeBuilder = documentTab.newRange();
const tables = documentTab.getBody().getTables();
for (let i = 0; i < tables.length; i++) {
  rangeBuilder.addElement(tables[i]);
}
doc.setSelection(rangeBuilder.build());

חזרה

RangeBuilder – הכלי החדש ליצירת קהלים.

אישור

סקריפטים שמשתמשים בשיטה הזו דורשים הרשאה עם אחת או יותר מההיקפים הבאים:

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