มาเขียน Notion Formula with Confidence ด้วย TDD กัน (แจก Formula Thousand Comma ด้วยนะ)
สวัสดีครับ วันนี้ผมมาแชร์เทคนิคการเขียน สูตรแบบ ยังไงก็ไม่พลาดแน่ๆ กันครับ เทคนิคที่ใช้ TDD (Test-Driven Development) คือ โดยผมประยุกต์ใช้ใน Notion โดย
ในตัวอย่างนี้จะใช้สูตรการใส่ comma เลขหลักพันนะครับ มาเริ่มกันเลย!
-
การสร้าง Table มา
-
กำหนด Column
- Number = คือค่าตั้งต้น
- Output = สูตรของเรา
- Expected = สิ่งที่เราอยากให้เป็น คำตอบที่ถูกต้อง
- Tester = สูตรง่ายๆ สำหรับเช็คว่า Output กับ Expected เท่ากันมั้ย
-
เราจะเริ่มจากให้ Pass บางเคสก่อน (เอาจริงๆ ต้องเริ่มจาก Fail ทุกเคส แต่นี้ขอข้ามขั้นตอนนั้นไป)
-
จากนั้น เราจะค่อยๆ ปรับแก้สูตรไปเรื่อยๆ จนผ่านครบทุกเคสครับ
Thousand Comma Formula
if(empty(prop("Number")), "", concat(if(round(prop("Number")) > 1000000, concat(format(floor(round(prop("Number")) / 1000000)), ","), ""), if(mod(round(prop("Number")), 1000000) > 1000, concat(format(floor(mod(round(prop("Number")), 1000000) / 1000)), ",", if(mod(mod(round(prop("Number")), 1000000), 1000) < 10, "00", if(mod(mod(round(prop("Number")), 1000000), 1000) < 100, "0", "")), format(mod(mod(round(prop("Number")), 1000000), 1000))), format(mod(round(prop("Number")), 1000000)))))
Tester Formula
if(prop("Output") == prop("Expected"), "✅ PASS", "❌ FAILED")