बेहतर Docs सेवा

Docs की ऐडवांस सेवा की मदद से, Apps Script में Google Docs API का इस्तेमाल किया जा सकता है. Apps Script की Docs सेवा की तरह ही, यह एपीआई स्क्रिप्ट को Google Docs में कॉन्टेंट पढ़ने, उसमें बदलाव करने, और उसे फ़ॉर्मैट करने की अनुमति देता है. ज़्यादातर मामलों में, डिवाइस में पहले से मौजूद सेवा को इस्तेमाल करना आसान होता है. हालांकि, इस ऐडवांस सेवा में कुछ अतिरिक्त सुविधाएं मिलती हैं.

रेफ़रंस

इस सेवा के बारे में ज़्यादा जानकारी के लिए, Docs API के रेफ़रंस दस्तावेज़ देखें. Apps Script की सभी ऐडवांस सेवाओं की तरह, Docs की ऐडवांस सेवा भी सार्वजनिक एपीआई के ऑब्जेक्ट, तरीकों, और पैरामीटर का इस्तेमाल करती है. ज़्यादा जानकारी के लिए, तरीके के सिग्नेचर कैसे तय किए जाते हैं लेख पढ़ें.

समस्याओं की शिकायत करने और अन्य सहायता पाने के लिए, Docs API की सहायता गाइड देखें.

नमूना कोड

नीचे दिए गए सैंपल कोड में, एपीआई के वर्शन 1 का इस्तेमाल किया गया है.

दस्तावेज़ बनाएँ

यह सैंपल, एक नया दस्तावेज़ बनाता है.

/**
 * Create a new document.
 * @see https://developers.google.com/docs/api/reference/rest/v1/documents/create
 * @return {string} documentId
 */
function createDocument() {
  // Create document with title
  const document = Docs.Documents.create({ title: "My New Document" });
  console.log(`Created document with ID: ${document.documentId}`);
  return document.documentId;
}

टेक्स्ट ढूंढना और बदलना

यह सैंपल, किसी दस्तावेज़ के सभी टैब में टेक्स्ट के जोड़े ढूंढता है और उन्हें बदलता है. यह तब काम आ सकता है, जब आपको टेंप्लेट दस्तावेज़ की कॉपी में मौजूद प्लेसहोल्डर को डेटाबेस से मिली वैल्यू से बदलना हो.

/**
 * Performs "replace all".
 * @param {string} documentId The document to perform the replace text operations on.
 * @param {Object} findTextToReplacementMap A map from the "find text" to the "replace text".
 * @return {Object} replies
 * @see https://developers.google.com/docs/api/reference/rest/v1/documents/batchUpdate
 */
function findAndReplace(documentId, findTextToReplacementMap) {
  const requests = [];
  for (const findText in findTextToReplacementMap) {
    const replaceText = findTextToReplacementMap[findText];

    // Replace all text across all tabs.
    const replaceAllTextRequest = {
      replaceAllText: {
        containsText: {
          text: findText,
          matchCase: true,
        },
        replaceText: replaceText,
      },
    };

    // Replace all text across specific tabs.
    const _replaceAllTextWithTabsCriteria = {
      replaceAllText: {
        ...replaceAllTextRequest.replaceAllText,
        tabsCriteria: {
          tabIds: [TAB_ID_1, TAB_ID_2, TAB_ID_3],
        },
      },
    };
    requests.push(replaceAllTextRequest);
  }
  const response = Docs.Documents.batchUpdate(
    { requests: requests },
    documentId,
  );
  const replies = response.replies;
  for (const [index] of replies.entries()) {
    const numReplacements =
      replies[index].replaceAllText.occurrencesChanged || 0;
    console.log(
      "Request %s performed %s replacements.",
      index,
      numReplacements,
    );
  }
  return replies;
}

टेक्स्ट डालना और उसे स्टाइल करना

इस सैंपल में, दस्तावेज़ के पहले टैब की शुरुआत में नया टेक्स्ट डाला जाता है. साथ ही, उसे किसी खास फ़ॉन्ट और साइज़ में स्टाइल किया जाता है. ध्यान दें कि जब भी हो सके, आपको कई कार्रवाइयों को एक साथ एक ही batchUpdate कॉल में बैच करना चाहिए, ताकि बेहतर तरीके से काम किया जा सके.

/**
 * Insert text at the beginning of the first tab in the document and then style
 * the inserted text.
 * @param {string} documentId The document the text is inserted into.
 * @param {string} text The text to insert into the document.
 * @return {Object} replies
 * @see https://developers.google.com/docs/api/reference/rest/v1/documents/batchUpdate
 */
function insertAndStyleText(documentId, text) {
  const requests = [
    {
      insertText: {
        location: {
          index: 1,
          // A tab can be specified using its ID. When omitted, the request is
          // applied to the first tab.
          // tabId: TAB_ID
        },
        text: text,
      },
    },
    {
      updateTextStyle: {
        range: {
          startIndex: 1,
          endIndex: text.length + 1,
        },
        textStyle: {
          fontSize: {
            magnitude: 12,
            unit: "PT",
          },
          weightedFontFamily: {
            fontFamily: "Calibri",
          },
        },
        fields: "weightedFontFamily, fontSize",
      },
    },
  ];
  const response = Docs.Documents.batchUpdate(
    { requests: requests },
    documentId,
  );
  return response.replies;
}

पहला पैराग्राफ़ पढ़ो

यह सैंपल, दस्तावेज़ के पहले टैब के पहले पैराग्राफ़ के टेक्स्ट को लॉग करता है. Docs API में पैराग्राफ़ का स्ट्रक्चर तय होता है. इसलिए, इसमें कई सब-एलिमेंट के टेक्स्ट को एक साथ जोड़ा जाता है.

/**
 * Read the first paragraph of the first tab in a document.
 * @param {string} documentId The ID of the document to read.
 * @return {Object} paragraphText
 * @see https://developers.google.com/docs/api/reference/rest/v1/documents/get
 */
function readFirstParagraph(documentId) {
  // Get the document using document ID
  const document = Docs.Documents.get(documentId, {
    includeTabsContent: true,
  });
  const firstTab = document.tabs[0];
  const bodyElements = firstTab.documentTab.body.content;
  for (let i = 0; i < bodyElements.length; i++) {
    const structuralElement = bodyElements[i];
    // Print the first paragraph text present in document
    if (structuralElement.paragraph) {
      const paragraphElements = structuralElement.paragraph.elements;
      let paragraphText = "";

      for (let j = 0; j < paragraphElements.length; j++) {
        const paragraphElement = paragraphElements[j];
        if (paragraphElement.textRun !== null) {
          paragraphText += paragraphElement.textRun.content;
        }
      }
      console.log(paragraphText);
      return paragraphText;
    }
  }
}

सबसे सही तरीके

बैच अपडेट

Docs की ऐडवांस सेवा का इस्तेमाल करते समय, कई अनुरोधों को एक ऐरे में मिलाएं. इसके बजाय, लूप में batchUpdate को कॉल करें.

Don't — कॉल batchUpdate को लूप में न रखें.

var textToReplace = ['foo', 'bar'];
for (var i = 0; i < textToReplace.length; i++) {
  Docs.Documents.batchUpdate({
    requests: [{
      replaceAllText: ...
    }]
  }, docId);
}

करो — अपडेट की एक सीरीज़ के साथ batchUpdate को कॉल करो.

var requests = [];
var textToReplace = ['foo', 'bar'];
for (var i = 0; i < textToReplace.length; i++) {
  requests.push({ replaceAllText: ... });
}

Docs.Documents.batchUpdate({
  requests: requests
}, docId);