Google Docs, Sheets, और Slides से टेक्स्ट का अनुवाद करना

कोडिंग का लेवल: बीच का लेवल
अवधि: 30 मिनट
प्रोजेक्ट टाइप: Google Workspace ऐड-ऑन

मकसद

  • समझें कि समाधान से क्या होता है.
  • जानें कि इस समाधान में Apps Script सेवाएं क्या करती हैं.
  • एनवायरमेंट सेट अप करें.
  • स्क्रिप्ट सेट अप करें.
  • स्क्रिप्ट चलाएं.

इस प्लैटफ़ॉर्म के बारे में जानकारी

इस तरीके की मदद से, Google Docs, Sheets, और Slides में मौजूद टेक्स्ट का अनुवाद आसानी से किया जा सकता है.

Translate Google Workspace ऐड-ऑन का स्क्रीनशॉट

यह कैसे काम करता है

Docs, Sheets या Slides में टेक्स्ट चुनने और ऐड-ऑन में सिलेक्शन पाएं पर क्लिक करने पर, स्क्रिप्ट टेक्स्ट को ऐड-ऑन में कॉपी करती है, उसका अनुवाद करती है, और अनुवाद किया गया टेक्स्ट दिखाती है.

डिफ़ॉल्ट रूप से, स्क्रिप्ट सोर्स भाषा का पता लगाती है और टेक्स्ट का अनुवाद अंग्रेज़ी में करती है. सोर्स और टारगेट भाषाओं में बदलाव किया जा सकता है.

Apps Script की सेवाएं

यह समाधान इन सेवाओं का इस्तेमाल करता है:

  • कार्ड सेवा–ऐड-ऑन का यूज़र इंटरफ़ेस बनाता है.
  • भाषा सेवा–Google Translate का इस्तेमाल करके टेक्स्ट का अनुवाद करता है.

ज़रूरी शर्तें

इस सैंपल का इस्तेमाल करने के लिए, आपको यहां दी गई शर्तें पूरी करनी होंगी:

  • Google खाता (Google Workspace खातों के लिए एडमिन की अनुमति ज़रूरी हो सकती है).
  • इंटरनेट ऐक्सेस करने वाला वेब ब्राउज़र.

  • Google Cloud प्रोजेक्ट.

अपना एनवायरमेंट सेट अप करें

Google Cloud कंसोल में अपना Cloud प्रोजेक्ट खोलना

अगर यह पहले से नहीं खुला है, तो वह Cloud प्रोजेक्ट खोलें जिसका इस्तेमाल आपको इस सैंपल के लिए करना है:

  1. Google Cloud Console में, प्रोजेक्ट चुनें पेज पर जाएं.

    कोई क्लाउड प्रोजेक्ट चुनें

  2. वह Google Cloud प्रोजेक्ट चुनें जिसका आपको इस्तेमाल करना है. इसके अलावा, प्रोजेक्ट बनाएं पर क्लिक करके, स्क्रीन पर दिए गए निर्देशों का पालन करें. अगर आपने Google Cloud प्रोजेक्ट बनाया है, तो आपको प्रोजेक्ट के लिए बिलिंग की सुविधा चालू करनी पड़ सकती है.

Google Workspace ऐड-ऑन के लिए, ऐसी स्क्रीन कॉन्फ़िगर करना ज़रूरी है जिसके लिए सहमति दी गई हो. अपने ऐड-ऑन की OAuth सहमति वाली स्क्रीन को कॉन्फ़िगर करने से यह तय होता है कि Google, उपयोगकर्ताओं को क्या दिखाएगा.

  1. Google Cloud Console में, मेन्यू > एपीआई और सेवाएं > OAuth की सहमति वाली स्क्रीन पर जाएं.

    ऐसी स्क्रीन पर जाएं जहां OAuth के लिए सहमति दी जाती है

  2. अपने ऐप्लिकेशन के लिए, उपयोगकर्ता का टाइप चुनें. इसके बाद, बनाएं पर क्लिक करें.
  3. ऐप्लिकेशन रजिस्ट्रेशन फ़ॉर्म भरें. इसके बाद, सेव करें और जारी रखें पर क्लिक करें.
  4. फ़िलहाल, दायरे को न जोड़कर, सेव करें और जारी रखें पर क्लिक करें. आने वाले समय में, जब आपको अपने संगठन के Google Workspace से बाहर इस्तेमाल करने के लिए कोई ऐप्लिकेशन बनाना होगा, तो आपको अपने ऐप्लिकेशन के लिए ज़रूरी अनुमति वाले स्कोप जोड़ने होंगे और उनकी पुष्टि करनी होगी.

  5. अगर आपने उपयोगकर्ता टाइप के लिए बाहरी उपयोगकर्ता को चुना है, तो टेस्ट उपयोगकर्ता जोड़ें:
    1. टेस्ट उपयोगकर्ता में जाकर, उपयोगकर्ता जोड़ें पर क्लिक करें.
    2. अपना ईमेल पता और जांच के लिए अनुमति पा चुके किसी अन्य उपयोगकर्ता का नाम डालें. इसके बाद, सेव करें और जारी रखें पर क्लिक करें.
  6. अपने ऐप्लिकेशन रजिस्ट्रेशन की खास जानकारी की समीक्षा करें. बदलाव करने के लिए, बदलाव करें पर क्लिक करें. अगर ऐप्लिकेशन रजिस्ट्रेशन ठीक लगता है, तो डैशबोर्ड पर वापस पर क्लिक करें.

स्क्रिप्ट सेट अप करें

Apps Script प्रोजेक्ट बनाना

  1. अनुवाद करें Apps Script प्रोजेक्ट को खोलने के लिए, नीचे दिए गए बटन पर क्लिक करें.
    प्रोजेक्ट खोलें

  2. खास जानकारी पर क्लिक करें.

  3. खास जानकारी देने वाले पेज पर, कॉपी बनाएं कॉपी बनाने के लिए आइकॉन पर क्लिक करें.

Cloud प्रोजेक्ट नंबर कॉपी करें

  1. Google Cloud Console में, अपने Google Cloud प्रोजेक्ट पर जाएं.
  2. सेटिंग और सुविधाएं > प्रोजेक्ट सेटिंग पर क्लिक करें.
  3. प्रोजेक्ट नंबर कॉपी करें.

Apps Script प्रोजेक्ट का क्लाउड प्रोजेक्ट सेट करना

  1. कॉपी किए गए Apps Script प्रोजेक्ट में, प्रोजेक्ट सेटिंग प्रोजेक्ट सेटिंग का आइकॉन पर क्लिक करें.
  2. Google Cloud Platform (GCP) प्रोजेक्ट में जाकर, प्रोजेक्ट बदलें पर क्लिक करें.
  3. GCP प्रोजेक्ट नंबर में, Google Cloud का प्रोजेक्ट नंबर चिपकाएं.
  4. प्रोजेक्ट सेट करें पर क्लिक करें.

टेस्ट डिप्लॉयमेंट इंस्टॉल करना

  1. कॉपी किए गए Apps Script प्रोजेक्ट में, एडिटर पर क्लिक करें.
  2. Code.gs फ़ाइल खोलें और चलाएं पर क्लिक करें. जब कहा जाए, तब स्क्रिप्ट को अनुमति दें.
  3. डिप्लॉय करें > डिप्लॉयमेंट की जांच करें पर क्लिक करें.
  4. इंस्टॉल करें > हो गया पर क्लिक करें.

स्क्रिप्ट चलाएं

  1. Google Docs, Sheets या Slides में कोई फ़ाइल खोलें या नई फ़ाइल बनाएं.
  2. दाईं ओर मौजूद, Translate ऐड-ऑन खोलें.
  3. अगर कहा जाए, तो ऐड-ऑन के लिए अनुमति दें.
  4. अपनी फ़ाइल में टेक्स्ट चुनें.
  5. ऐड-ऑन में, चुनें > अनुवाद करें पर क्लिक करें.

कोड की समीक्षा करें

इस समाधान के लिए Apps Script कोड की समीक्षा करने के लिए, नीचे सोर्स कोड देखें पर क्लिक करें:

सोर्स कोड देखें

Code.gs

const DEFAULT_INPUT_TEXT = '';
const DEFAULT_OUTPUT_TEXT = '';
const DEFAULT_ORIGIN_LAN = ''; // Empty string means detect langauge
const DEFAULT_DESTINATION_LAN = 'en' // English

const LANGUAGE_MAP =
  [
    { text: 'Detect Language', val: '' },
    { text: 'Afrikaans', val: 'af' },
    { text: 'Albanian', val: 'sq' },
    { text: 'Amharic', val: 'am' },
    { text: 'Arabic', val: 'ar' },
    { text: 'Armenian', val: 'hy' },
    { text: 'Azerbaijani', val: 'az' },
    { text: 'Basque', val: 'eu' },
    { text: 'Belarusian', val: 'be' },
    { text: 'Bengali', val: 'bn' },
    { text: 'Bosnian', val: 'bs' },
    { text: 'Bulgarian', val: 'bg' },
    { text: 'Catalan', val: 'ca' },
    { text: 'Cebuano', val: 'ceb' },
    { text: 'Chinese (Simplified)', val: 'zh-CN' },
    { text: 'Chinese (Traditional)', val: 'zh-TW' },
    { text: 'Corsican', val: 'co' },
    { text: 'Croatian', val: 'hr' },
    { text: 'Czech', val: 'cs' },
    { text: 'Danish', val: 'da' },
    { text: 'Dutch', val: 'nl' },
    { text: 'English', val: 'en' },
    { text: 'Esperanto', val: 'eo' },
    { text: 'Estonian', val: 'et' },
    { text: 'Finnish', val: 'fi' },
    { text: 'French', val: 'fr' },
    { text: 'Frisian', val: 'fy' },
    { text: 'Galician', val: 'gl' },
    { text: 'Georgian', val: 'ka' },
    { text: 'German', val: 'de' },
    { text: 'Greek', val: 'el' },
    { text: 'Gujarati', val: 'gu' },
    { text: 'Haitian Creole', val: 'ht' },
    { text: 'Hausa', val: 'ha' },
    { text: 'Hawaiian', val: 'haw' },
    { text: 'Hebrew', val: 'he' },
    { text: 'Hindi', val: 'hi' },
    { text: 'Hmong', val: 'hmn' },
    { text: 'Hungarian', val: 'hu' },
    { text: 'Icelandic', val: 'is' },
    { text: 'Igbo', val: 'ig' },
    { text: 'Indonesian', val: 'id' },
    { text: 'Irish', val: 'ga' },
    { text: 'Italian', val: 'it' },
    { text: 'Japanese', val: 'ja' },
    { text: 'Javanese', val: 'jv' },
    { text: 'Kannada', val: 'kn' },
    { text: 'Kazakh', val: 'kk' },
    { text: 'Khmer', val: 'km' },
    { text: 'Korean', val: 'ko' },
    { text: 'Kurdish', val: 'ku' },
    { text: 'Kyrgyz', val: 'ky' },
    { text: 'Lao', val: 'lo' },
    { text: 'Latin', val: 'la' },
    { text: 'Latvian', val: 'lv' },
    { text: 'Lithuanian', val: 'lt' },
    { text: 'Luxembourgish', val: 'lb' },
    { text: 'Macedonian', val: 'mk' },
    { text: 'Malagasy', val: 'mg' },
    { text: 'Malay', val: 'ms' },
    { text: 'Malayalam', val: 'ml' },
    { text: 'Maltese', val: 'mt' },
    { text: 'Maori', val: 'mi' },
    { text: 'Marathi', val: 'mr' },
    { text: 'Mongolian', val: 'mn' },
    { text: 'Myanmar (Burmese)', val: 'my' },
    { text: 'Nepali', val: 'ne' },
    { text: 'Norwegian', val: 'no' },
    { text: 'Nyanja (Chichewa)', val: 'ny' },
    { text: 'Pashto', val: 'ps' },
    { text: 'Persian', val: 'fa' },
    { text: 'Polish', val: 'pl' },
    { text: 'Portuguese (Portugal, Brazil)', val: 'pt' },
    { text: 'Punjabi', val: 'pa' },
    { text: 'Romanian', val: 'ro' },
    { text: 'Russian', val: 'ru' },
    { text: 'Samoan', val: 'sm' },
    { text: 'Scots Gaelic', val: 'gd' },
    { text: 'Serbian', val: 'sr' },
    { text: 'Sesotho', val: 'st' },
    { text: 'Shona', val: 'sn' },
    { text: 'Sindhi', val: 'sd' },
    { text: 'Sinhala (Sinhalese)', val: 'si' },
    { text: 'Slovak', val: 'sk' },
    { text: 'Slovenian', val: 'sl' },
    { text: 'Somali', val: 'so' },
    { text: 'Spanish', val: 'es' },
    { text: 'Sundanese', val: 'su' },
    { text: 'Swahili', val: 'sw' },
    { text: 'Swedish', val: 'sv' },
    { text: 'Tagalog (Filipino)', val: 'tl' },
    { text: 'Tajik', val: 'tg' },
    { text: 'Tamil', val: 'ta' },
    { text: 'Telugu', val: 'te' },
    { text: 'Thai', val: 'th' },
    { text: 'Turkish', val: 'tr' },
    { text: 'Ukrainian', val: 'uk' },
    { text: 'Urdu', val: 'ur' },
    { text: 'Uzbek', val: 'uz' },
    { text: 'Vietnamese', val: 'vi' },
    { text: 'Welsh', val: 'cy' },
    { text: 'Xhosa', val: 'xh' },
    { text: 'Yiddish', val: 'yi' },
    { text: 'Yoruba', val: 'yo' },
    { text: 'Zulu', val: 'zu' }
  ];


/**
 * Callback for rendering the main card.
 * @return {CardService.Card} The card to show the user.
 */
function onHomepage(e) {
  return createSelectionCard(e, DEFAULT_ORIGIN_LAN, DEFAULT_DESTINATION_LAN, DEFAULT_INPUT_TEXT, DEFAULT_OUTPUT_TEXT);
}

/**
 * Main function to generate the main card.
 * @param {String} originLanguage Language of the original text.
 * @param {String} destinationLanguage Language of the translation.
 * @param {String} inputText The text to be translated.
 * @param {String} outputText The text translated.
 * @return {CardService.Card} The card to show to the user.
 */
function createSelectionCard(e, originLanguage, destinationLanguage, inputText, outputText) {
  var hostApp = e['hostApp'];
  var builder = CardService.newCardBuilder();

  // "From" language selection & text input section
  var fromSection = CardService.newCardSection()
    .addWidget(generateLanguagesDropdown('origin', 'From: ', originLanguage))
    .addWidget(CardService.newTextInput()
      .setFieldName('input')
      .setValue(inputText)
      .setTitle('Enter text...')
      .setMultiline(true));

  if (hostApp === 'docs') {
    fromSection.addWidget(CardService.newButtonSet()
      .addButton(CardService.newTextButton()
        .setText('Get Selection')
        .setOnClickAction(CardService.newAction().setFunctionName('getDocsSelection'))
        .setDisabled(false)))
  } else if (hostApp === 'sheets') {
    fromSection.addWidget(CardService.newButtonSet()
      .addButton(CardService.newTextButton()
        .setText('Get Selection')
        .setOnClickAction(CardService.newAction().setFunctionName('getSheetsSelection'))
        .setDisabled(false)))
  } else if (hostApp === 'slides') {
    fromSection.addWidget(CardService.newButtonSet()
      .addButton(CardService.newTextButton()
        .setText('Get Selection')
        .setOnClickAction(CardService.newAction().setFunctionName('getSlidesSelection'))
        .setDisabled(false)))
  }


  builder.addSection(fromSection);

  // "Translation" language selection & text input section
  builder.addSection(CardService.newCardSection()
    .addWidget(generateLanguagesDropdown('destination', 'To: ', destinationLanguage))
    .addWidget(CardService.newTextInput()
      .setFieldName('output')
      .setValue(outputText)
      .setTitle('Translation...')
      .setMultiline(true)));

  //Buttons section
  builder.addSection(CardService.newCardSection()
    .addWidget(CardService.newButtonSet()
      .addButton(CardService.newTextButton()
        .setText('Translate')
        .setTextButtonStyle(CardService.TextButtonStyle.FILLED)
        .setOnClickAction(CardService.newAction().setFunctionName('translateText'))
        .setDisabled(false))
      .addButton(CardService.newTextButton()
        .setText('Clear')
        .setOnClickAction(CardService.newAction().setFunctionName('clearText'))
        .setDisabled(false))));

  return builder.build();

}

/**
 * Helper function to generate the drop down language menu. It checks what language the user had selected.
 * @param {String} fieldName
 * @param {String} fieldTitle
 * @param {String} previousSelected The language the user previously had selected.
 * @return {CardService.SelectionInput} The card to show to the user.
 */
function generateLanguagesDropdown(fieldName, fieldTitle, previousSelected) {
  var selectionInput = CardService.newSelectionInput().setTitle(fieldTitle)
    .setFieldName(fieldName)
    .setType(CardService.SelectionInputType.DROPDOWN);

  LANGUAGE_MAP.forEach((language, index, array) => {
    selectionInput.addItem(language.text, language.val, language.val == previousSelected);
  })

  return selectionInput;
}

/**
 * Helper function to translate the text. If the originLanguage is an empty string, the API detects the language
 * @return {CardService.Card} The card to show to the user.
 */
function translateText(e) {
  var originLanguage = e.formInput.origin;
  var destinationLanguage = e.formInput.destination;
  var inputText = e.formInput.input;

  if (originLanguage !== destinationLanguage && inputText !== undefined) {
    var translation = LanguageApp.translate(e.formInput.input, e.formInput.origin, e.formInput.destination);
    return createSelectionCard(e, originLanguage, destinationLanguage, inputText, translation);
  }
}

/**
 * Helper function to clean the text.
 * @return {CardService.Card} The card to show to the user.
 */
function clearText(e) {
  var originLanguage = e.formInput.origin;
  var destinationLanguage = e.formInput.destination;
  return createSelectionCard(e, originLanguage, destinationLanguage, DEFAULT_INPUT_TEXT, DEFAULT_OUTPUT_TEXT);
}

/**
 * Helper function to get the text selected.
 * @return {CardService.Card} The selected text.
 */
function getDocsSelection(e) {
  var text = '';
  var selection = DocumentApp.getActiveDocument().getSelection();
  Logger.log(selection)
  if (selection) {
    var elements = selection.getRangeElements();
    for (var i = 0; i < elements.length; i++) {
      Logger.log(elements[i]);
      var element = elements[i];
      // Only modify elements that can be edited as text; skip images and other non-text elements.
      if (element.getElement().asText() && element.getElement().asText().getText() !== '') {
        text += element.getElement().asText().getText() + '\n';
      }
    }
  }

  if (text !== '') {
    var originLanguage = e.formInput.origin;
    var destinationLanguage = e.formInput.destination;
    var translation = LanguageApp.translate(text, e.formInput.origin, e.formInput.destination);
    return createSelectionCard(e, originLanguage, destinationLanguage, text, translation);
  }
}

/**
 * Helper function to get the text of the selected cells.
 * @return {CardService.Card} The selected text.
 */
function getSheetsSelection(e) {
  var text = '';
  var ranges = SpreadsheetApp.getActive().getSelection().getActiveRangeList().getRanges();
  for (var i = 0; i < ranges.length; i++) {
    const range = ranges[i];
    const numRows = range.getNumRows();
    const numCols = range.getNumColumns();
    for (let i = 1; i <= numCols; i++) {
      for (let j = 1; j <= numRows; j++) {
        const cell = range.getCell(j, i);
        if (cell.getValue()) {
          text += cell.getValue() + '\n';
        }
      }
    }
  }
  if (text !== '') {
    var originLanguage = e.formInput.origin;
    var destinationLanguage = e.formInput.destination;
    var translation = LanguageApp.translate(text, e.formInput.origin, e.formInput.destination);
    return createSelectionCard(e, originLanguage, destinationLanguage, text, translation);
  }
}

/**
 * Helper function to get the selected text of the active slide.
 * @return {CardService.Card} The selected text.
 */
function getSlidesSelection(e) {
  var text = '';
  var selection = SlidesApp.getActivePresentation().getSelection();
  var selectionType = selection.getSelectionType();
  if (selectionType === SlidesApp.SelectionType.TEXT) {
    var textRange = selection.getTextRange();
    if (textRange.asString() !== '') {
      text += textRange.asString() + '\n';
    }
  }
  if (text !== '') {
    var originLanguage = e.formInput.origin;
    var destinationLanguage = e.formInput.destination;
    var translation = LanguageApp.translate(text, e.formInput.origin, e.formInput.destination);
    return createSelectionCard(e, originLanguage, destinationLanguage, text, translation);
  }
}

Appsscript.json

{
  "timeZone": "America/New_York",
  "dependencies": {},
  "exceptionLogging": "STACKDRIVER",
  "oauthScopes": [
    "https://www.googleapis.com/auth/documents.currentonly",
    "https://www.googleapis.com/auth/spreadsheets.currentonly",
    "https://www.googleapis.com/auth/presentations.currentonly"
  ],
  "runtimeVersion": "V8",
  "addOns": {
    "common": {
      "name": "Translate",
      "logoUrl": "https://www.gstatic.com/images/branding/product/1x/translate_24dp.png",
      "layoutProperties": {
        "primaryColor": "#2772ed"
      },
      "homepageTrigger": {
        "runFunction": "onHomepage"
      }
    },
    "docs" : {},
    "slides" : {},
    "sheets" : {}
  }
}

योगदानकर्ता

Google, इस सैंपल का रखरखाव Google Developer के विशेषज्ञों की मदद से करता है.

अगले चरण