উন্নত ডক্স পরিষেবা

উন্নত দস্তাবেজ পরিষেবা আপনাকে Apps স্ক্রিপ্টে Google ডক্স API ব্যবহার করার অনুমতি দেয়৷ অনেকটা Apps Script-এর অন্তর্নির্মিত ডক্স পরিষেবার মতো, এই API স্ক্রিপ্টগুলিকে Google ডক্সে বিষয়বস্তু পড়তে, সম্পাদনা করতে এবং ফর্ম্যাট করতে দেয়৷ বেশিরভাগ ক্ষেত্রে অন্তর্নির্মিত পরিষেবাটি ব্যবহার করা সহজ, তবে এই উন্নত পরিষেবাটি কয়েকটি অতিরিক্ত বৈশিষ্ট্য সরবরাহ করে।

রেফারেন্স

এই পরিষেবার বিস্তারিত তথ্যের জন্য, ডক্স API-এর জন্য রেফারেন্স ডকুমেন্টেশন দেখুন। Apps Script-এর সমস্ত উন্নত পরিষেবাগুলির মতো, উন্নত ডক্স পরিষেবা সর্বজনীন API হিসাবে একই বস্তু, পদ্ধতি এবং পরামিতিগুলি ব্যবহার করে৷ আরও তথ্যের জন্য, দেখুন কিভাবে পদ্ধতি স্বাক্ষর নির্ধারণ করা হয়

সমস্যাগুলি রিপোর্ট করতে এবং অন্যান্য সমর্থন খুঁজতে, ডক্স API সমর্থন নির্দেশিকা দেখুন।

কোডের উদাহরণ

নীচের নমুনা কোডটি API-এর সংস্করণ 1 ব্যবহার করে।

নথি তৈরি করুন

এই নমুনা একটি নতুন নথি তৈরি করে।

advanced/docs.gs
/**
 * 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);
  }
}

পাঠ্য খুঁজুন এবং প্রতিস্থাপন করুন

এই নমুনা একটি নথিতে পাঠ্যের জোড়া খুঁজে বের করে এবং প্রতিস্থাপন করে। একটি ডাটাবেস থেকে মান সহ একটি টেমপ্লেট নথির একটি অনুলিপিতে স্থানধারক প্রতিস্থাপন করার সময় এটি কার্যকর হতে পারে।

advanced/docs.gs
/**
 * 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 কলে একাধিক অপারেশন একসাথে ব্যাচ করা উচিত।

advanced/docs.gs
/**
 * 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);
  }
}

প্রথম অনুচ্ছেদ পড়ুন

এই নমুনাটি নথিতে প্রথম অনুচ্ছেদের পাঠ্য লগ করে। ডক্স API-এ অনুচ্ছেদের কাঠামোগত প্রকৃতির কারণে, এতে একাধিক উপ-উপাদানের পাঠ্য একত্রিত করা জড়িত।

advanced/docs.gs
/**
 * 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);
  }
}

সেরা অনুশীলন

ব্যাচ আপডেট

উন্নত ডক্স পরিষেবা ব্যবহার করার সময়, একটি লুপে 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);