क्यूरेट की गई सामग्री भेजें

कोडिंग का लेवल: शुरुआती
कुल समय: 20 मिनट
प्रोजेक्ट टाइप: इवेंट से चलने वाले ट्रिगर के साथ ऑटोमेशन की सुविधा

मकसद

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

इस सलूशन के बारे में जानकारी

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

Google Forms और Gmail का इस्तेमाल करके कॉन्टेंट भेजने का डेमो

यह सुविधा कैसे काम करती है

स्क्रिप्ट एक इवेंट-ड्रिवन ट्रिगर इंस्टॉल करती है, जो हर बार उपयोगकर्ता के फ़ॉर्म सबमिट करने पर चलता है. हर फ़ॉर्म सबमिशन के साथ, स्क्रिप्ट एक Google Docs टेंप्लेट बनाती है और उससे ईमेल भेजती है. इस ईमेल में उपयोगकर्ता का नाम और उसके चुने गए कॉन्टेंट की जानकारी शामिल होती है. आपका कॉन्टेंट किसी भी तरह का हो सकता है, बशर्ते उसे यूआरएल से रेफ़र किया गया हो.

Apps Script सेवाएं

यह समाधान नीचे दी गई सेवाओं का इस्तेमाल करता है:

  • स्क्रिप्ट सेवा–जब कोई व्यक्ति फ़ॉर्म सबमिट करता है, तब ट्रिगर होने वाला इवेंट-ड्रिवन ट्रिगर इंस्टॉल करता है.
  • दस्तावेज़ सेवा– वह Docs टेंप्लेट खोलता है जिसका इस्तेमाल स्क्रिप्ट, ईमेल बनाने के लिए करती है.
  • मेल सेवा–उपयोगकर्ता के नाम और चुने गए कॉन्टेंट के साथ ईमेल बनाती और भेजती है.
  • स्प्रेडशीट सेवा–स्क्रिप्ट से ईमेल भेजे जाने के बाद, फ़ॉर्म रिस्पॉन्स शीट में पुष्टि करने की सुविधा जोड़ी जाती है.

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

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

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

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

  1. क्यूरेट किया गया कॉन्टेंट भेजें स्प्रेडशीट की कॉपी बनाने के लिए, नीचे दिए गए बटन पर क्लिक करें. इस समाधान के लिए, Apps Script प्रोजेक्ट, स्प्रेडशीट से अटैच होता है.
    एक कॉपी बनाएं

  2. कॉपी की गई स्प्रेडशीट में, एक्सटेंशन > Apps Script पर क्लिक करें.

  3. फ़ंक्शन के ड्रॉपडाउन में, installTrigger चुनें.

  4. चलाएं पर क्लिक करें.

  5. जब कहा जाए, तब स्क्रिप्ट को अनुमति दें. अगर OAuth के लिए सहमति वाली स्क्रीन पर चेतावनी दिखती है, तो इस ऐप्लिकेशन की पुष्टि नहीं हुई है. बेहतर सेटिंग > को चुनकर जारी रखें {Project Name} पर जाएं (असुरक्षित).

ज़रूरी जानकारी: अगर एक से ज़्यादा बार installtrigger चलाया जाता है, तो स्क्रिप्ट कई ट्रिगर बनाती है. इन ट्रिगर में से हर ट्रिगर को तब ईमेल भेजा जाता है, जब उपयोगकर्ता फ़ॉर्म सबमिट करता है. अतिरिक्त ट्रिगर मिटाने और डुप्लीकेट ईमेल से बचने के लिए, ट्रिगर पर क्लिक करें . हर एक अतिरिक्त ट्रिगर पर राइट-क्लिक करें और ट्रिगर मिटाएं पर क्लिक करें.

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

  1. स्प्रेडशीट पर वापस जाएं और टूल > फ़ॉर्म मैनेज करें > लाइव फ़ॉर्म पर जाएं पर क्लिक करें.
  2. फ़ॉर्म भरें और सबमिट करें पर क्लिक करें.
  3. आपके चुने गए कॉन्टेंट के लिंक वाला ईमेल देखने के लिए, अपना ईमेल देखें.

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

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

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

Code.gs

solutions/automations/content-signup/Code.js
// To learn how to use this script, refer to the documentation:
// https://developers.google.com/apps-script/samples/automations/content-signup

/*
Copyright 2022 Google LLC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// To use your own template doc, update the below variable with the URL of your own Google Doc template.
// Make sure you update the sharing settings so that 'anyone'  or 'anyone in your organization' can view.
const EMAIL_TEMPLATE_DOC_URL = 'https://docs.google.com/document/d/1enes74gWsMG3dkK3SFO08apXkr0rcYBd3JHKOb2Nksk/edit?usp=sharing';
// Update this variable to customize the email subject.
const EMAIL_SUBJECT = 'Hello, here is the content you requested';

// Update this variable to the content titles and URLs you want to offer. Make sure you update the form so that the content titles listed here match the content titles you list in the form.
const topicUrls = {
  'Google Calendar how-to videos': 'https://www.youtube.com/playlist?list=PLU8ezI8GYqs7IPb_UdmUNKyUCqjzGO9PJ',
  'Google Drive how-to videos': 'https://www.youtube.com/playlist?list=PLU8ezI8GYqs7Y5d1cgZm2Obq7leVtLkT4',
  'Google Docs how-to videos': 'https://www.youtube.com/playlist?list=PLU8ezI8GYqs4JKwZ-fpBP-zSoWPL8Sit7',
  'Google Sheets how-to videos': 'https://www.youtube.com/playlist?list=PLU8ezI8GYqs61ciKpXf_KkV7ZRbRHVG38',
};

/**
 * Installs a trigger on the spreadsheet for when someone submits a form.
 */
function installTrigger() {
  ScriptApp.newTrigger('onFormSubmit')
      .forSpreadsheet(SpreadsheetApp.getActive())
      .onFormSubmit()
      .create();
}

/**
 * Sends a customized email for every form response.
 * 
 * @param {Object} event - Form submit event
 */
function onFormSubmit(e) {
  let responses = e.namedValues;

  // If the question title is a label, it can be accessed as an object field.
  // If it has spaces or other characters, it can be accessed as a dictionary.
  let timestamp = responses.Timestamp[0];
  let email = responses['Email address'][0].trim();
  let name = responses.Name[0].trim();
  let topicsString = responses.Topics[0].toLowerCase();

  // Parse topics of interest into a list (since there are multiple items
  // that are saved in the row as blob of text).
  let topics = Object.keys(topicUrls).filter(function(topic) {
    // indexOf searches for the topic in topicsString and returns a non-negative
    // index if the topic is found, or it will return -1 if it's not found.
    return topicsString.indexOf(topic.toLowerCase()) != -1;
  });

  // If there is at least one topic selected, send an email to the recipient.
  let status = '';
  if (topics.length > 0) {
    MailApp.sendEmail({
      to: email,
      subject: EMAIL_SUBJECT,
      htmlBody: createEmailBody(name, topics),
    });
    status = 'Sent';
  }
  else {
    status = 'No topics selected';
  }

  // Append the status on the spreadsheet to the responses' row.
  let sheet = SpreadsheetApp.getActiveSheet();
  let row = sheet.getActiveRange().getRow();
  let column = e.values.length + 1;
  sheet.getRange(row, column).setValue(status);

  console.log("status=" + status + "; responses=" + JSON.stringify(responses));
}

/**
 * Creates email body and includes the links based on topic.
 *
 * @param {string} recipient - The recipient's email address.
 * @param {string[]} topics - List of topics to include in the email body.
 * @return {string} - The email body as an HTML string.
 */
function createEmailBody(name, topics) {
  let topicsHtml = topics.map(function(topic) {
  let url = topicUrls[topic];
    return '<li><a href="' + url + '">' + topic + '</a></li>';
  }).join('');
  topicsHtml = '<ul>' + topicsHtml + '</ul>';

  // Make sure to update the emailTemplateDocId at the top.
  let docId = DocumentApp.openByUrl(EMAIL_TEMPLATE_DOC_URL).getId();
  let emailBody = docToHtml(docId);
  emailBody = emailBody.replace(/{{NAME}}/g, name);
  emailBody = emailBody.replace(/{{TOPICS}}/g, topicsHtml);
  return emailBody;
}

/**
 * Downloads a Google Doc as an HTML string.
 * 
 * @param {string} docId - The ID of a Google Doc to fetch content from.
 * @return {string} The Google Doc rendered as an HTML string.
 */
function docToHtml(docId) {

  // Downloads a Google Doc as an HTML string.
  let url = "https://docs.google.com/feeds/download/documents/export/Export?id=" +
            docId + "&exportFormat=html";
  let param = {
    method: "get",
    headers: {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
    muteHttpExceptions: true,
  };
  return UrlFetchApp.fetch(url, param).getContentText();
}

योगदानकर्ता

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

अगले चरण