BAHTTEXT เป็น function สำหรับแปลงตัวเลขเงินให้เป็นคำอ่านภาษาไทย
ส่วน Google sheet เป็น cloud application จาก Google ที่ทำงานได้เหมือนๆกับ Excel และฟรีอีกด้วย แต่ปัญหาอย่างหนึ่งที่อาจทำให้ผู้ที่เคยใช้ Excel มาก่อนหงุดหงิดก็คือ มันไม่มี function บางตัวที่เราใช้กันบ่อยๆใน Excel อย่างเช่น BAHTTEXT
ข้อดีของ Google sheet คือมันมีความสามารถพิเศษที่ให้เราเขียน Apps script ซึ่งเขียนได้ง่ายเพราะมีพื้นฐานมาจาก Javascript เพื่อเพิ่ม custom function อะไรก็ได้ตามที่เราต้องการเข้าไปในไฟล์ Google sheet ของเรา และทำงานได้บนทุก platform ทั้ง browser และ mobile apps เพราะ script ที่เขียนขึ้นจะประมวลผลบน server ของ Google ตามคอนเซปต์ Cloud Computing ต่างจาก VBA ของ Excel ที่ประมวลผลบนเครื่องเราและไม่สามารถรันได้บนอุปกรณ์มือถือ
ปัญหาไม่มี function BAHTTEXT จึงแก้ได้ด้วยการเขียน custom function ด้วย Apps script ซึ่ง ไอที24ชั่วโมง ได้เขียนไว้ให้ท่านนำไปใช้ได้ง่ายๆทันที และมีวิธีการใช้ดังที่อธิบายอยู่ด้านล่าง code
Code BAHTTEXT
/* * BAHTTEXT Custom Function for Google Sheet by iT24Hrs * * iT24Hrs ไอที24ชั่วโมง เปิดโลกไอที พลิกสู่ชีวิตที่ดีกว่า * https://www.it24hrs.com * * Note: เพื่อป้องกันความผิดพลาดจากการปัดเศษสตางค์ที่อาจไม่ตรงกับค่าที่แสดงใน * Google Sheet ได้ในกรณีที่ตัวเลขเกินหนึ่งล้านและมีทศนิยมมากกว่า 2 หลัก * ควรใช้ Function ROUND(value, 2) ที่ input cell ก่อนเรียกใช้ BAHTTEXT * * Copyright (C) 2015 @panraphee * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see http://www.gnu.org/licenses/ */ function BAHTTEXT(number) { if (isNaN(number)) return "#VALUE!"; var absNum = +(Math.round(Math.abs(number) + "e+2") + "e-2"); if (absNum > 9999999999999.99) return "#NUM!"; if (!absNum) return "ศูนย์บาทถ้วน"; var numArray = absNum.toFixed(2).split("."); var bahtText = (numArray[0] == "0") ? "" : NUMBERTEXT(numArray[0]) + "บาท"; bahtText += (numArray[1] == "00") ? "ถ้วน" : NUMBERTEXT(numArray[1]) + "สตางค์"; function NUMBERTEXT(myInt) { var digitArray = ["ศูนย์","หนึ่ง","สอง","สาม","สี่","ห้า","หก","เจ็ด","แปด","เก้า","สิบ"]; var placeValueArray = ["","สิบ","ร้อย","พัน","หมื่น","แสน"]; var numberText = ""; myInt = String(+myInt); for (var i = 1; i <= myInt.length; i++) { var digit = myInt.charAt(i - 1); var place = (myInt.length - i) % 6; if (digit != 0) numberText += (digit == 1 && place == 0 && i != 1) ? "เอ็ด" : digitArray[digit] + placeValueArray[place]; if (place == 0 && i != myInt.length) numberText += "ล้าน"; } return numberText.replace(/หนึ่งสิบ/g,"สิบ").replace(/สองสิบ/g,"ยี่สิบ"); } return (number < 0) ? "ลบ" + bahtText : bahtText; }
วิธีใช้
1.) copy code BAHTTEXT ด้านบนนี้
2.) เปิดไฟล์ google sheet ที่ต้องการใช้ function BAHTTEXT เช่นไฟล์ใบเสร็จ หรือ สร้างไฟล์ใหม่ บน browser ของคุณ (แนะนำให้ใช้ Chrome)
3.) เลือกเมนู เครื่องมือ > โปรแกรมแก้ไขสคริปต์
4.) ปิดหน้าต่างแนะนำ Apps Script
5.) ลบ code ตัวอย่างที่ปรากฎใน tab
6.) paste code BAHTTEXT ลงไปแทน และ กดบันทึก (save) ที่เมนูหรือ toolbar
7.) เรียกใช้ function BAHTTEXT แบบเดียวกับที่ใช้ใน Excel เช่น =BAHTTEXT(E26) เป็นต้น (หาก Error อาจต้องรอสักครู่เพื่อ update ข้อมูลบน server หรือกด refresh)
Note: เพื่อป้องกันความผิดพลาดจากการปัดเศษสตางค์ที่อาจไม่ตรงกับค่าที่แสดงใน Google Sheet ได้ในกรณีที่ตัวเลขเกินหนึ่งล้านและมีทศนิยมมากกว่า 2 หลัก ควรใช้ Function ROUND(value, 2) ที่ input cell ก่อนเรียกใช้ BAHTTEXT
เพียงเท่านี้ไฟล์ของคุณก็มี function BAHTTEXT ใช้เหมือน Excel แล้ว อย่างไรก็ตามการเพิ่ม function แบบนี้ function จะอยู่กับไฟล์นี้และเรียกใข้ได้จากไฟล์นี้เท่านั้น หากต้องการใช้กับไฟล์อื่นให้ทำซ้ำวิธีข้างต้น หรือ ทำสำเนาไฟล์ (save as) ไฟล์ใหม่จะมี function BAHTTEXT เช่นเดิม
จะเห็นได้ว่า Apps script ทำให้เราสร้างสรรค์การทำงานด้วย Google apps ได้มากมายและเรียกใช้ที่ไหนก็ได้
สนใจศึกษาเพิ่มเติม ดูที่