Google Workspace दस्तावेज़ों में डायलॉग और साइडबार

Google Docs, Sheets या Forms से जुड़ी स्क्रिप्ट में, उपयोगकर्ता-इंटरफ़ेस के कई तरह के एलिमेंट दिख सकते हैं — पहले से बने अलर्ट और प्रॉम्प्ट के साथ-साथ ऐसे डायलॉग और साइडबार जिनमें कस्टम एचटीएमएल सेवा पेज होते हैं. आम तौर पर, ये एलिमेंट मेन्यू आइटम से खोले जाते हैं. (ध्यान दें कि Google Forms में, यूज़र-इंटरफ़ेस एलिमेंट सिर्फ़ उस एडिटर को दिखते हैं जो फ़ॉर्म में बदलाव करने के लिए उसे खोलता है. वह उस उपयोगकर्ता को नहीं दिखता जो जवाब देने के लिए फ़ॉर्म खोलता है.)

सूचना से जुड़े डायलॉग

सूचना, पहले से बना हुआ एक डायलॉग बॉक्स होता है. यह Google Docs, Sheets, Slides या Forms एडिटर में खुलता है. इसमें एक मैसेज और "ठीक है" बटन दिखता है; टाइटल और दूसरे बटन इस्तेमाल करना ज़रूरी नहीं होता. यह किसी वेब ब्राउज़र में क्लाइंट-साइड JavaScript में window.alert() को कॉल करने जैसा है.

डायलॉग बॉक्स खुला होने पर, सूचना की सुविधा, सर्वर साइड स्क्रिप्ट को निलंबित कर देती है. उपयोगकर्ता के डायलॉग बंद करने के बाद स्क्रिप्ट फिर से शुरू हो जाती है. हालांकि, निलंबन के दौरान JDBC कनेक्शन नहीं बने रहते.

जैसा कि नीचे उदाहरण में दिखाया गया है, Google Docs, Forms, Slides, और Sheets, Ui.alert() तरीके का इस्तेमाल करते हैं. यह तीन वैरिएंट में उपलब्ध है. डिफ़ॉल्ट "OK" बटन को बदलने के लिए, Ui.ButtonSet enum से buttons आर्ग्युमेंट के तौर पर कोई वैल्यू पास करें. यह पता लगाने के लिए कि उपयोगकर्ता ने किस बटन पर क्लिक किया, 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 Docs, Sheets, Slides या Forms एडिटर में खुलता है. इसमें एक मैसेज, टेक्स्ट-इनपुट फ़ील्ड, और "ठीक है" बटन दिखता है. टाइटल और वैकल्पिक बटन भी ज़रूरी नहीं होते. यह किसी वेब ब्राउज़र में क्लाइंट-साइड JavaScript में window.prompt() को कॉल करने जैसा है.

डायलॉग बॉक्स खुला होने पर, सर्वर साइड स्क्रिप्ट को प्रॉम्प्ट निलंबित कर देते हैं. उपयोगकर्ता के डायलॉग बंद करने के बाद स्क्रिप्ट फिर से शुरू हो जाती है. हालांकि, निलंबन के दौरान JDBC कनेक्शन नहीं बने रहते.

जैसा कि नीचे दिए गए उदाहरण में दिखाया गया है, Google Docs फ़ॉर्म, Slides, और Sheets, Ui.prompt() तरीके का इस्तेमाल करते हैं. यह तरीका तीन वैरिएंट में उपलब्ध है. डिफ़ॉल्ट "OK" बटन को बदलने के लिए, Ui.ButtonSet Enum से buttons आर्ग्युमेंट के तौर पर कोई वैल्यू पास करें. उपयोगकर्ता के जवाब का आकलन करने के लिए, prompt() के लिए रिटर्न वैल्यू को कैप्चर करें. इसके बाद, PromptResponse.getResponseText() को कॉल करें, ताकि उपयोगकर्ता का इनपुट फिर से मिल सके. साथ ही, PromptResponse.getSelectedButton() के लिए रिटर्न वैल्यू की तुलना Ui.Button के एनम से करें.

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 Docs, Sheets, Slides या Forms एडिटर में एचटीएमएल सेवा का यूज़र इंटरफ़ेस दिखाया जा सकता है.

डायलॉग बॉक्स खुला रहने पर, कस्टम डायलॉग, सर्वर साइड स्क्रिप्ट को निलंबित नहीं करते हैं. क्लाइंट-साइड कॉम्पोनेंट, एचटीएमएल सेवा के इंटरफ़ेस के लिए google.script एपीआई का इस्तेमाल करके, सर्वर-साइड स्क्रिप्ट को एसिंक्रोनस कॉल कर सकता है.

एचटीएमएल-सेवा इंटरफ़ेस के क्लाइंट-साइड में google.script.host.close() को कॉल करके डायलॉग, अपने-आप बंद हो सकता है. डायलॉग को दूसरे इंटरफ़ेस से बंद नहीं किया जा सकता, सिर्फ़ उपयोगकर्ता या खुद उसके ज़रिए.

जैसा कि नीचे दिए गए उदाहरण में दिखाया गया है, Google Docs, Forms, Slides, और Sheets, डायलॉग खोलने के लिए 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 Docs, Forms, Slides, और Sheets एडिटर में एचटीएमएल सेवा का यूज़र इंटरफ़ेस दिखा सकता है.

डायलॉग बॉक्स खुला होने पर, साइडबार, सर्वर साइड स्क्रिप्ट को निलंबित नहीं करते हैं. क्लाइंट-साइड कॉम्पोनेंट, एचटीएमएल-सेवा इंटरफ़ेस के लिए google.script एपीआई का इस्तेमाल करके, सर्वर-साइड स्क्रिप्ट को एसिंक्रोनस कॉल कर सकता है.

एचटीएमएल-सेवा इंटरफ़ेस के क्लाइंट साइड में, google.script.host.close() को कॉल करके साइडबार खुद को बंद कर सकता है. साइडबार को दूसरे इंटरफ़ेस से, सिर्फ़ उपयोगकर्ता या खुद बंद नहीं किया जा सकता.

जैसा कि नीचे दिए गए उदाहरण में दिखाया गया है, Google Docs, Forms, Slides, और Sheets, साइडबार खोलने के लिए 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 पिकर एक ऐसा "फ़ाइल खोलने" वाला डायलॉग बॉक्स है जो Google के सर्वर पर मौजूद जानकारी को सेव करता है. इनमें Google Drive, Google Image Search, Google Video Search वगैरह शामिल हैं.

जैसा कि नीचे उदाहरण में दिखाया गया है, पिकर के क्लाइंट-साइड JavaScript API का इस्तेमाल एचटीएमएल सेवा में, एक कस्टम डायलॉग बनाने के लिए किया जा सकता है. इस डायलॉग की मदद से उपयोगकर्ता मौजूदा फ़ाइलें चुन सकते हैं या नई फ़ाइलें अपलोड कर सकते हैं. इसके बाद, उस चुने हुए विकल्प को दोबारा इस्तेमाल करने के लिए उसे आपकी स्क्रिप्ट पर पास कर सकते हैं.

पिकर चालू करने और एपीआई पासकोड पाने के लिए, इन निर्देशों का पालन करें:

  1. पुष्टि करें कि आपका स्क्रिप्ट प्रोजेक्ट, स्टैंडर्ड GCP प्रोजेक्ट का इस्तेमाल कर रहा है.
  2. अपने Google Cloud प्रोजेक्ट में "Google Picker API" चालू करें.
  3. जब आपका Google Cloud प्रोजेक्ट खुला हो, तब एपीआई और सेवाएं चुनें. इसके बाद, क्रेडेंशियल पर क्लिक करें.
  4. क्रेडेंशियल बनाएं > एपीआई पासकोड पर क्लिक करें. यह कार्रवाई कुंजी बनाती है, लेकिन आपको कुंजी में ऐप्लिकेशन पाबंदियां और एपीआई पाबंदी, दोनों जोड़ने के लिए कुंजी में बदलाव करना चाहिए.
  5. एपीआई पासकोड वाले डायलॉग बॉक्स में, बंद करें पर क्लिक करें.
  6. आपने जो एपीआई पासकोड बनाया है उसके बगल में, ज़्यादा ज़्यादा देखने का आइकॉन> एपीआई पासकोड में बदलाव करें पर क्लिक करें.
  7. ऐप्लिकेशन पाबंदियों में जाकर, नीचे दिए गए चरण पूरे करें:

    1. एचटीटीपी के रेफ़रल देने वाले (वेबसाइटें) चुनें.
    2. वेबसाइट पाबंदियां में जाकर, कोई आइटम जोड़ें पर क्लिक करें.
    3. रेफ़रलकर्ता पर क्लिक करें और *.google.com डालें.
    4. कोई दूसरा आइटम जोड़ें और रेफ़रल देने वाले के तौर पर *.googleusercontent.com डालें.
    5. हो गया पर क्लिक करें.
  8. एपीआई से जुड़ी पाबंदियों में जाकर, ये चरण पूरे करें:

    1. बटन पर पाबंदी लगाएं को चुनें.
    2. एपीआई चुनें सेक्शन में, Google पिकर एपीआई चुनें और ठीक है पर क्लिक करें.

      ध्यान दें: Google पिकर एपीआई तब तक नहीं दिखता, जब तक इसे चालू न किया जाए. इसकी वजह यह है कि सूची में सिर्फ़ वे एपीआई दिखते हैं जो Cloud प्रोजेक्ट के लिए चालू किए गए हों.

  9. एपीआई पासकोड में, क्लिपबोर्ड पर कॉपी करें क्लिपबोर्ड आइकॉन पर कॉपी करें पर क्लिक करें.

  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

picker/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>