Advertisement
KasimirRomer

Convert Sheet to To-Do-List - Google App Script - MON

Oct 6th, 2022 (edited)
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. To-Do-List creator by Kasimir ROMER (as part of MON1 at EC Marseille)
  3. It creates a To-Do-List from a simple sheet
  4. */
  5.  
  6. function createToDosAndChart() {
  7.   // get different parts of the table and save them in variables
  8.   var currentSpreadsheet = SpreadsheetApp.getActiveSpreadsheet(); // returns the current spreadsheet
  9.   //var activeSheet = currentSpreadsheet.getActiveSheet(); // returns the current sheet
  10.   var activeSheet = currentSpreadsheet.getSheetByName('input'); // returns the sheet with the name 'input'
  11.  
  12.   var wholeTable = activeSheet.getDataRange(); // returns the whole table as range
  13.   var dataSection = wholeTable.offset(1,0); //returns the wholeTable-DataRange without headers
  14.  
  15.   var lastColumn = wholeTable.getLastColumn();
  16.   var lastRow = wholeTable.getLastRow();
  17.  
  18.   // sort the list by date
  19.   dataSection.sort(2); // 2 = 2nd column which includes the date
  20.  
  21.   // -----------------------
  22.   // -- Create checkboxes --
  23.   // -----------------------
  24.   doneColumnRange = activeSheet.getRange(2,3,lastRow-1, 1); //get range that includes the done values (done/not done)
  25.   doneValues = doneColumnRange.getValues(); // get the values from this range (oui/non)
  26.  
  27.   // replace french words with corresponding boolean
  28.   for (i in doneValues) {
  29.     if (doneValues[i][0] == "oui") doneValues[i][0] = true;
  30.     else if (doneValues[i][0] == "non") doneValues[i][0] = false;
  31.   }
  32.   doneColumnRange.setValues(doneValues); // update the range with the booleans
  33.   doneColumnRange.insertCheckboxes(); // convert the booleans to checkboxes
  34.  
  35.   // iterate through all rows. If done is checked, strikethrough the line and grey it out
  36.   var data = dataSection.getValues(); // get the values from the whole table range
  37.   data.forEach(function (row, index) {
  38.     var currentRowRange = wholeTable.offset(index+1, 0, 1); // offset returns a new range that contains the current row
  39.     if (row[2] == true) {
  40.       currentRowRange.setFontLine('line-through'); // the striktethrough is applied to this range
  41.       currentRowRange.setFontColor('lightgray'); // text gets grey
  42.     } else {
  43.       currentRowRange.setFontLine(null); // reset strikethrough
  44.       currentRowRange.setFontColor(null); // reset font color
  45.     };
  46.   })
  47.  
  48.   // ------------------------ \\
  49.   // START of date operations \\
  50.   // ------------------------ \\
  51.  
  52.   var datesAndDoneColumnRange = activeSheet.getRange(2,2,lastRow-1, 2); //get range that includes the dates and done values (done/not done)
  53.   var dateAndDoneValues = datesAndDoneColumnRange.getValues(); // get the values from this range -> Array[Date][done]
  54.  
  55.   // create new sheet to store temporary data
  56.   var temporarySheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('temporary-data');
  57.   temporarySheet.clear(); // clear the temporary sheet
  58.   temporarySheet.getRange(1,1,1,3).setValues([["Date", "Done", "Not done"]]); // create header in the temporary sheet
  59.  
  60.   var firstDay = new Date(dateAndDoneValues[0][0]); // get first array element
  61.   var lastDate = new Date(dateAndDoneValues.slice(-1)[0][0]); //get last array element
  62.  
  63.   // iterate through all dates in the range: https://stackoverflow.com/questions/4345045/loop-through-a-date-range-with-javascript
  64.   var allDates = []; // array where all dates will be saved
  65.   for (let d = firstDay; d <= lastDate; d.setDate(d.getDate() + 1)) {
  66.     let newDate = new Date(d);
  67.     allDates.push([newDate.toLocaleDateString('fr-FR'),0,0]);
  68.   }
  69.   temporarySheet.getRange(2, 1, allDates.length, 3).setValues(allDates); // write the array with all dates to the temporary sheet
  70.  
  71.   // iterate throug dateAndDoneValues and read them
  72.   dateAndDoneValues.forEach(function(row, index) {
  73.     let currentDate = new Date(row[0]);
  74.     let currentDoneState = row[1];
  75.     let currentRow = temporarySheet.createTextFinder(currentDate.toLocaleDateString('fr-FR')).findNext().getRow();
  76.  
  77.     if (currentDoneState == true) {
  78.       temporarySheet.getRange(currentRow, 2).setValue(temporarySheet.getRange(currentRow, 2).getValue() + 1); // increase value of "done" by 1
  79.     } else if (currentDoneState == false) {
  80.       temporarySheet.getRange(currentRow, 3).setValue(temporarySheet.getRange(currentRow, 3).getValue() + 1); // increase value of "not done" by 1
  81.     } else {
  82.       Logger.log('Impossible state, currentDoneState is neither true nor false'); // this case should never happen
  83.     }
  84.   });
  85.  
  86.   // create a chart if there is none yet
  87.   if (activeSheet.getCharts()[0] == null) { //
  88.     var chartBuilder = activeSheet.newChart();
  89.     chartBuilder.addRange(temporarySheet.getDataRange())
  90.       .setChartType(Charts.ChartType.LINE)
  91.       .setOption('title', 'Tasks per deadline')
  92.       .setNumHeaders(1)
  93.       .setPosition(1, lastColumn + 2, 0, 0);
  94.     activeSheet.insertChart(chartBuilder.build());
  95.   }
  96. }
  97.  
  98. function convertTableToListOfTasks(){
  99.   // get different parts of the table and save them in variables
  100.  
  101.   var currentSpreadsheet = SpreadsheetApp.getActiveSpreadsheet(); // returns the current spreadsheet
  102.   var activeSheet = currentSpreadsheet.getSheetByName('input'); // returns the current sheet
  103.   var headers = activeSheet.getRange('A1:C1'); // returns the header row
  104.  
  105.   var wholeTable = activeSheet.getDataRange(); // returns the whole table as range
  106.  
  107.   // format header
  108.   headers.setFontWeight('bold');
  109.   headers.setBackground('lightblue')
  110.  
  111.   // add filter
  112.   if (activeSheet.getFilter() == null) wholeTable.createFilter();
  113. }
  114.  
  115. // Code from the YouTube-Tutorial to get used to Google Apps Script, not relevant for the To-Do-List
  116. function formatReport() {
  117.   var sheet = SpreadsheetApp.getActiveSpreadsheet();
  118.   var headers = sheet.getRange('A1:C1');
  119.   var table = sheet.getDataRange();
  120.  
  121.   headers.setFontWeight('bold');
  122.   headers.setFontColor('white');
  123.   headers.setBackground('blue');
  124.  
  125.   table.setFontFamily('Roboto');
  126.   table.setHorizontalAlignment('center');
  127.   table.setBorder(true, true, true, true,false, true, 'blue', SpreadsheetApp.BorderStyle.SOLID);
  128.   table.createFilter();
  129.   Logger.log(SpreadsheetApp.getActiveSheet().getName());
  130. }
  131.  
  132. function onOpen() {
  133.   var ui = SpreadsheetApp.getUi();
  134.   ui.createMenu('Kasis Skripts')
  135.     //.addItem('Format List', 'formatReport')
  136.     .addItem('Create ToDo', 'convertTableToListOfTasks')
  137.     .addItem('Reload To-Dos and Stats', 'createToDosAndChart')
  138.     .addToUi();
  139. }
  140.  
  141. function onEdit(e) {
  142.   createToDosAndChart();
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement