Skip to content

Instantly share code, notes, and snippets.

@sunfmin
Created May 27, 2011 02:23
Show Gist options
  • Save sunfmin/994525 to your computer and use it in GitHub Desktop.
Save sunfmin/994525 to your computer and use it in GitHub Desktop.
function CalculateFromOvertimeCalendars() {
var file = SpreadsheetApp.getActiveSpreadsheet();
var settingSheet = file.getSheetByName("Setting");
var settingRange = settingSheet.getRange("A2:C50");
var month = parseInt(settingSheet.getRange("G2").getValue());
var thisMonth = new Date();
var sheetName = thisMonth.getFullYear() + "-" + (month);
var startTime = new Date(thisMonth.getFullYear(), month - 1, 1, 0, 0, 0);
var endTime = new Date(thisMonth.getFullYear(), month, 1, 0, 0, 0);
var sheet = file.getSheetByName(sheetName);
if(sheet == null){
sheet = file.insertSheet(sheetName);
}
sheet.clear();
sheet.getRange(2, 2).setValue("Name");
sheet.getRange(2, 3).setValue("Overtime Hours");
sheet.getRange(2, 4).setValue("Total(RMB)");
var lastRow = 1;
var summaryRow = 2;
var convertToChineseTime = function(d){
utc = d.getTime() + (d.getTimezoneOffset() * 60000);
return new Date(utc + (3600000*(-8)));
}
var weekdays = ["SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"];
var settingValues = settingRange.getValues();
for(var i=0; i < settingValues.length; i++){
var name = settingValues[i][0];
var calendarName = settingValues[i][1];
var hourRate = settingValues[i][2];
if(name.length == 0){
continue;
}
var cal = CalendarApp.getCalendarsByName(calendarName);
var allEvents = cal[0].getEvents(startTime, endTime);
lastRow = lastRow + 2;
var nameRange = sheet.getRange(lastRow, 6)
nameRange.setValue(name);
nameRange.setBackgroundColor("#000000");
nameRange.setFontColor("#ffffff");
var totalHoursForUser = 0;
for(var j=0; j < allEvents.length; j++){
lastRow = lastRow + 1;
var eventStartTime = allEvents[j].getStartTime();
var hours = (allEvents[j].getEndTime() - eventStartTime)/(60*60*1000);
hours = Math.round(hours * 100) / 100;
totalHoursForUser = totalHoursForUser + hours;
sheet.getRange(lastRow, 6).setValue(hours);
var chineseEventStartTime = convertToChineseTime(eventStartTime);
sheet.getRange(lastRow, 7).setValue(chineseEventStartTime);
sheet.getRange(lastRow, 8).setValue(convertToChineseTime(allEvents[j].getEndTime()));
sheet.getRange(lastRow, 9).setValue(allEvents[j].getTitle());
}
summaryRow = summaryRow + 1;
sheet.getRange(summaryRow, 2).setValue(name);
sheet.getRange(summaryRow, 3).setValue(totalHoursForUser);
sheet.getRange(summaryRow, 4).setValue(Math.floor(totalHoursForUser * hourRate * 1.5));
}
summaryRow = summaryRow + 1;
sheet.getRange(summaryRow, 4).setFormula('=SUM(D3:D' +(summaryRow-1)+ ')');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment