बेहतर 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() {
  try {
    // Create document with title
    const document = Docs.Documents.create({'title': 'My New Document'});
    console.log('Created document with ID: ' + document.documentId);
    return document.documentId;
  } catch (e) {
    // TODO (developer) - Handle exception
    console.log('Failed with error %s', e.message);
  }
}

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

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

/**
 * 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];
    const request = {
      replaceAllText: {
        containsText: {
          text: findText,
          matchCase: true
        },
        replaceText: replaceText
      }
    };
    requests.push(request);
  }
  try {
    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;
  } catch (e) {
    // TODO (developer) - Handle exception
    console.log('Failed with error : %s', e.message);
  }
}

टेक्स्ट शामिल करना और उसे स्टाइल देना

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

/**
 * Insert text at the beginning of 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
      },
      text: text
    }
  },
  {
    updateTextStyle: {
      range: {
        startIndex: 1,
        endIndex: text.length + 1
      },
      textStyle: {
        fontSize: {
          magnitude: 12,
          unit: 'PT'
        },
        weightedFontFamily: {
          fontFamily: 'Calibri'
        }
      },
      fields: 'weightedFontFamily, fontSize'
    }
  }];
  try {
    const response =Docs.Documents.batchUpdate({'requests': requests}, documentId);
    return response.replies;
  } catch (e) {
    // TODO (developer) - Handle exception
    console.log('Failed with an error %s', e.message);
  }
}

पहला पैराग्राफ़ पढ़ें

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

/**
 * Read the first paragraph of the body of 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) {
  try {
    // Get the document using document ID
    const document = Docs.Documents.get(documentId);
    const bodyElements = document.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;
      }
    }
  } catch (e) {
    // TODO (developer) - Handle exception
    console.log('Failed with error %s', e.message);
  }
}

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

बैच अपडेट

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

ऐसा न करें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);