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

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

তথ্যসূত্র

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

সমস্যাগুলি রিপোর্ট করতে এবং অন্যান্য সহায়তা পেতে, ডক্স এপিআই সহায়তা নির্দেশিকা দেখুন।

নমুনা কোড

নীচের নমুনা কোডটি 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];
    // One option for replacing all text is to specify all tab IDs.
    const request = {
      replaceAllText: {
        containsText: {
          text: findText,
          matchCase: true
        },
        replaceText: replaceText,
        tabsCriteria: {
          tabIds: [TAB_ID_1, TAB_ID_2, TAB_ID_3],
        }
      }
    };
    // Another option is to omit TabsCriteria if you are replacing across all tabs.
    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 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'
    }
  }];
  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);
  }
}

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

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

উন্নত/ডক্স.জিএস
/**
 * 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) {
  try {
    // 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;
      }
    }
  } 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);