Last active
May 14, 2020 02:25
-
-
Save peerasak-u/3bc9cecc2d859d3072b64fbc98fe4f6e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const getShowtimes = () => { | |
const spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); | |
const range = spreadsheet.getSheetByName('ShowTime').getRange('A2:D'); | |
const values = range.getValues(); | |
const showtimes = values.filter((val) => val[0]).map(mappingShowTime); | |
const incoming = showtimes.filter(isIncoming) | |
const showing = showtimes.filter(isShowing) | |
const alreayShown = showtimes.filter(wasAlreadyShown) | |
return { | |
incoming, | |
showing, | |
alreayShown | |
} | |
} | |
const mappingShowTime = (item) => { | |
const now = new Date(); | |
const hours = now.getHours(); | |
const mins = now.getMinutes(); | |
const year = now.getFullYear(); | |
const month = now.getMonth(); | |
const day = now.getDate(); | |
const time = now.getTime(); | |
const startDate = new Date(item[1]); | |
const endDate = new Date(item[2]); | |
const sHours = startDate.getHours(); | |
const sMins = startDate.getMinutes(); | |
const eHours = endDate.getHours(); | |
const eMins = endDate.getMinutes(); | |
const startAt = new Date(year, month, day, sHours, sMins, 0 ,0); | |
const endAt = new Date(year, month, day, eHours, eMins, 0 ,0); | |
return { | |
agenda: item[0], | |
startAt, | |
endAt, | |
speaker: item[3] | |
} | |
} | |
const isIncoming = (showtime) => { | |
const now = new Date(); | |
const time = now.getTime(); | |
const startTime = showtime.startAt.getTime(); | |
return time < startTime | |
} | |
const isShowing = (showtime) => { | |
const now = new Date(); | |
const time = now.getTime(); | |
const startTime = showtime.startAt.getTime(); | |
const endTime = showtime.endAt.getTime(); | |
return time >= startTime && time <= endTime | |
} | |
const wasAlreadyShown = (showtime) => { | |
const now = new Date(); | |
const time = now.getTime(); | |
const endTime = showtime.endAt.getTime(); | |
return time > endTime | |
} | |
const doGet = () => { | |
const showtimes = getShowtimes() | |
return ContentService.createTextOutput(JSON.stringify(showtimes)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ไหนๆก็ได้คำตอบในฝั่งของ formula ไปแล้วนะครับ
.
ผมจะมาตอบในฝั่งของ script บ้างแต่ขอมองต่างมุมเพราะเห็นว่าเจ้าของโพสต์ต้องการเอาไปใช้กับ web อยู่แล้ว ดังนั้นเราไม่ต้องจัดการอะไรในตารางก็ได้ให้กินเวลาครับ แต่จะใช้หลักการประมาณนี้
.
.
จะเห็นว่าทั้งหมดนี้ไม่ต้องขยับหรือเปลี่ยนแปลงข้อมูลใน sheet เลย แถมเรายังได้ผลลัพธ์เป็น JSON เหมือนทำ API แบบง่ายๆได้ในตัวครับ
.
ตัวอย่างผลลัพธ์ที่ได้ (แบบ JSON)
.
https://script.google.com/macros/s/AKfycbymYb652s7RnKO7IaIcaC_l5eMTwqEU7xriadmKlMd8pgq-plaM/exec
.
ตัวอย่าง sheet (copy ไปลองได้ครับ)
.
https://docs.google.com/spreadsheets/d/1UAqFq4YQfF3ZxhTqaio5X0THLEJ55rWXXwJK7m5q8Ec/edit?usp=sharing