0

I should preface by saying I know nothing about scripting. I found this script online that fit my needs, so I was able to re-purpose it for my project. Anyway, this script takes Google form submissions, populates a Google doc template, that template gets copied, converted to PDF, and placed in a specific folder on my Google Drive.

So my question is, I have a simple line that pulls the current date when the script gets run, but I also need some code that can calculate the current date plus 5 weekdays (which should exclude weekends), but I also need it to exclude defined holidays. Any help would be greatly appreciated.

// Work Order


// Get template from Google Docs and name it
var docTemplate = ""; // *** replace with your template ID ***
var docName = "Work Order";

// When Form Gets submitted
function onFormSubmit(e) {
  //Get information from form and set as variables
  var email_address = "";
  var job_name = e.values[1];
  var ship_to = e.values[11];
  var address = e.values[12];
  var order_count = e.values[7];
  var program = e.values[2];
  var workspace = e.values[3];
  var offer = e.values[4];
  var sort_1 = e.values[5];
  var sort_2 = e.values[6];
  var print_services = e.values[10];
  var priority = e.values[13];
  var notes = e.values[14];
  var formattedDate = Utilities.formatDate(new Date(), "EDT", "MM/dd/yyyy");

  // Get document template, copy it as a new temp doc, and save the Doc's id
  var copyId = DriveApp.getFileById(docTemplate)
    .makeCopy(docName + ' for ' + job_name)
    .getId();
  // Open the temporary document
  var copyDoc = DocumentApp.openById(copyId);
  // Get the document's body section
  var copyBody = copyDoc.getActiveSection();

  // Replace place holder keys,in our google doc template  
  copyBody.replaceText('keyJobName', job_name);
  copyBody.replaceText('keyShipTo', ship_to);
  copyBody.replaceText('keyAddress', address);
  copyBody.replaceText('keyOrderCount', order_count);
  copyBody.replaceText('keyProgram', program);
  copyBody.replaceText('keyWorkspace', workspace);
  copyBody.replaceText('keyOffer', offer);
  copyBody.replaceText('keySort1', sort_1);
  copyBody.replaceText('keySort2', sort_2);
  copyBody.replaceText('keyPrintServices', print_services);
  copyBody.replaceText('keyPriority', priority);
  copyBody.replaceText('keyNotes', notes);
  copyBody.replaceText('keyDate', formattedDate);
  copyBody.replaceText('keyDue', expirationDate);

  // Save and close the temporary document
  copyDoc.saveAndClose();

  // Convert temporary document to PDF by using the getAs blob conversion
  var pdf = DriveApp.getFileById(copyId).getAs("application/pdf");

  // Attach PDF and send the email
  var subject = "New Job Submission";
  var body = "Here is the work order for " + job_name + "";
  MailApp.sendEmail(email_address, subject, body, {
    htmlBody: body,
    attachments: pdf
  });

  // Move file to folder
  var file = DriveApp.getFileById(copyId);
  DriveApp.getFolderById("").addFile(file);
  file.getParents().next().removeFile(file);
}
Brandon
  • 93
  • 9
  • 3
    This is not a free code writing service. Where is your list of "defined holidays"? They're likely different in different places. What have you tried? – RobG Aug 07 '17 at 12:42
  • I've spent some time on these forums and Google's forums looking for similar problems, and while I've found some similarities, I just don't have the javascript knowledge to understand how to integrate it into my own project. A starting point would be US national holidays, but if I had a basic example of how to define holidays, I could add as necessary. Also, my question is pretty broad, so I'm not looking for custom code or anything. – Brandon Aug 07 '17 at 13:14
  • "*…my question is pretty broad*". Yes. You could search on [`[javascript] exclude holidays`](https://stackoverflow.com/search?q=%5Bjavascript%5D+exclude+holidays). – RobG Aug 07 '17 at 22:34
  • Fortunately somebody answered my question and hopefully it will help others as it's not dependent on a specific script and it's basic enough to be easily digestible for new users. – Brandon Aug 09 '17 at 14:26

1 Answers1

0

You can use the below function to get future date which excludes weekends and if any holiday declared in the array.

function addDates() {
    var date = new Date(); // yor form date
    var hodiday = ["08/09/2017","08/15/2017"]; //Define holiday dates in MM/dd/yyyy
    var days = 5; //No of days you want to add
    date.setDate(date.getDate());
    var counter = 0;
        if(days > 0 ){
            while (counter < days) {
                date.setDate(date.getDate() + 1 ); 
                var check = date.getDay(); 
                var holidayCheck = hodiday.indexOf(Utilities.formatDate(date, "GMT", "MM/dd/yyyy"));
                  if (check != 0 && check != 6  && holidayCheck == -1) {
                         counter++;
                    }
            }
        }
        Logger.log(date) //for this example will give 08/16/2017
    return date;
}
Ritesh Nair
  • 3,327
  • 1
  • 17
  • 24
  • 2
    Thanks for the quick and thorough response! I'm still figuring out how to integrate this into my code, but I'm learning, so I really appreciate the help. Edit: Worked perfect! Thanks. – Brandon Aug 07 '17 at 13:56
  • It's not a good idea to rely on the built–in parser, see [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – RobG Aug 07 '17 at 22:35
  • I thought about that, but I just figured the browser would convert to local time. If this raises a problem in the future I will definitely be back to troubleshoot. Feel free to post your fix to the function above. – Brandon Aug 09 '17 at 14:29