Google Workspace ডকুমেন্টে ডায়ালগ এবং সাইডবার

Google ডক্স, শীট বা ফর্মের সাথে আবদ্ধ স্ক্রিপ্টগুলি বিভিন্ন ধরণের ব্যবহারকারী-ইন্টারফেস উপাদানগুলি প্রদর্শন করতে পারে — পূর্ব-নির্মিত সতর্কতা এবং প্রম্পট, এছাড়াও ডায়ালগ এবং সাইডবারগুলি যাতে কাস্টম HTML পরিষেবা পৃষ্ঠা রয়েছে৷ সাধারণত, এই উপাদানগুলি মেনু আইটেম থেকে খোলা হয়। (মনে রাখবেন যে Google ফর্মগুলিতে, ব্যবহারকারী-ইন্টারফেস উপাদানগুলি কেবলমাত্র একজন সম্পাদকের কাছে দৃশ্যমান হয় যিনি ফর্মটি সংশোধন করার জন্য খোলেন, এমন কোনও ব্যবহারকারীর কাছে নয় যিনি প্রতিক্রিয়া জানাতে ফর্মটি খোলেন৷)

সতর্ক সংলাপ

একটি সতর্কতা হল একটি পূর্ব-নির্মিত ডায়ালগ বক্স যা একটি Google ডক্স, শীট, স্লাইড বা ফর্ম সম্পাদকের ভিতরে খোলে৷ এটি একটি বার্তা এবং একটি "ঠিক আছে" বোতাম প্রদর্শন করে; একটি শিরোনাম এবং বিকল্প বোতাম ঐচ্ছিক। এটি একটি ওয়েব ব্রাউজারের মধ্যে ক্লায়েন্ট-সাইড জাভাস্ক্রিপ্টে window.alert() কল করার অনুরূপ।

সংলাপ খোলা থাকাকালীন সতর্কতা সার্ভার-সাইড স্ক্রিপ্ট স্থগিত করে। ব্যবহারকারী ডায়ালগ বন্ধ করার পরে স্ক্রিপ্ট পুনরায় শুরু হয়, কিন্তু JDBC সংযোগগুলি সাসপেনশন জুড়ে থাকে না।

নীচের উদাহরণে যেমন দেখানো হয়েছে, Google ডক্স, ফর্ম, স্লাইড এবং পত্রক সবগুলিই Ui.alert() পদ্ধতি ব্যবহার করে, যা তিনটি ভেরিয়েন্টে উপলব্ধ৷ ডিফল্ট "ঠিক আছে" বোতামটি ওভাররাইড করতে, buttons আর্গুমেন্ট হিসাবে Ui.ButtonSet enum থেকে একটি মান পাস করুন। ব্যবহারকারী কোন বোতামটি ক্লিক করেছে তা মূল্যায়ন করতে, alert() এর রিটার্ন মানটিকে Ui.Button enum-এর সাথে তুলনা করুন।

function onOpen() {
  SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
      .createMenu('Custom Menu')
      .addItem('Show alert', 'showAlert')
      .addToUi();
}

function showAlert() {
  var ui = SpreadsheetApp.getUi(); // Same variations.

  var result = ui.alert(
     'Please confirm',
     'Are you sure you want to continue?',
      ui.ButtonSet.YES_NO);

  // Process the user's response.
  if (result == ui.Button.YES) {
    // User clicked "Yes".
    ui.alert('Confirmation received.');
  } else {
    // User clicked "No" or X in the title bar.
    ui.alert('Permission denied.');
  }
}

প্রম্পট ডায়ালগ

একটি প্রম্পট হল একটি পূর্ব-নির্মিত ডায়ালগ বক্স যা Google ডক্স, শীট, স্লাইড বা ফর্ম সম্পাদকের ভিতরে খোলে৷ এটি একটি বার্তা, একটি পাঠ্য-ইনপুট ক্ষেত্র এবং একটি "ঠিক আছে" বোতাম প্রদর্শন করে; একটি শিরোনাম এবং বিকল্প বোতাম ঐচ্ছিক। এটি একটি ওয়েব ব্রাউজারের মধ্যে ক্লায়েন্ট-সাইড জাভাস্ক্রিপ্টে window.prompt() কল করার অনুরূপ।

ডায়ালগ খোলা থাকাকালীন প্রম্পট সার্ভার-সাইড স্ক্রিপ্ট স্থগিত করে। ব্যবহারকারী ডায়ালগ বন্ধ করার পরে স্ক্রিপ্ট পুনরায় শুরু হয়, কিন্তু JDBC সংযোগগুলি সাসপেনশন জুড়ে থাকে না।

নীচের উদাহরণে যেমন দেখানো হয়েছে, Google ডক্স¸ ফর্ম, স্লাইড এবং পত্রক সবই Ui.prompt() পদ্ধতি ব্যবহার করে, যা তিনটি ভেরিয়েন্টে পাওয়া যায়। ডিফল্ট "ঠিক আছে" বোতামটি ওভাররাইড করতে, buttons আর্গুমেন্ট হিসাবে Ui.ButtonSet enum থেকে একটি মান পাস করুন। ব্যবহারকারীর প্রতিক্রিয়া মূল্যায়ন করতে, prompt() এর জন্য রিটার্ন মান ক্যাপচার করুন, তারপর ব্যবহারকারীর ইনপুট পুনরুদ্ধার করতে PromptResponse.getResponseText() কল করুন এবং Ui.Button enum-এর সাথে PromptResponse.getSelectedButton() এর রিটার্ন মান তুলনা করুন।

function onOpen() {
  SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
      .createMenu('Custom Menu')
      .addItem('Show prompt', 'showPrompt')
      .addToUi();
}

function showPrompt() {
  var ui = SpreadsheetApp.getUi(); // Same variations.

  var result = ui.prompt(
      'Let\'s get to know each other!',
      'Please enter your name:',
      ui.ButtonSet.OK_CANCEL);

  // Process the user's response.
  var button = result.getSelectedButton();
  var text = result.getResponseText();
  if (button == ui.Button.OK) {
    // User clicked "OK".
    ui.alert('Your name is ' + text + '.');
  } else if (button == ui.Button.CANCEL) {
    // User clicked "Cancel".
    ui.alert('I didn\'t get your name.');
  } else if (button == ui.Button.CLOSE) {
    // User clicked X in the title bar.
    ui.alert('You closed the dialog.');
  }
}

কাস্টম ডায়ালগ

একটি কাস্টম ডায়ালগ Google ডক্স, শীট, স্লাইড বা ফর্ম সম্পাদকের মধ্যে একটি HTML পরিষেবা ব্যবহারকারী ইন্টারফেস প্রদর্শন করতে পারে।

ডায়ালগ খোলা থাকার সময় কাস্টম ডায়ালগ সার্ভার-সাইড স্ক্রিপ্ট স্থগিত করে না । ক্লায়েন্ট-সাইড কম্পোনেন্ট HTML-সার্ভিস ইন্টারফেসের জন্য google.script API ব্যবহার করে সার্ভার-সাইড স্ক্রিপ্টে অ্যাসিঙ্ক্রোনাস কল করতে পারে।

ডায়ালগটি একটি HTML-সার্ভিস ইন্টারফেসের ক্লায়েন্ট সাইডে google.script.host.close() কল করে নিজেকে বন্ধ করতে পারে। ডায়ালগটি অন্য ইন্টারফেস দ্বারা বন্ধ করা যাবে না, শুধুমাত্র ব্যবহারকারী বা নিজের দ্বারা।

নীচের উদাহরণে দেখানো হয়েছে, Google ডক্স, ফর্ম, স্লাইড এবং শীটগুলি ডায়ালগ খুলতে Ui.showModalDialog() পদ্ধতি ব্যবহার করে।

Code.gs

function onOpen() {
  SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
      .createMenu('Custom Menu')
      .addItem('Show dialog', 'showDialog')
      .addToUi();
}

function showDialog() {
  var html = HtmlService.createHtmlOutputFromFile('Page')
      .setWidth(400)
      .setHeight(300);
  SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
      .showModalDialog(html, 'My custom dialog');
}

Page.html

Hello, world! <input type="button" value="Close" onclick="google.script.host.close()" />

কাস্টম সাইডবার

একটি সাইডবার একটি Google ডক্স, ফর্ম, স্লাইড এবং পত্রক সম্পাদকের মধ্যে একটি HTML পরিষেবা ব্যবহারকারী ইন্টারফেস প্রদর্শন করতে পারে।

ডায়ালগ খোলা থাকা অবস্থায় সাইডবার সার্ভার-সাইড স্ক্রিপ্ট স্থগিত করে না । ক্লায়েন্ট-সাইড কম্পোনেন্ট HTML-সার্ভিস ইন্টারফেসের জন্য google.script API ব্যবহার করে সার্ভার-সাইড স্ক্রিপ্টে অ্যাসিঙ্ক্রোনাস কল করতে পারে।

একটি HTML-সার্ভিস ইন্টারফেসের ক্লায়েন্ট সাইডে google.script.host.close() কল করে সাইডবার নিজেকে বন্ধ করতে পারে। সাইডবার অন্য ইন্টারফেস দ্বারা বন্ধ করা যাবে না, শুধুমাত্র ব্যবহারকারী বা নিজেই।

নীচের উদাহরণে দেখানো হয়েছে, Google ডক্স, ফর্ম, স্লাইড এবং শীটগুলি সাইডবার খুলতে Ui.showSidebar() পদ্ধতি ব্যবহার করে।

Code.gs

function onOpen() {
  SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
      .createMenu('Custom Menu')
      .addItem('Show sidebar', 'showSidebar')
      .addToUi();
}

function showSidebar() {
  var html = HtmlService.createHtmlOutputFromFile('Page')
      .setTitle('My custom sidebar');
  SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
      .showSidebar(html);
}

Page.html

Hello, world! <input type="button" value="Close" onclick="google.script.host.close()" />

ফাইল-খোলা ডায়ালগ

গুগল পিকার হল গুগল ড্রাইভ, গুগল ইমেজ সার্চ, গুগল ভিডিও সার্চ এবং আরও অনেক কিছু সহ Google সার্ভারে সঞ্চিত তথ্যের জন্য একটি "ফাইল-ওপেন" ডায়ালগ।

নীচের উদাহরণে দেখানো হয়েছে, পিকারের ক্লায়েন্ট-সাইড জাভাস্ক্রিপ্ট API একটি কাস্টম ডায়ালগ তৈরি করতে HTML পরিষেবাতে ব্যবহার করা যেতে পারে যা ব্যবহারকারীদের বিদ্যমান ফাইলগুলি নির্বাচন করতে বা নতুনগুলি আপলোড করতে দেয়, তারপর আরও ব্যবহারের জন্য সেই নির্বাচনটি আপনার স্ক্রিপ্টে ফেরত পাঠাতে দেয়৷

পিকার সক্ষম করতে এবং একটি API কী পেতে, এই নির্দেশাবলী অনুসরণ করুন:

  1. যাচাই করুন যে আপনার স্ক্রিপ্ট প্রকল্প একটি আদর্শ GCP প্রকল্প ব্যবহার করছে।
  2. আপনার Google ক্লাউড প্রকল্পে "Google পিকার API" সক্ষম করুন
  3. আপনার Google ক্লাউড প্রকল্পটি এখনও খোলা থাকা অবস্থায়, APIs এবং পরিষেবাগুলি নির্বাচন করুন, তারপরে শংসাপত্রে ক্লিক করুন৷
  4. শংসাপত্র তৈরি করুন > API কী ক্লিক করুন। এই ক্রিয়াটি কী তৈরি করে, তবে কীটিতে অ্যাপ্লিকেশন সীমাবদ্ধতা এবং একটি API সীমাবদ্ধতা উভয়ই যোগ করতে আপনার কীটি সম্পাদনা করা উচিত।
  5. API কী ডায়ালগে, Close এ ক্লিক করুন।
  6. আপনার তৈরি করা API কী-এর পাশে, আরও ক্লিক করুন আরও আইকন > API কী সম্পাদনা করুন
  7. অ্যাপ্লিকেশন সীমাবদ্ধতার অধীনে, নিম্নলিখিত পদক্ষেপগুলি সম্পূর্ণ করুন:

    1. HTTP রেফারার (ওয়েব সাইট) নির্বাচন করুন।
    2. ওয়েবসাইট সীমাবদ্ধতার অধীনে, একটি আইটেম যোগ করুন ক্লিক করুন।
    3. Referrer এ ক্লিক করুন এবং *.google.com এ প্রবেশ করুন।
    4. আরেকটি আইটেম যোগ করুন এবং রেফারার হিসাবে *.googleusercontent.com লিখুন।
    5. সম্পন্ন ক্লিক করুন.
  8. API সীমাবদ্ধতার অধীনে, নিম্নলিখিত পদক্ষেপগুলি সম্পূর্ণ করুন:

    1. সীমাবদ্ধ কী নির্বাচন করুন।
    2. APIs নির্বাচন করুন বিভাগে, Google পিকার API নির্বাচন করুন এবং ঠিক আছে ক্লিক করুন।

      দ্রষ্টব্য: আপনি যদি সক্রিয় না করেন তবে Google পিকার API প্রদর্শিত হয় না কারণ তালিকাটি শুধুমাত্র ক্লাউড প্রকল্পের জন্য সক্ষম করা APIগুলি দেখায়৷

  9. API কী-এর অধীনে, ক্লিপবোর্ডে অনুলিপি ক্লিক করুন ক্লিপবোর্ড আইকনে অনুলিপি করুন .

  10. নীচে, সংরক্ষণ করুন ক্লিক করুন।

code.gs

/**
 * Creates a custom menu in Google Sheets when the spreadsheet opens.
 */
function onOpen() {
  try {
    SpreadsheetApp.getUi().createMenu('Picker')
        .addItem('Start', 'showPicker')
        .addToUi();
  } catch (e) {
    // TODO (Developer) - Handle exception
    console.log('Failed with error: %s', e.error);
  }
}

/**
 * Displays an HTML-service dialog in Google Sheets that contains client-side
 * JavaScript code for the Google Picker API.
 */
function showPicker() {
  try {
    const html = HtmlService.createHtmlOutputFromFile('dialog.html')
        .setWidth(600)
        .setHeight(425)
        .setSandboxMode(HtmlService.SandboxMode.IFRAME);
    SpreadsheetApp.getUi().showModalDialog(html, 'Select a file');
  } catch (e) {
    // TODO (Developer) - Handle exception
    console.log('Failed with error: %s', e.error);
  }
}

/**
 * Gets the user's OAuth 2.0 access token so that it can be passed to Picker.
 * This technique keeps Picker from needing to show its own authorization
 * dialog, but is only possible if the OAuth scope that Picker needs is
 * available in Apps Script. In this case, the function includes an unused call
 * to a DriveApp method to ensure that Apps Script requests access to all files
 * in the user's Drive.
 *
 * @return {string} The user's OAuth 2.0 access token.
 */
function getOAuthToken() {
  try {
    DriveApp.getRootFolder();
    return ScriptApp.getOAuthToken();
  } catch (e) {
    // TODO (Developer) - Handle exception
    console.log('Failed with error: %s', e.error);
  }
}

dialog.html

পিকার/dialog.html
<!DOCTYPE html>
<html>
<head>
  <link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons.css">
  <script>
    // IMPORTANT: Replace the value for DEVELOPER_KEY with the API key obtained
    // from the Google Developers Console.
    var DEVELOPER_KEY = 'ABC123 ... ';
    var DIALOG_DIMENSIONS = {width: 600, height: 425};
    var pickerApiLoaded = false;

    /**
     * Loads the Google Picker API.
     */
    function onApiLoad() {
      gapi.load('picker', {'callback': function() {
        pickerApiLoaded = true;
      }});
     }

    /**
     * Gets the user's OAuth 2.0 access token from the server-side script so that
     * it can be passed to Picker. This technique keeps Picker from needing to
     * show its own authorization dialog, but is only possible if the OAuth scope
     * that Picker needs is available in Apps Script. Otherwise, your Picker code
     * will need to declare its own OAuth scopes.
     */
    function getOAuthToken() {
      google.script.run.withSuccessHandler(createPicker)
          .withFailureHandler(showError).getOAuthToken();
    }

    /**
     * Creates a Picker that can access the user's spreadsheets. This function
     * uses advanced options to hide the Picker's left navigation panel and
     * default title bar.
     *
     * @param {string} token An OAuth 2.0 access token that lets Picker access the
     *     file type specified in the addView call.
     */
    function createPicker(token) {
      if (pickerApiLoaded && token) {
        var picker = new google.picker.PickerBuilder()
            // Instruct Picker to display only spreadsheets in Drive. For other
            // views, see https://developers.google.com/picker/docs/#otherviews
            .addView(google.picker.ViewId.SPREADSHEETS)
            // Hide the navigation panel so that Picker fills more of the dialog.
            .enableFeature(google.picker.Feature.NAV_HIDDEN)
            // Hide the title bar since an Apps Script dialog already has a title.
            .hideTitleBar()
            .setOAuthToken(token)
            .setDeveloperKey(DEVELOPER_KEY)
            .setCallback(pickerCallback)
            .setOrigin(google.script.host.origin)
            // Instruct Picker to fill the dialog, minus 2 pixels for the border.
            .setSize(DIALOG_DIMENSIONS.width - 2,
                DIALOG_DIMENSIONS.height - 2)
            .build();
        picker.setVisible(true);
      } else {
        showError('Unable to load the file picker.');
      }
    }

    /**
     * A callback function that extracts the chosen document's metadata from the
     * response object. For details on the response object, see
     * https://developers.google.com/picker/docs/result
     *
     * @param {object} data The response object.
     */
    function pickerCallback(data) {
      var action = data[google.picker.Response.ACTION];
      if (action == google.picker.Action.PICKED) {
        var doc = data[google.picker.Response.DOCUMENTS][0];
        var id = doc[google.picker.Document.ID];
        var url = doc[google.picker.Document.URL];
        var title = doc[google.picker.Document.NAME];
        document.getElementById('result').innerHTML =
            '<b>You chose:</b><br>Name: <a href="' + url + '">' + title +
            '</a><br>ID: ' + id;
      } else if (action == google.picker.Action.CANCEL) {
        document.getElementById('result').innerHTML = 'Picker canceled.';
      }
    }

    /**
     * Displays an error message within the #result element.
     *
     * @param {string} message The error message to display.
     */
    function showError(message) {
      document.getElementById('result').innerHTML = 'Error: ' + message;
    }
  </script>
</head>
<body>
  <div>
    <button onclick="getOAuthToken()">Select a file</button>
    <p id="result"></p>
  </div>
  <script src="https://apis.google.com/js/api.js?onload=onApiLoad"></script>
</body>
</html>