Created
July 1, 2020 15:16
-
-
Save nbouliol/cf872d81cbbcb72eafa51b8cc3c2b213 to your computer and use it in GitHub Desktop.
This file contains 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
function getString(from, to) { | |
let f = new Intl.DateTimeFormat('fr', {hour:'numeric',minute:'numeric'}).format(from) | |
let t = new Intl.DateTimeFormat('fr', {hour:'numeric',minute:'numeric'}).format(to) | |
return `${f} à ${t}` | |
} | |
/** | |
* Generes les horaires à partir d'une heure de début de journée | |
* | |
* @param {number} start_hour L'heure de début. ex : 9 | |
* @param {number} start_min La minute de début. ex : 30 | |
* @param {number} total_week Le nombre d'heure de la semaine. ex : 35 | |
* @return La plage horaires pour les jours de la semaine | |
* @customfunction | |
*/ | |
function timeSplitStart(start_hour, start_min, total_week) { | |
if (total_week % 5 !== 0 && total_week !== 48) throw 'cannot divide by 5'; | |
let from = new Date(); | |
from.setHours(start_hour) | |
from.setMinutes(start_min) | |
from.setSeconds(0) | |
let to = new Date() | |
if (total_week !== 48) { | |
let hours = total_week / 5; | |
to.setTime(from.getTime() + (hours*60*60*1000)) | |
} else { | |
to.setTime(from.getTime() + (9*60*60*1000) + (30*60*1000)) | |
} | |
// let from = new Intl.DateTimeFormat('fr', {hour:'numeric',minute:'numeric'}).format(date) | |
// let to = new Intl.DateTimeFormat('fr', {hour:'numeric',minute:'numeric'}).format(d) | |
// const string = `${from} à ${to}` | |
let string = getString(from, to) | |
var cellValues = [[]]; | |
for (let i = 0; i < 5; i++) { | |
if (total_week === 48 && i === 4) { | |
to.setTime(from.getTime() + (10*60*60*1000)) | |
string = getString(from, to) | |
} | |
cellValues[0].push(string); | |
} | |
return cellValues | |
} | |
/** | |
* Generes les horaires à partir d'une heure de fin de journée | |
* | |
* @param {number} start_hour L'heure de fin. ex : 9 | |
* @param {number} start_min La minute de fin. ex : 30 | |
* @param {number} total_week Le nombre d'heure de la semaine. ex : 35 | |
* @return La plage horaires pour les jours de la semaine | |
* @customfunction | |
*/ | |
function timeSplitEnd(end_hour, end_min, total_week) { | |
if (total_week % 5 !== 0 && total_week !== 48) throw 'cannot divide by 5'; | |
// let hours = total_week / 5; | |
let to = new Date() | |
to.setHours(end_hour) | |
to.setMinutes(end_min) | |
to.setSeconds(0) | |
// let d = new Date() | |
// d.setTime(date.getTime() - (hours*60*60*1000)) | |
let from = new Date() | |
if (total_week !== 48) { | |
let hours = total_week / 5; | |
from.setTime(to.getTime() - (hours*60*60*1000)) | |
} else { | |
from.setTime(to.getTime() - (9*60*60*1000) - (30*60*1000)) | |
} | |
// let from = new Intl.DateTimeFormat('fr', {hour:'numeric',minute:'numeric'}).format(d) | |
// let to = new Intl.DateTimeFormat('fr', {hour:'numeric',minute:'numeric'}).format(date) | |
// const string = `${from} à ${to}` | |
let string = getString(from, to) | |
var cellValues = [[]]; | |
for (let i = 0; i < 5; i++) { | |
if (total_week === 48 && i === 4) { | |
from.setTime(to.getTime() - (10*60*60*1000)) | |
string = getString(from, to) | |
} | |
cellValues[0].push(string); | |
} | |
return cellValues | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment