Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function generateTable() {
- var month = 8;
- var sheet = SpreadsheetApp.getActiveSheet();
- var lastRow = sheet.getDataRange().getLastRow();
- var fromRow = lastRow;
- if (lastRow != 1) {
- sheet.appendRow([' '])
- sheet.appendRow([' '])
- var fromRow = lastRow + 3
- }
- var tableLength = generateContent(month, fromRow, sheet)
- var toRow = fromRow + tableLength + 1;
- setDateNumbersFormat(fromRow, toRow, sheet);
- sheet.getRange(fromRow, 1, toRow - fromRow - 1, 7).setBorder(true, true, true, true, true, true)
- sheet.getRange(fromRow, 6, toRow - fromRow - 1).setFontWeight('bold').setFontLine('underline')
- sheet.getRange(fromRow + 1, 1, toRow - fromRow - 1, 7).setVerticalAlignment('middle').setHorizontalAlignment('center')
- }
- function isSunday(date) {
- return date.getDay() == 0;
- }
- function generateContent(month, firstRow, sheet) {
- var date = new Date(month +'/1/2018');
- var monthStr = Utilities.formatDate(date, Session.getScriptTimeZone(), "MMMM");
- sheet.appendRow([monthStr]);
- sheet.setRowHeight(firstRow, 50)
- sheet.getRange(firstRow, 1, 1, 7).merge()
- .setBackgroundRGB(183, 225, 205)
- .setFontSize(18)
- .setFontWeight('bold')
- .setVerticalAlignment('middle');
- var count = 1;
- var skippedRowsCount = 0;
- var row = 0;
- var lastDayOfMonthInt;
- while (true) {
- var next = new Date(month +'/' + count + '/2018');
- if (next.getMonth() != month - 1) {
- lastDayOfMonthInt = count - 1
- break;
- }
- row = firstRow + count + skippedRowsCount;
- sheet.appendRow([next, '=TEXT(WEEKDAY(A' + row + '), "dddd")', '', '', '', '=D' + row + '-C' + row + '-E' + row]);
- var dateCells = sheet.getRange(row, 1, 1, 2)
- dateCells
- .setBackgroundRGB(183, 225, 205)
- .setFontWeight('bold')
- if (next.getDay() == 0 || next.getDay() == 6) {
- dateCells.setFontColor('#cc0000')
- }
- if (isSunday(next)){
- appendTotalForWeekRow(firstRow, row, sheet);
- skippedRowsCount++;
- }
- count++;
- }
- return count + skippedRowsCount + appendTotalForLastWeekRow(month, lastDayOfMonthInt, row, sheet);
- }
- function appendTotalForWeekRow(firstRow, row, sheet) {
- var mondayRow = row - 6;
- if (mondayRow < firstRow + 1) {
- mondayRow = firstRow + 1;
- }
- var workingDaysCount = row - mondayRow - 1
- sheet.appendRow(['', '', '', '', '', '=SUM(F' + mondayRow + ':F' + row + ')', '=F' + (row + 1) + '- TIME(4, 0, 0) * ' + workingDaysCount])
- sheet.getRange(row + 1, 7)
- .setFontColor('#cc0000')
- .setFontWeight('bold')
- sheet.setRowHeight(row + 1, 35)
- }
- function appendTotalForLastWeekRow(month, lastDayOfMonthInt, lastRow, sheet) {
- if (!isSunday(new Date(month +'/' + lastDayOfMonthInt + '/2018'))) {
- var rowBeforeMonday = lastRow - 1
- while (true) {
- rowBeforeMonday--;
- lastDayOfMonthInt--;
- if (new Date(month +'/' + lastDayOfMonthInt + '/2018').getDay() == 1) {
- break
- }
- }
- if (lastRow - rowBeforeMonday == 6){
- rowBeforeMonday -= 1
- }
- else {
- rowBeforeMonday -= 2
- }
- appendTotalForWeekRow(rowBeforeMonday, lastRow, sheet)
- return 1
- }
- return 0
- }
- function setDateNumbersFormat(fromRow, toRow, sheet) {
- sheet.getRange(fromRow, 2, toRow - fromRow, 6).setNumberFormat('[h]:mm')
- sheet.getRange(fromRow, 1, toRow - fromRow).setNumberFormat('dd.MM')
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement