daily pastebin goal
78%
SHARE
TWEET

Google Apps Script: Weekly Report

charles_bickel Nov 13th, 2017 17 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Slice dates give double digit mm & dd
  2. function sliceit(x) {
  3.   thing = ("0" + (x)).slice(-2);
  4.   return thing
  5. }
  6.  
  7. // Get Monday
  8. function dateDiff() {
  9.   var day = d.getDay(),
  10.       diff = d.getDate() - day + (day == 0 ? -6:1); // adjust when day is sunday
  11.   return diff
  12. }
  13.  
  14. // Get date & create name of document
  15. function setName() {
  16.   d = new Date();
  17.   diff = dateDiff(d);
  18.   thatmonday = new Date(d.setDate(diff));
  19.   thatfriday = new Date(d.setDate(thatmonday.getDate() + 4));
  20.   newformat = 'Completed ' + thatmonday.getFullYear() + '_' + sliceit(thatmonday.getMonth() + 1) + '/' +
  21.     sliceit(thatmonday.getDate()) + ' - ' + sliceit(thatfriday.getMonth() + 1) + '/' + sliceit(thatfriday.getDate())
  22.   return newformat
  23. }
  24.  
  25. // Write each day of the week in document
  26. function writeDays(step) {
  27.   d = new Date();
  28.   var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
  29.   eachday = new Date(d.setDate(diff + step));
  30.   eachdayformat = days[eachday.getDay()] + ' ' + sliceit((eachday.getMonth() + 1)) + '/' + sliceit(eachday.getDate()) + ':';
  31.   return eachdayformat
  32. }
  33.  
  34. // Move from root to work folder
  35. function moveFile(file, folder) {
  36.   folder.addFile(file);
  37.   DriveApp.getRootFolder().removeFile(file);
  38. }
  39.  
  40. function myFunction() {
  41.   // Create document
  42.   var folder = DriveApp.getFolderById("EnterYourFolderIDNumberHere");
  43.   var doc = DocumentApp.create(setName());
  44.   var file = DriveApp.getFileById(doc.getId());
  45.   var body = doc.getBody();
  46.  
  47.   // For loop write days of the week and assign bullet points
  48.   for (step = 0; step < 5; step++){
  49.     body.appendParagraph(writeDays(step));
  50.     body.appendListItem("\n").setGlyphType(DocumentApp.GlyphType.BULLET);
  51.   }
  52.  
  53.   moveFile(file, folder);
  54. }
RAW Paste Data
Top