Template คำนวณอัตราสิ้นเปลืองน้ำมัน และระยะทางจาก Google Map แบบอัตโนมัติ

ในโพสนี้ผมจะมาแจก Template ที่มีระบบในการคำนวณอัตราสิ้นเปลืองน้ำมันโดยประมาณแบบอัตโนมัติ รวมถึงระบบสามารถดึงระยะทางจาก Google Map มาให้แบบอัตโนมัติได้อีกด้วย

Screenshot

โดยถ้าเราต้องการที่จะนำฟังชั่นคำนวณระยะทางจาก Google Map ไปใช้งานใน template อื่นๆ เราสามารถทำการนำ script ดังต่อไปนี้ไปวางไว้ที่ script ของไฟล์ Google Sheets ของเราได้ทันที

/**
 * Calculate the distance between two
 * locations on Google Maps and convert to kilometers.
 *
 * =GOOGLEMAPS_DISTANCE_KM("NY 10005", "Hoboken NJ", "walking")
 *
 * @param {String} origin The address of starting point
 * @param {String} destination The address of destination
 * @param {String} mode The mode of travel (driving, walking, bicycling or transit)
 * @return {Number} The distance in kilometers
 * @customFunction
 */
const GOOGLEMAPS_DISTANCE_KM = (origin, destination, mode) => {
  const { routes: [data] = [] } = Maps.newDirectionFinder()
    .setOrigin(origin)
    .setDestination(destination)
    .setMode(mode)
    .getDirections();

  if (!data) {
    throw new Error('No route found!');
  }

  const { legs: [{ distance: { text: distanceText } } = {}] = [] } = data;

  const matches = distanceText.match(/([\d.]+)\s*(mi|km)/i);
  if (!matches) {
    throw new Error('Unable to parse distance');
  }

  const value = parseFloat(matches[1]);
  const unit = matches[2].toLowerCase();

  if (unit === 'mi') {
    return value * 1.60934; // convert miles to km
  } else {
    return value; // already in kilometers
  }
};

โดยวิธีการใช้งาน function นี้ให้เราทำการใส่สูตร “GOOGLEMAPS_DISTANCE_KM” ลงใน Google Sheets ได้ทันที

โดยในตัวแปรแรกจะเป็นสถานที่เริ่มต้น ยกตัวอย่างเช่น “Siam Paragon”

ตัวแปรที่สองจะเป็นสถานที่สิ้นสุด ตัวอย่างเช่น “ซีคอนสแควร์”

ตัวแปรที่สามจะเป็นวิธีการเดินทาง ตัวอย่างเช่น “Driving” คือการขับรถไป “Walking” คือการเดินไป (โดยใน template นี้เราจะใช้ mode “Driving” อย่างเดียว)

Screenshot

การใช้สูตรนี้ระบบจะ return ค่าการเดินทางที่สั้นที่สุดที่ปรากฏใน Google Map มาให้เราแบบอัตโนมัติ ซึ่งเราก็สามารถนำไปใช้กับงานประเภทอื่นได้

หากต้องการ download Template ระบบคำนวณระยะทางแบบอัตโนมัติก็สามารถคลิกที่นี้ได้เลยครับ
🔗 Template ระบบคำนวณระยะทางแบบอัตโนมัติ

💳 ชอบคลิปที่ช่วยเพิ่มประสิทธิภาพการทำงานแบบนี้สามารถสมัครสมาชิกช่องได้ที่ 
https://www.youtube.com/channel/UChxmhkD8uSSzUOkfMO_p5oQ/join

🎥 อุปกรณ์ที่ผมใช้

กล้อง Sony ZV-E10 kit 16-50mm
Mouse Logitech MX Master 3s
MacBook Air M2
ไมค์ wireless Saramonic Blink 500
เก้าอี้ Anda Seat X-Air Pro Ergonomic Gaming Chair
แขนจับจอ Anda Seat Stealth A6L Ergonomic Monitor Arm
ไมโครโฟน AKG Lyra
ไฟส่องหน้าจอ Xiaomi Light Bar

Leave a Reply

Your email address will not be published. Required fields are marked *