ড্রাইভিং দূরত্ব গণনা করুন & মিটারকে মাইলে রূপান্তর করুন

কোডিং স্তর : শিক্ষানবিস
সময়কাল : 10 মিনিট
প্রকল্পের ধরন : একটি কাস্টম মেনু সহ কাস্টম ফাংশন এবং অটোমেশন

উদ্দেশ্য

  • বুঝুন সমাধান কি করে।
  • সমাধানের মধ্যে অ্যাপস স্ক্রিপ্ট পরিষেবাগুলি কী করে তা বুঝুন।
  • স্ক্রিপ্ট সেট আপ করুন।
  • স্ক্রিপ্ট চালান।

এই সমাধান সম্পর্কে

কাস্টম ফাংশন ব্যবহার করে, আপনি দুটি অবস্থানের মধ্যে ড্রাইভিং দূরত্ব গণনা করতে পারেন এবং দূরত্বকে মিটার থেকে মাইলে রূপান্তর করতে পারেন। একটি অতিরিক্ত অটোমেশন একটি কাস্টম মেনু প্রদান করে যা আপনাকে একটি নতুন শীটে শুরুর ঠিকানা থেকে শেষ ঠিকানায় ধাপে ধাপে নির্দেশনা যোগ করতে দেয়।

একটি শীটে গাড়ি চালানোর দিকনির্দেশের স্ক্রিনশট।

কিভাবে এটা কাজ করে

স্ক্রিপ্টটি 2টি কাস্টম ফাংশন এবং একটি অটোমেশন ব্যবহার করে।

  • drivingDistance(origin, destination) ফাংশন দুটি অবস্থানের মধ্যে ড্রাইভিং দিকনির্দেশ গণনা করতে এবং মিটারে দুটি ঠিকানার মধ্যে দূরত্ব ফেরত দিতে মানচিত্র পরিষেবা ব্যবহার করে।
  • metersToMiles(meters) ফাংশন একটি নির্দিষ্ট মিটারের জন্য মাইলের সমতুল্য সংখ্যা গণনা করে।
  • অটোমেশন ব্যবহারকারীকে ড্রাইভিং দিকনির্দেশ গণনা করতে শুরু এবং শেষ ঠিকানার কোন সারি প্রবেশ করতে অনুরোধ করে এবং একটি নতুন শীটে ধাপে ধাপে ড্রাইভিং দিকনির্দেশ যোগ করে।

অ্যাপস স্ক্রিপ্ট পরিষেবা

এই সমাধানটি নিম্নলিখিত পরিষেবাগুলি ব্যবহার করে:

  • স্প্রেডশীট পরিষেবা - কাস্টম মেনু যোগ করে, এই সমাধানটি পরীক্ষা করার জন্য ডেমো ডেটা যোগ করে এবং স্ক্রিপ্ট যখন ড্রাইভিং দিকনির্দেশ যোগ করে তখন নতুন শীটগুলি ফর্ম্যাট করে৷
  • বেস পরিষেবা - ব্যবহারকারীকে নির্দেশের জন্য একটি সারি নম্বর লিখতে অনুরোধ করতে Browser ক্লাস ব্যবহার করে এবং কোনও ত্রুটি ঘটলে ব্যবহারকারীকে সতর্ক করে।
  • ইউটিলিটি পরিষেবা - ব্যবহারকারী-নির্দিষ্ট তথ্য সহ টেমপ্লেটেড স্ট্রিং আপডেট করে।
  • মানচিত্র পরিষেবা - শুরুর ঠিকানা থেকে শেষ ঠিকানা পর্যন্ত ধাপে ধাপে Google মানচিত্রের দিকনির্দেশ পায়।

পূর্বশর্ত

এই নমুনা ব্যবহার করতে, আপনার নিম্নলিখিত পূর্বশর্ত প্রয়োজন:

  • একটি Google অ্যাকাউন্ট (Google Workspace অ্যাকাউন্টের জন্য অ্যাডমিনিস্ট্রেটরের অনুমোদনের প্রয়োজন হতে পারে)।
  • ইন্টারনেট অ্যাক্সেস সহ একটি ওয়েব ব্রাউজার।

স্ক্রিপ্ট সেট আপ করুন

  1. ড্রাইভিং দূরত্ব গণনা করার একটি অনুলিপি তৈরি করুন এবং মিটারকে মাইল স্প্রেডশীটে রূপান্তর করুন । এই সমাধানের জন্য অ্যাপস স্ক্রিপ্ট প্রকল্পটি স্প্রেডশীটের সাথে সংযুক্ত রয়েছে৷
    একটি কপি করো
  2. আপনার শীটে হেডার এবং ডেমো ডেটা যোগ করতে, দিকনির্দেশ > শীট প্রস্তুত করুন এ ক্লিক করুন। এই কাস্টম মেনু প্রদর্শিত হওয়ার জন্য আপনাকে পৃষ্ঠাটি রিফ্রেশ করতে হতে পারে।
  3. অনুরোধ করা হলে, স্ক্রিপ্ট অনুমোদন করুন. যদি OAuth সম্মতি স্ক্রীন সতর্কতা প্রদর্শন করে, এই অ্যাপটি যাচাই করা হয়নি , তাহলে Advanced > Go to {Project Name} (অনিরাপদ) নির্বাচন করে চালিয়ে যান।

  4. নির্দেশাবলী > আবার শীট প্রস্তুত করুন ক্লিক করুন।

স্ক্রিপ্ট চালান

  1. C2 কক্ষে, সূত্রটি লিখুন =DRIVINGDISTANCE(A2,B2) এবং এন্টার টিপুন। আপনি যদি দশমিক কমা ব্যবহার করে এমন একটি অবস্থানে থাকেন, তাহলে আপনাকে এর পরিবর্তে =DRIVINGDISTANCE(A2;B2) লিখতে হবে।
  2. ঘরে D2 , সূত্রটি লিখুন =METERSTOMILES(C2) এবং এন্টার টিপুন।
  3. (ঐচ্ছিক) শুরু এবং শেষ ঠিকানার অতিরিক্ত সারি যোগ করুন এবং বিভিন্ন স্থানের মধ্যে ড্রাইভিং দূরত্ব গণনা করতে C এবং D কলামে সূত্র অনুলিপি করুন।
  4. নির্দেশাবলী > ধাপে ধাপে তৈরি করুন ক্লিক করুন।
  5. ডায়ালগে, যে ঠিকানাগুলির জন্য আপনি দিকনির্দেশ তৈরি করতে চান তার সারি নম্বর লিখুন এবং ঠিক আছে ক্লিক করুন।
  6. স্ক্রিপ্ট তৈরি করা নতুন শীটে ড্রাইভিং নির্দেশাবলী পর্যালোচনা করুন।

কোড পর্যালোচনা করুন

এই সমাধানের জন্য অ্যাপস স্ক্রিপ্ট কোড পর্যালোচনা করতে, নীচের উৎস কোড দেখুন ক্লিক করুন:

সোর্স কোড দেখুন

Code.gs

sheets/customFunctions/customFunctions.gs
/**
 * @OnlyCurrentDoc Limits the script to only accessing the current sheet.
 */

/**
 * A special function that runs when the spreadsheet is open, used to add a
 * custom menu to the spreadsheet.
 */
function onOpen() {
  try {
    const spreadsheet = SpreadsheetApp.getActive();
    const menuItems = [
      {name: 'Prepare sheet...', functionName: 'prepareSheet_'},
      {name: 'Generate step-by-step...', functionName: 'generateStepByStep_'}
    ];
    spreadsheet.addMenu('Directions', menuItems);
  } catch (e) {
    // TODO (Developer) - Handle Exception
    console.log('Failed with error: %s' + e.error);
  }
}

/**
 * A custom function that converts meters to miles.
 *
 * @param {Number} meters The distance in meters.
 * @return {Number} The distance in miles.
 */
function metersToMiles(meters) {
  if (typeof meters !== 'number') {
    return null;
  }
  return meters / 1000 * 0.621371;
}

/**
 * A custom function that gets the driving distance between two addresses.
 *
 * @param {String} origin The starting address.
 * @param {String} destination The ending address.
 * @return {Number} The distance in meters.
 */
function drivingDistance(origin, destination) {
  const directions = getDirections_(origin, destination);
  return directions.routes[0].legs[0].distance.value;
}

/**
 * A function that adds headers and some initial data to the spreadsheet.
 */
function prepareSheet_() {
  try {
    const sheet = SpreadsheetApp.getActiveSheet().setName('Settings');
    const headers = [
      'Start Address',
      'End Address',
      'Driving Distance (meters)',
      'Driving Distance (miles)'];
    const initialData = [
      '350 5th Ave, New York, NY 10118',
      '405 Lexington Ave, New York, NY 10174'];
    sheet.getRange('A1:D1').setValues([headers]).setFontWeight('bold');
    sheet.getRange('A2:B2').setValues([initialData]);
    sheet.setFrozenRows(1);
    sheet.autoResizeColumns(1, 4);
  } catch (e) {
    // TODO (Developer) - Handle Exception
    console.log('Failed with error: %s' + e.error);
  }
}

/**
 * Creates a new sheet containing step-by-step directions between the two
 * addresses on the "Settings" sheet that the user selected.
 */
function generateStepByStep_() {
  try {
    const spreadsheet = SpreadsheetApp.getActive();
    const settingsSheet = spreadsheet.getSheetByName('Settings');
    settingsSheet.activate();

    // Prompt the user for a row number.
    const selectedRow = Browser
        .inputBox('Generate step-by-step', 'Please enter the row number of' +
        ' the' + ' addresses to use' + ' (for example, "2"):',
        Browser.Buttons.OK_CANCEL);
    if (selectedRow === 'cancel') {
      return;
    }
    const rowNumber = Number(selectedRow);
    if (isNaN(rowNumber) || rowNumber < 2 ||
      rowNumber > settingsSheet.getLastRow()) {
      Browser.msgBox('Error',
          Utilities.formatString('Row "%s" is not valid.', selectedRow),
          Browser.Buttons.OK);
      return;
    }


    // Retrieve the addresses in that row.
    const row = settingsSheet.getRange(rowNumber, 1, 1, 2);
    const rowValues = row.getValues();
    const origin = rowValues[0][0];
    const destination = rowValues[0][1];
    if (!origin || !destination) {
      Browser.msgBox('Error', 'Row does not contain two addresses.',
          Browser.Buttons.OK);
      return;
    }

    // Get the raw directions information.
    const directions = getDirections_(origin, destination);

    // Create a new sheet and append the steps in the directions.
    const sheetName = 'Driving Directions for Row ' + rowNumber;
    let directionsSheet = spreadsheet.getSheetByName(sheetName);
    if (directionsSheet) {
      directionsSheet.clear();
      directionsSheet.activate();
    } else {
      directionsSheet =
        spreadsheet.insertSheet(sheetName, spreadsheet.getNumSheets());
    }
    const sheetTitle = Utilities
        .formatString('Driving Directions from %s to %s', origin, destination);
    const headers = [
      [sheetTitle, '', ''],
      ['Step', 'Distance (Meters)', 'Distance (Miles)']
    ];
    const newRows = [];
    for (const step of directions.routes[0].legs[0].steps) {
      // Remove HTML tags from the instructions.
      const instructions = step.html_instructions
          .replace(/<br>|<div.*?>/g, '\n').replace(/<.*?>/g, '');
      newRows.push([
        instructions,
        step.distance.value
      ]);
    }
    directionsSheet.getRange(1, 1, headers.length, 3).setValues(headers);
    directionsSheet.getRange(headers.length + 1, 1, newRows.length, 2)
        .setValues(newRows);
    directionsSheet.getRange(headers.length + 1, 3, newRows.length, 1)
        .setFormulaR1C1('=METERSTOMILES(R[0]C[-1])');

    // Format the new sheet.
    directionsSheet.getRange('A1:C1').merge().setBackground('#ddddee');
    directionsSheet.getRange('A1:2').setFontWeight('bold');
    directionsSheet.setColumnWidth(1, 500);
    directionsSheet.getRange('B2:C').setVerticalAlignment('top');
    directionsSheet.getRange('C2:C').setNumberFormat('0.00');
    const stepsRange = directionsSheet.getDataRange()
        .offset(2, 0, directionsSheet.getLastRow() - 2);
    setAlternatingRowBackgroundColors_(stepsRange, '#ffffff', '#eeeeee');
    directionsSheet.setFrozenRows(2);
    SpreadsheetApp.flush();
  } catch (e) {
    // TODO (Developer) - Handle Exception
    console.log('Failed with error: %s' + e.error);
  }
}

/**
 * Sets the background colors for alternating rows within the range.
 * @param {Range} range The range to change the background colors of.
 * @param {string} oddColor The color to apply to odd rows (relative to the
 *     start of the range).
 * @param {string} evenColor The color to apply to even rows (relative to the
 *     start of the range).
 */
function setAlternatingRowBackgroundColors_(range, oddColor, evenColor) {
  const backgrounds = [];
  for (let row = 1; row <= range.getNumRows(); row++) {
    const rowBackgrounds = [];
    for (let column = 1; column <= range.getNumColumns(); column++) {
      if (row % 2 === 0) {
        rowBackgrounds.push(evenColor);
      } else {
        rowBackgrounds.push(oddColor);
      }
    }
    backgrounds.push(rowBackgrounds);
  }
  range.setBackgrounds(backgrounds);
}

/**
 * A shared helper function used to obtain the full set of directions
 * information between two addresses. Uses the Apps Script Maps Service.
 *
 * @param {String} origin The starting address.
 * @param {String} destination The ending address.
 * @return {Object} The directions response object.
 */
function getDirections_(origin, destination) {
  const directionFinder = Maps.newDirectionFinder();
  directionFinder.setOrigin(origin);
  directionFinder.setDestination(destination);
  const directions = directionFinder.getDirections();
  if (directions.status !== 'OK') {
    throw directions.error_message;
  }
  return directions;
}

অবদানকারী

এই নমুনা Google ডেভেলপার বিশেষজ্ঞদের সাহায্যে Google দ্বারা রক্ষণাবেক্ষণ করা হয়।

পরবর্তী পদক্ষেপ