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

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

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

এটি একটি উন্নত পরিষেবা যা ব্যবহারের আগে আপনাকে অবশ্যই সক্রিয় করতে হবে। কীভাবে শুরু করবেন তার ধাপে ধাপে নির্দেশাবলীর জন্য কুইকস্টার্ট অনুসরণ করুন।

রেফারেন্স

এই পরিষেবা সম্পর্কে বিস্তারিত তথ্যের জন্য, ডকস এপিআই (Docs API)-এর রেফারেন্স ডকুমেন্টেশন দেখুন। অ্যাপস স্ক্রিপ্ট (Apps Script)-এর অন্যান্য সকল অ্যাডভান্সড পরিষেবার মতো, অ্যাডভান্সড ডকস পরিষেবাটিও পাবলিক এপিআই (public API)-এর মতোই একই অবজেক্ট, মেথড এবং প্যারামিটার ব্যবহার করে। আরও তথ্যের জন্য, ‘মেথড সিগনেচার কীভাবে নির্ধারণ করা হয়’ (How method signatures are determined ) দেখুন।

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

নমুনা কোড

নিম্নলিখিত নমুনা কোডটি API-এর সংস্করণ ১ ব্যবহার করে।

ডকুমেন্ট তৈরি করুন

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

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

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

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

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

সর্বোত্তম অনুশীলন

ব্যাচ আপডেট

অ্যাডভান্সড ডক্স সার্ভিস ব্যবহার করার সময়, লুপের মধ্যে 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);