Guest User

Untitled

a guest
Feb 19th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. function endOfWK_1 () {
  2.  
  3. //This script converts all formulas to values in the currently displayed sheet, then converts the currently displayed sheet to a pdf, then emails the
  4. pdf as an attachment to the addresses shown in cell B17 in the "Email" sheet.
  5.  
  6. //Replace all formulas in range "WK 1!A6:A29" with values
  7.  
  8. var ss = SpreadsheetApp.getActiveSpreadsheet();
  9. var sheet = ss.getSheetByName('WK 1');
  10. var range = sheet.getRange("WK 1!A6:A29");
  11.  
  12. range.copyTo(range, {contentsOnly: true});
  13.  
  14. // FOR WK1 ONLY!!!
  15.  
  16. // Set the Active Spreadsheet so we don't forget
  17. var originalSpreadsheet = SpreadsheetApp.getActive();
  18.  
  19. // Set the message to attach to the email.
  20. var message = "Please see attached.";
  21.  
  22. // Get Dates from Email!B5
  23. var period = originalSpreadsheet.getRange("Email!B5").getValues();
  24.  
  25. // Construct the Subject Line
  26. var subject = period;
  27.  
  28. // Get contact details from "Email" sheet and construct To: Header
  29. var contacts = originalSpreadsheet.getSheetByName("Email");
  30. var numRows = contacts.getLastRow();
  31. var emailTo = contacts.getRange(17, 2, numRows, 1).getValues();
  32.  
  33. // Create a new Spreadsheet and copy the current sheet into it.
  34. var newSpreadsheet = SpreadsheetApp.create("Spreadsheet to export");
  35. var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  36. var projectname = SpreadsheetApp.getActiveSpreadsheet();
  37. sheet = originalSpreadsheet.getActiveSheet();
  38. sheet.copyTo(newSpreadsheet);
  39.  
  40. // Find and delete the default "Sheet1"
  41. newSpreadsheet.getSheetByName('Sheet1').activate();
  42. newSpreadsheet.deleteActiveSheet();
  43.  
  44. // Create the PDF, currently called "Tracking Sheet.pdf"
  45. var pdf = DriveApp.getFileById(newSpreadsheet.getId()).getAs('application/pdf').getBytes();
  46. var attach = {fileName:'Tracking Sheet.pdf',content:pdf, mimeType:'application/pdf'};
  47.  
  48. // Send the freshly constructed email
  49. MailApp.sendEmail(emailTo, subject, message, {attachments:[attach]});
  50.  
  51. // Delete the sheet that was created
  52. DriveApp.getFileById(newSpreadsheet.getId()).setTrashed(true);
  53.  
  54. // Write the date and time that the script ran
  55. var date = sheet.getRange('Statistics!A1').getValues();
  56. SpreadsheetApp.getActiveSheet().getRange('Analysis!E5').setValues(date);
  57.  
  58. }
Add Comment
Please, Sign In to add comment