टैब के साथ कार्य करना

Google Docs के लिए Apps Script, आपको किसी भी प्लैटफ़ॉर्म से कॉन्टेंट ऐक्सेस करने की सुविधा देती है टैब क्लिक करें.

टैब क्या होते हैं?

Google Docs में संगठन की एक लेयर होती है, जिसे टैब कहते हैं. दस्तावेज़ उपयोगकर्ताओं को किसी दस्तावेज़ में एक या उससे ज़्यादा टैब बनाने की सुविधा देता है. आज Sheets में कई टैब मौजूद हैं. हर टैब का अपना टाइटल और एक आईडी होता है डालें). किसी टैब में चाइल्ड टैब भी हो सकते हैं, जो नेस्ट किए गए टैब होते हैं अन्य टैब के नीचे दिखेगा.

फ़िलहाल, चाइल्ड टैब के लिए एपीआई की सुविधा उपलब्ध है. हालांकि, यूज़र इंटरफ़ेस (यूआई) के लिए सहायता जल्द ही उपलब्ध होगी. आज ही अपने कोड में चाइल्ड टैब को मैनेज किया जा सकता है, ताकि यूज़र इंटरफ़ेस (यूआई) सहायता लॉन्च होने पर आपको कोड को और अपडेट करने की ज़रूरत नहीं होगी.

टैब ऐक्सेस करें

टैब प्रॉपर्टी और कॉन्टेंट को Document.getTabs() जो Tab की सूची दिखाता है. बाद के सेक्शन में, Google News की Tab क्लास; Tab क्लास का दस्तावेज़ और ज़्यादा जानकारी भी मिलती है.

टैब की प्रॉपर्टी

टैब प्रॉपर्टी को इन तरीकों का इस्तेमाल करके वापस पाया जा सकता है Tab.getId() और Tab.getTitle().

टैब का कॉन्टेंट

हर टैब में मौजूद दस्तावेज़ के कॉन्टेंट को, इसका इस्तेमाल करके वापस पाया जा सकता है Tab.asDocumentTab(). दस्तावेज़ की क्लास के स्ट्रक्चर में बदलाव सेक्शन में बताया गया है कि इसका इस्तेमाल कैसे किया जा सकता है.

टैब की हैरारकी

चाइल्ड टैब, Google Apps Script में इसके ज़रिए सार्वजनिक किए जाते हैं Tab.getChildTabs(). सभी टैब से कॉन्टेंट ऐक्सेस करने के लिए, "पेड़" को पार करना ज़रूरी है की तरह दिखता है. उदाहरण के लिए, ऐसा दस्तावेज़ देखें जिसमें टैब की हैरारकी इस तरह शामिल हो:

टैबलिस्ट यूज़र इंटरफ़ेस (यूआई) की इमेज, जिसमें टॉप-लेवल के तीन टैब हैं. इनमें से कुछ में चाइल्ड टैब भी हैं

टैब 3.1.2 को ऐक्सेस करने के लिए, ये काम किए जा सकते हैं:

// Print the ID of Tab 3.1.2.
const doc = DocumentApp.getActiveDocument();
const tab = doc.getTabs()[2].getChildTabs()[0].getChildTabs()[1];
console.log(tab.getId());

बाद के सेक्शन में, सैंपल कोड ब्लॉक देखें. सेक्शन में, जो दस्तावेज़ में सभी टैब में बार-बार होते हैं.

टैब वापस पाने के अन्य तरीके

टैब वापस पाने के दो अन्य तरीके हैं:

  • Document.getTab(tabId): दिए गए आईडी वाला टैब दिखाता है.
  • Document.getActiveTab(): यह फ़ंक्शन उपयोगकर्ता का ऐक्टिव टैब दिखाता है. केवल इसमें काम करता है स्क्रिप्ट जो किसी दस्तावेज़ से बाउंड होती हैं. कॉन्टेंट बनाने बाद के सेक्शन में इसके बारे में और विस्तार से बताया गया है.

दस्तावेज़ की क्लास के स्ट्रक्चर में बदलाव

पहले, दस्तावेज़ों में टैब का कॉन्सेप्ट नहीं होता था, इसलिए डॉक्यूमेंट क्लास दस्तावेज़ के टेक्स्ट कॉन्टेंट को सीधे ऐक्सेस करने और उसमें बदलाव करने के तरीके बताए गए हैं. इस कैटगरी में, इन तरीकों का इस्तेमाल किया जाता है:

टैब के अतिरिक्त संरचनात्मक पदानुक्रम के साथ, ये विधियां अब दस्तावेज़ में मौजूद सभी टैब के टेक्स्ट कॉन्टेंट को सिमेंटिक तौर पर दिखाएं. टेक्स्ट कॉन्टेंट को अब किसी अलग लेयर में दिखाया जाएगा; ऊपर दिए गए सभी टेक्स्ट के तरीकों को DocumentTab से ऐक्सेस किया जा सकता है.

Document क्लास के इन मौजूदा तरीकों से, कॉन्टेंट को ऐक्सेस किया जा सकेगा या उसमें बदलाव किया जा सकेगा या तो सक्रिय टैब से (स्क्रिप्ट में बाउंड से विशेष दस्तावेज़) या पहला टैब (अगर कोई सक्रिय दस्तावेज़ उपलब्ध नहीं है).

किसी खास टैब में टेक्स्ट कॉन्टेंट ऐक्सेस करना

हमारा सुझाव है कि Document के बजाय, टेक्स्ट वाले तरीकों का इस्तेमाल करें इसके बजाय, DocumentTab क्लास के अलावा अन्य तरीकों से भी उपलब्ध होते हैं (जो उपलब्ध है Tab.asDocumentTab() तरीका). उदाहरण के लिए:

// Print the text from the body of the active tab.
const doc = DocumentApp.getActiveDocument();
const documentTab = doc.getActiveTab().asDocumentTab();
const body = documentTab.getBody();
console.log(body.getText());

उपयोगकर्ता को चुनने के तरीके में किए गए बदलाव

टेक्स्ट चुनने के तरीके

Document क्लास, गैटर और सेटर की सुविधा देती है. इसकी मदद से, यह मैनेज किया जा सकता है कि टेक्स्ट में कहां पर जाना है जो ऐक्टिव दस्तावेज़ में मौजूद है. ये तरीके यहां काम करते हैं: स्क्रिप्ट चलाने वाले उपयोगकर्ता के सक्रिय टैब का संदर्भ.

  • Document.getCursor(): यह फ़ंक्शन चालू टैब में, उपयोगकर्ता के कर्सर की जगह की जानकारी दिखाता है.
  • Document.getSelection(): यह फ़ंक्शन चालू टैब में, उपयोगकर्ता की चुनी गई रेंज दिखाता है.
  • Document.setCursor(position): सक्रिय दस्तावेज़ में उपयोगकर्ता के कर्सर की स्थिति सेट करता है. अगर स्थिति तो उपयोगकर्ता का सक्रिय टैब भी संबद्ध टैब पर स्विच हो जाता है उस स्थिति के साथ.
  • Document.setSelection(range): यह विकल्प, ऐक्टिव दस्तावेज़ में उपयोगकर्ता की चुनी गई रेंज सेट करता है. अगर रेंज तो उपयोगकर्ता का सक्रिय टैब भी संबद्ध टैब पर स्विच हो जाता है रेंज में शामिल किया जा सकता है.

टैब चुनने के तरीके और इस्तेमाल के उदाहरण

शुरुआती टैब की सुविधा के साथ, स्क्रिप्ट चलाने वाला उपयोगकर्ता. ऐसा करने के लिए, नीचे दिए गए तरीकों का इस्तेमाल करें:

  • Document.getActiveTab(): यह फ़ंक्शन ऐक्टिव दस्तावेज़ में उपयोगकर्ता का ऐक्टिव Tab दिखाता है.
  • Document.setActiveTab(tabId): यह वर्तमान दस्तावेज़ में उपयोगकर्ता के चुने गए Tab को आईडी दिया गया है.

उपयोगकर्ता की ज़रूरतों के हिसाब से "चुने गए" विकल्प सक्रिय टैब के संयोजन से बना होता है साथ ही, चुनी हुई रेंज या कर्सर की मौजूदा स्थिति की जानकारी भी शामिल की जा सकती है. दोनों सक्रिय चयन के साथ काम करने के पैटर्न के लिए या तो किसी खास टैब पर जाने के लिए या उपयोगकर्ता के ऐक्टिव टैब का इस्तेमाल करें.

उपयोगकर्ता के सक्रिय टैब को स्पष्ट रूप से बदलने के लिए Document.setActiveTab(tabId). इसके अलावा, कॉल करने का Document.setCursor(position) या Document.setSelection(range) अगर किसी इनऐक्टिव टैब में Position या Range का इस्तेमाल किया गया है, तो वह टैब नया हो जाएगा सक्रिय है.

अगर स्क्रिप्ट को उपयोगकर्ता के ऐक्टिव टैब का इस्तेमाल करना है बिना कोई बदलाव किए, तो Document.setActiveTab(tabId) ज़रूरी नहीं है. कॉन्टेंट बनाने Document.getCursor() और Document.getSelection() के तरीके पहले से ही 'चालू' टैब पर काम करते रहेंगे. ऐसा उस टैब के हिसाब से होगा जिसमें उपयोगकर्ता से स्क्रिप्ट चला रहा है.

ध्यान दें कि किसी दस्तावेज़ में एक से ज़्यादा टैब चुनने या एक से ज़्यादा टैब चुनने की सुविधा नहीं है पोज़िशन या रेंज को अलग-अलग टैब पर कर सकते हैं. इसलिए, Document.setActiveTab(tabId) कर्सर की पिछली पोज़िशन या चुनी गई रेंज को हटा देगा.

किसी खास टैब के लिए पोज़िशन और रेंज के तरीके

खास टैब वह तरीका है जो Position और Range. दूसरे शब्दों में, कर्सर की पोज़िशन या चुनी गई रेंज तभी अर्थपूर्ण होते हैं, जब स्क्रिप्ट को वह विशिष्ट टैब पता हो जहां स्थान या रेंज के अंदर है.

ऐसा करने के लिए, DocumentTab.newPosition(element, offset) और DocumentTab.newRange() विधियों का उपयोग करें, जो विशिष्ट DocumentTab जिससे यह तरीका कॉल किया गया है. इसके उलट, Document.newPosition(element, offset) और Document.newRange() सक्रिय टैब (या पहले टैब, यदि स्क्रिप्ट बाउंड नहीं है.

बाद के सेक्शन में, सैंपल कोड ब्लॉक देखें. सेक्शन में, और चुने गए विकल्पों के साथ काम करते हैं.

टैब के इस्तेमाल के सामान्य पैटर्न

नीचे दिए गए कोड सैंपल, टैब के साथ इंटरैक्ट करने के अलग-अलग तरीकों के बारे में बताते हैं.

दस्तावेज़ के सभी टैब से टैब का कॉन्टेंट पढ़ें

जिस मौजूदा कोड ने टैब की सुविधा शुरू होने से पहले ऐसा किया था उसे सहायता ब्राउज़र पर माइग्रेट किया जा सकता है 'टैब ट्री' को ट्रैक करके और Tab से गेटर मेथड को कॉल करके और Document के बजाय DocumentTab. नीचे दिया गया आंशिक कोड का नमूना दिखाता है कि का इस्तेमाल करें. यह टैब ट्रैवर्सल कोड को ऐसे कई और मामलों के लिए इस्तेमाल किया जा सकता है जिनमें असल ढांचा हो सकता है.

/** Logs all text contents from all tabs in the active document. */
function logAllText() {
  // Generate a list of all the tabs in the document, including any
  // nested child tabs. DocumentApp.openById('abc123456') can also
  // be used instead of DocumentApp.getActiveDocument().
  const doc = DocumentApp.getActiveDocument();
  const allTabs = getAllTabs(doc);

  // Log the content from each tab in the document.
  for (const tab of allTabs) {
    // Get the DocumentTab from the generic Tab object.
    const documentTab = tab.asDocumentTab();
    // Get the body from the given DocumentTab.
    const body = documentTab.getBody();
    // Get the body text and log it to the console.
    console.log(body.getText());
  }
}

/**
 * Returns a flat list of all tabs in the document, in the order
 * they would appear in the UI (i.e. top-down ordering). Includes
 * all child tabs.
 */
function getAllTabs(doc) {
  const allTabs = [];
  // Iterate over all tabs and recursively add any child tabs to
  // generate a flat list of Tabs.
  for (const tab of doc.getTabs()) {
    addCurrentAndChildTabs(tab, allTabs);
  }
  return allTabs;
}

/**
 * Adds the provided tab to the list of all tabs, and recurses
 * through and adds all child tabs.
 */
function addCurrentAndChildTabs(tab, allTabs) {
  allTabs.push(tab);
  for (const childTab of tab.getChildTabs()) {
    addCurrentAndChildTabs(childTab, allTabs);
  }
}

दस्तावेज़ के पहले टैब में मौजूद टैब का कॉन्टेंट पढ़ें

यह सभी टैब को पढ़ने के समान है.

/** 
 * Logs all text contents from the first tab in the active 
 * document. 
 */
function logAllText() {
  // Generate a list of all the tabs in the document, including any
  // nested child tabs.
  const doc = DocumentApp.getActiveDocument();
  const allTabs = getAllTabs(doc);

  // Log the content from the first tab in the document.
  const firstTab = allTabs[0];
  // Get the DocumentTab from the generic Tab object.
  const documentTab = firstTab.asDocumentTab();
  // Get the body from the DocumentTab.
  const body = documentTab.getBody();
  // Get the body text and log it to the console.
  console.log(body.getText());
}

पहले टैब में जाकर, टैब का कॉन्टेंट अपडेट करें

नीचे दिया गया पार्शियल कोड सैंपल, किसी टैब को टारगेट करने का तरीका बताता है अपडेट.

/** Inserts text into the first tab of the active document. */
function insertTextInFirstTab() {
  // Get the first tab's body.
  const doc = DocumentApp.getActiveDocument();
  const firstTab = doc.getTabs()[0];
  const firstDocumentTab = firstTab.asDocumentTab();
  const firstTabBody = firstDocumentTab.getBody();

  // Append a paragraph and a page break to the first tab's body
  // section.
  firstTabBody.appendParagraph("A paragraph.");
  firstTabBody.appendPageBreak();
}

चालू या चुने गए टैब में, टैब के कॉन्टेंट अपडेट करें

नीचे दिया गया 'पार्शियल कोड सैंपल', कन्वर्ज़न बनाते समय ऐक्टिव टैब को टारगेट करने का तरीका बताता है अपडेट.

/**
 * Inserts text into the active/selected tab of the active
 * document.
 */
function insertTextInActiveTab() {
  // Get the active/selected tab's body.
  const doc = DocumentApp.getActiveDocument();
  const activeTab = doc.getActiveTab();
  const activeDocumentTab = activeTab.asDocumentTab();
  const activeTabBody = activeDocumentTab.getBody();

  // Append a paragraph and a page break to the active tab's body
  // section.
  activeTabBody.appendParagraph("A paragraph.");
  activeTabBody.appendPageBreak();
}

ऐक्टिव टैब में, कर्सर की पोज़िशन या चुनने की रेंज सेट करना

नीचे दिया गया आंशिक कोड सैंपल, कर्सर की जगह को अपडेट करने या उपयोगकर्ता के ऐक्टिव टैब के अंदर चुनी गई रेंज में भी विज्ञापन दिखाया जा सकता है. यह सिर्फ़ बाउंड में प्रासंगिक है स्क्रिप्ट.

/**
 * Changes the user's selection to select all tables within the tab
 * with the provided ID.
 */
function selectAllTables(tabId) {
  const doc = DocumentApp.getActiveDocument();
  const tab = doc.getTab(tabId);
  const documentTab = tab.asDocumentTab();

  // Build a range that encompasses all tables within the specified
  // tab.
  const rangeBuilder = documentTab.newRange();
  const tables = documentTab.getBody().getTables();
  for (let i = 0; i < tables.length; i++) {
    rangeBuilder.addElement(tables[i]);
  }
  // Set the document's selection to the tables within the specified
  // tab. Note that this actually switches the user's active tab as
  // well.
  doc.setSelection(rangeBuilder.build());
}

चालू या चुना गया टैब सेट करें

नीचे दिया गया 'पार्शियल कोड सैंपल', उपयोगकर्ता के ऐक्टिव टैब को बदलने का तरीका बताता है. यह सिर्फ़ बाउंड स्क्रिप्ट में काम का है.

/**
 * Changes the user's selected tab to the tab immediately following
 * the currently selected one. Handles child tabs.
 *
 * 

Only changes the selection if there is a tab following the * currently selected one. */ function selectNextTab() { const doc = DocumentApp.getActiveDocument(); const allTabs = getAllTabs(doc); const activeTab = doc.getActiveTab(); // Find the index of the currently active tab. let activeTabIndex = -1; for (let i = 0; i < allTabs.length; i++) { if (allTabs[i].getId() === activeTab.getId()) { activeTabIndex = i; } } // Update the user's selected tab if there is a valid next tab. const nextTabIndex = activeTabIndex + 1; if (nextTabIndex < allTabs.length) { doc.setActiveTab(allTabs[nextTabIndex].getId()); } }