Skip to content

Instantly share code, notes, and snippets.

@LordShedy
Last active March 8, 2019 08:30
Show Gist options
  • Save LordShedy/bff0111441e4080468a3569dafd291e4 to your computer and use it in GitHub Desktop.
Save LordShedy/bff0111441e4080468a3569dafd291e4 to your computer and use it in GitHub Desktop.
Kód pro aplikaci Atletický záznamníku
function train() {
/* an instance of the class Ui allowing promts and stuff */
const ui = SpreadsheetApp.getUi();
/* main spreadsheet */
const atletickyZaznamnikSheet = SpreadsheetApp.openById("1tIvDM5W5DvVAf0a9QwqYzBUWX8j4JBujvfwr3Wlt2NQ");
if (SpreadsheetApp.getActiveSpreadsheet().getSheetName() != "atleti na tréninku") return;
/* (sub)sheets */
const atletiNaTreninkuSheet = atletickyZaznamnikSheet.getSheetByName("atleti na tréninku");
const statistikyAtletuSheet = atletickyZaznamnikSheet.getSheetByName("statistiky atletů");
/* cell with a date */
const date = atletickyZaznamnikSheet.getRange("B3");
/* cell with a checkmark */
const checked = atletickyZaznamnikSheet.getRange("E3");
/* A6 B6 C6 D6 */
/* … … … … */
/* A103 B106 C106 D106 */
const atleti = atletickyZaznamnikSheet.getRange("B6:D106");
const atletiAttend = atletickyZaznamnikSheet.getRange("D6:D106")
const atletiValues = atleti.getValues();
const atletiRows = atleti.getNumRows();
/* A3 B3 C3 D3 */
/* … … … … */
/* A103 B103 C103 D103 */
const statistiky = statistikyAtletuSheet.getRange("A3:D103");
const statistikyValues = statistiky.getValues();
const statistikyRows = statistiky.getNumRows();
/* unless a checkbox is checked */
if (checked.getValue() === false) return null;
/* date cannot be empty, inform the user and clean checkbox */
if (date.getValue() === ""){
ui.alert('Varování','U uvedeného tréninku chybí datum.\nJe třeba vyplnit řádné datum, než odešlete údaje o tréninku.', ui.ButtonSet.OK);
return checked.setValue(false);
}
/* dialog to prevent accidental checking of the checkbox */
const result = ui.alert('Prosím potvrďte!', 'Opravdu chcete zaznamenat trénink?', ui.ButtonSet.YES_NO);
/* if user desires not to finish action, clean the checkbox */
if (result != ui.Button.YES) return checked.setValue(false)
else if (result == ui.Button.YES){
for (var i = 0; i < atletiRows; i++){
var atlet = atletiValues[i];
if (!atlet[2]) continue;
else if (atlet[2]) {
for (var j = 0; j < statistikyRows;j++){
var statistika = statistikyValues[j];
if (statistika[0] === atlet[0]){
Logger.log(statistika);
statistika[2]++;
statistika[3] = date.getValue();
break;
}
}
}
}
statistiky.setValues(statistikyValues);
checked.setValue(false);
atletiAttend.setValue(false);
date.setValue("");
ui.alert('Úspěch!','Váš záznam byl úspěšný.',ui.ButtonSet.OK_Cancel);
}
}
function race() {
/* an instance of the class Ui allowing promts and stuff */
const ui = SpreadsheetApp.getUi();
/* main spreadsheet */
const atletickyZaznamnikSheet = SpreadsheetApp.openById("1tIvDM5W5DvVAf0a9QwqYzBUWX8j4JBujvfwr3Wlt2NQ");
if (SpreadsheetApp.getActiveSpreadsheet().getSheetName() != "trenéři na závodech/trénincích") return;
/* (sub)sheets */
const treneriNaZavodechSheet = atletickyZaznamnikSheet.getSheetByName("trenéři na závodech/trénincích");
const statistikyTreneruSheet = atletickyZaznamnikSheet.getSheetByName("statistiky trenérů");
/* cell with a date */
const raceName = treneriNaZavodechSheet.getRange("C3");
/* cell with a date */
const date = treneriNaZavodechSheet.getRange("C5");
/* cell with a checkmark */
const checked = treneriNaZavodechSheet.getRange("C7");
/* A10 B10 C10 */
/* … … … */
/* A29 B29 C29 */
const treneri = treneriNaZavodechSheet.getRange("A10:C29");
const treneriAttend = treneriNaZavodechSheet.getRange("B10:B29");
const treneriHours = treneriNaZavodechSheet.getRange("C10:C29");
const treneriValues = treneri.getValues();
const treneriRows = treneri.getNumRows();
/* A3 B3 C3 D3 */
/* … … … … */
/* A103 B103 C103 D103 */
const statistiky = statistikyTreneruSheet.getRange("A3:D22");
const statistikyValues = statistiky.getValues();
const statistikyRows = statistiky.getNumRows();
/* unless a checkbox is checked */
if (checked.getValue() === false) return null;
/* date cannot be empty, inform the user and clean checkbox */
if (date.getValue() === ""){
ui.alert('Varování','U uvedeného závodu/tréninku vám chybí datum.\nJe třeba vyplnit řádné datum, než odešlete údaje o závodě/tréninku.', ui.ButtonSet.OK);
return checked.setValue(false);
}
/* date cannot be empty, inform the user and clean checkbox */
if (raceName.getValue() === ""){
ui.alert('Varování','U uvedeného závodu/tréninku vám název.\nJe třeba vyplnit nějaký název, než odešlete údaje o závodě/tréninku.', ui.ButtonSet.OK);
return checked.setValue(false);
}
/* dialog to prevent accidental checking of the checkbox */
const result = ui.alert('Prosím potvrďte!', 'Opravdu chcete zaznamenat trénink?', ui.ButtonSet.YES_NO);
/* if user desires not to finish action, clean the checkbox */
if (result != ui.Button.YES) return checked.setValue(false)
else if (result == ui.Button.YES) {
for (var i = 0; i < treneriRows; i++){
var trener = treneriValues[i];
if (!trener[1]) continue;
else if (trener[1]) {
for (var j = 0; j < statistikyRows;j++){
var statistika = statistikyValues[j];
if (statistika[0] === trener[0]){
Logger.log(trener)
statistika[1] += trener[2];
statistika[2] = date.getValue();
statistika[3] = raceName.getValue();
break;
}
}
}
}
statistiky.setValues(statistikyValues);
checked.setValue(false);
treneriAttend.setValue(false);
treneriHours.setValue("");
raceName.setValue("");
date.setValue("");
ui.alert('Úspěch!','Váš záznam byl úspěšný.',ui.ButtonSet.OK);
}
}
function erase(g){
/* an instance of the class Ui allowing promts and stuff */
const ui = SpreadsheetApp.getUi();
/* main spreadsheet */
const atletickyZaznamnikSheet = SpreadsheetApp.openById("1tIvDM5W5DvVAf0a9QwqYzBUWX8j4JBujvfwr3Wlt2NQ");
if (SpreadsheetApp.getActiveSpreadsheet().getSheetName() != "statistiky trenérů") return;
const statistikyTreneruSheet = atletickyZaznamnikSheet.getSheetByName("statistiky trenérů");
const noveObdobi = statistikyTreneruSheet.getRange("H3:H22");
const noveObdobiValues = noveObdobi.getValues();
const hoursTrained = statistikyTreneruSheet.getRange("B3:B22");
const hoursTrainedValues = hoursTrained.getValues();
const counterSince = statistikyTreneruSheet.getRange("E3:E22");
const counterSinceValues = counterSince.getValues();
var huh = 0;
Logger.log(noveObdobiValues);
Logger.log(hoursTrainedValues);
for (var i = 0; i < noveObdobiValues.length; i++) {
var noveObdobiValue = noveObdobiValues[i][0];
if (noveObdobiValue == true) huh++;
}
if (huh > 0){
const result = ui.alert(
'Prosím potvrďte!',
'Opravdu chcete anulovat hodiny tohoto trenéra?',
ui.ButtonSet.YES_NO);
if (result == ui.Button.YES){
for (var i = 0; i < noveObdobiValues.length; i++) {
var noveObdobiValue = noveObdobiValues[i][0];
if (noveObdobiValue == true){
hoursTrainedValues[i][0] = 0;
noveObdobiValues[i][0] = false;
counterSinceValues[i][0] = new Date();
}
}
noveObdobi.setValues(noveObdobiValues);
hoursTrained.setValues(hoursTrainedValues);
counterSince.setValues(counterSinceValues);
}
}
}
function athletsStats() {
/* main spreadsheet and sheet */
const sheet = SpreadsheetApp.openById("1tIvDM5W5DvVAf0a9QwqYzBUWX8j4JBujvfwr3Wlt2NQ").getSheetByName("statistiky atletů");
if (SpreadsheetApp.getActiveSpreadsheet().getSheetName() != "statistiky atletů") return;
const currentYear = sheet.getRange("E1").getValue();
const newYear = new Date().getYear();
const data = sheet.getRange("C3:D100");
if (currentYear != newYear){
data.setValue("");
sheet.getRange("E1").setValue(newYear);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment