Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 3.40 KB | None | 0 0
  1. function generateTable() {
  2.   var month = 8;
  3.   var sheet = SpreadsheetApp.getActiveSheet();
  4.   var lastRow = sheet.getDataRange().getLastRow();
  5.   var fromRow = lastRow;
  6.   if (lastRow != 1) {
  7.      sheet.appendRow([' '])
  8.      sheet.appendRow([' '])
  9.      var fromRow = lastRow + 3
  10.   }
  11.  
  12.   var tableLength = generateContent(month, fromRow, sheet)
  13.  
  14.   var toRow = fromRow + tableLength + 1;
  15.   setDateNumbersFormat(fromRow, toRow, sheet);
  16.   sheet.getRange(fromRow, 1, toRow - fromRow - 1, 7).setBorder(true, true, true, true, true, true)
  17.   sheet.getRange(fromRow, 6, toRow - fromRow - 1).setFontWeight('bold').setFontLine('underline')
  18.   sheet.getRange(fromRow + 1, 1, toRow - fromRow - 1, 7).setVerticalAlignment('middle').setHorizontalAlignment('center')
  19. }
  20.  
  21. function isSunday(date) {
  22.   return date.getDay() == 0;
  23. }
  24.  
  25. function generateContent(month, firstRow, sheet) {
  26.   var date =  new Date(month +'/1/2018');
  27.   var monthStr = Utilities.formatDate(date, Session.getScriptTimeZone(), "MMMM");
  28.  
  29.   sheet.appendRow([monthStr]);
  30.   sheet.setRowHeight(firstRow, 50)
  31.   sheet.getRange(firstRow, 1, 1, 7).merge()
  32.     .setBackgroundRGB(183, 225, 205)
  33.     .setFontSize(18)
  34.     .setFontWeight('bold')
  35.     .setVerticalAlignment('middle');
  36.  
  37.   var count = 1;
  38.   var skippedRowsCount = 0;
  39.   var row = 0;
  40.   var lastDayOfMonthInt;
  41.   while (true) {
  42.     var next = new Date(month +'/' + count + '/2018');
  43.     if (next.getMonth() != month - 1) {
  44.       lastDayOfMonthInt = count - 1
  45.       break;
  46.     }      
  47.     row = firstRow + count + skippedRowsCount;
  48.     sheet.appendRow([next, '=TEXT(WEEKDAY(A' + row + '), "dddd")', '', '', '', '=D' + row + '-C' + row + '-E' + row]);
  49.    
  50.     var dateCells = sheet.getRange(row, 1, 1, 2)
  51.     dateCells
  52.       .setBackgroundRGB(183, 225, 205)
  53.       .setFontWeight('bold')
  54.    
  55.     if (next.getDay() == 0 || next.getDay() == 6) {
  56.       dateCells.setFontColor('#cc0000')
  57.     }
  58.    
  59.     if (isSunday(next)){
  60.       appendTotalForWeekRow(firstRow, row, sheet);
  61.       skippedRowsCount++;
  62.     }    
  63.     count++;
  64.   }
  65.   return count + skippedRowsCount + appendTotalForLastWeekRow(month, lastDayOfMonthInt, row, sheet);
  66. }
  67.  
  68. function appendTotalForWeekRow(firstRow, row, sheet) {
  69.    var mondayRow = row - 6;
  70.    if (mondayRow < firstRow + 1) {
  71.      mondayRow = firstRow + 1;
  72.    }
  73.    var workingDaysCount = row - mondayRow - 1
  74.    sheet.appendRow(['', '', '', '', '', '=SUM(F' + mondayRow + ':F' + row + ')', '=F' + (row + 1) + '- TIME(4, 0, 0) * ' +  workingDaysCount])
  75.    sheet.getRange(row + 1, 7)
  76.       .setFontColor('#cc0000')
  77.       .setFontWeight('bold')
  78.    sheet.setRowHeight(row + 1, 35)
  79. }
  80.  
  81. function appendTotalForLastWeekRow(month, lastDayOfMonthInt, lastRow, sheet) {
  82.   if (!isSunday(new Date(month +'/' + lastDayOfMonthInt + '/2018'))) {
  83.     var rowBeforeMonday = lastRow - 1
  84.     while (true) {
  85.       rowBeforeMonday--;
  86.       lastDayOfMonthInt--;
  87.       if (new Date(month +'/' + lastDayOfMonthInt + '/2018').getDay() == 1) {
  88.         break
  89.       }      
  90.     }
  91.     if (lastRow - rowBeforeMonday == 6){
  92.       rowBeforeMonday -= 1
  93.     }
  94.     else {
  95.       rowBeforeMonday -= 2
  96.     }
  97.     appendTotalForWeekRow(rowBeforeMonday, lastRow, sheet)
  98.     return 1
  99.   }
  100.   return 0
  101. }
  102.  
  103. function setDateNumbersFormat(fromRow, toRow, sheet) {
  104.   sheet.getRange(fromRow, 2, toRow - fromRow, 6).setNumberFormat('[h]:mm')
  105.   sheet.getRange(fromRow, 1, toRow - fromRow).setNumberFormat('dd.MM')
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement