返回未来的工作日,不包括假期

时间:2017-08-07 12:38:39

标签: javascript google-apps-script

我应该先说我对脚本一无所知。我发现这个在线脚本符合我的需求,所以我能够为我的项目重新设计它。无论如何,此脚本会提交Google表单提交,填充Google文档模板,该模板被复制,转换为PDF,并放置在我的Google云端硬盘上的特定文件夹中。

所以我的问题是,我有一个简单的行,在脚本运行时拉出当前日期,但我还需要一些可以计算当前日期加上5个工作日的代码(应该排除周末),但我还需要它排除定义的假期。任何帮助将不胜感激。



// 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);
}




1 个答案:

答案 0 :(得分:0)

您可以使用以下函数获取未来日期,该日期不包括周末以及数组中声明的任何假日。

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;
}