在单个工作表而不是整个电子表格上运行Google脚本

布兰登

在我的电子表格中,我有2张Google表格与2张纸相关联。提交表单后,脚本便会执行并完成任务。但是,我只希望脚本基于单个工作表中的提交执行。就像现在一样,脚本在提交任何一种表单时执行。

我的两张纸是:作业提交和订单提交

有什么建议吗?

// Work Order


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

function addDates() {
    var date = new Date(); // your form date
    var holiday = ["09/04/2017","10/09/2017","11/23/2017","12/24/2017","12/25/2017","01/01/2018"]; //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 = holiday.indexOf(Utilities.formatDate(date, "EDT", "MM/dd/yyyy"));
                  if (check != 0 && check != 6  && holidayCheck == -1) {
                         counter++;
                    }
            }
        }
        Logger.log(date) //for this example will give 08/16/2017
    return date;
}

function createNewDoc(values) {
//Get information from form and set as variables
  var email_address = "";
  var job_name = values[1];
  var order_count = values[2];
  var order_form = values[7];
  var print_services = values[3];
  var priority = values[5];
  var notes = values[6];
  var formattedDate = Utilities.formatDate(new Date(), "EDT", "MM/dd/yyyy");
  var expirationDate = Utilities.formatDate(addDates(), "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('keyOrderCount', order_count);
   copyBody.replaceText('keyOrderForm', order_form);
   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 - " + job_name;
   var body    = "Here is the work order for " + job_name + ". Job is due " + expirationDate + ".";
   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);

   var newDocName = docName + ' for ' + job_name;
   return [copyId, newDocName];
}

function printGoogleDocument(copyId, docName) {  
  // For notes on ticket options see https://developers.google.com/cloud-print/docs/cdd?hl=en
  var ticket = {
    version: "1.0",
    print: {
      color: {
        type: "STANDARD_COLOR"
      },
      duplex: {
        type: "NO_DUPLEX"
      },
    }
  };

  var payload = {
    "printerid" : "",
    "content"   : copyId,
    "title"  : docName,
    "contentType" : "google.kix", // allows you to print google docs
    "ticket"    : JSON.stringify(ticket),
  };

  var response = UrlFetchApp.fetch('https://www.google.com/cloudprint/submit', {
    method: "POST",
    payload: payload,
    headers: {
      Authorization: 'Bearer ' + GoogleCloudPrint.getCloudPrintService().getAccessToken()
    },
    "muteHttpExceptions": true
  });

  // If successful, should show a job here: https://www.google.com/cloudprint/#jobs

  response = JSON.parse(response);
  if (response.success) {
    Logger.log("%s", response.message);
  } else {
    Logger.log("Error Code: %s %s", response.errorCode, response.message);
  }
  return response;
}

// When Form Gets submitted
function onFormSubmit(e) { 
  var values = e.values;
  var returnedDocValues = createNewDoc(values);
  var copyId = returnedDocValues[0];
  var docName= returnedDocValues[1];
  printGoogleDocument(copyId, docName);
}

编辑:我不确定如何做一个完整的或可验证的示例,因为它取决于表单提交。我不经常使用javascript,因此我仍在学习。

无论如何,我更新了onFormSubmit函数,但是,其他函数却无法执行。脚本不会创建文档,因此不会发送到Google云打印

// When Form Gets submitted
function onFormSubmit(e) { 
// Initialize
  var rng = e.range;
  var sheet = rng.getSheet();
  var name = sheet.getName();
// If the response was not submitted to the right sheet, exit.
  if(name != "Job Submission") return; 
  var values = e.values;
  var returnedDocValues = createNewDoc(values);
  var copyId = returnedDocValues[0];
  var docName= returnedDocValues[1];
  printGoogleDocument(copyId, docName);
}

布兰登

不遵循鲁宾的建议,这就是我最后得到的

function onFormSubmit(e) {
  // Initialize
  var name = e.range.getSheet().getName();
  // If the response was not submitted to the right sheet, exit.
  if (name != "Job Submission") return;
  var values = e.values;
  var returnedDocValues = createNewDoc(values);
  var copyId = returnedDocValues[0];
  var docName = returnedDocValues[1];
  printGoogleDocument(copyId, docName);
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何让 onEdit Trigger 专门在工作表上而不是整个电子表格上运行

如何在电子表格文件中的所有工作表上运行Google Apps脚本?

电子表格中所有工作表上的setActiveCell。Google Apps脚本

用于电子表格的Google脚本可从工作表上链接的PDF页面提取数据

跨电子表格中的特定工作表运行脚本

Google电子表格脚本

自动清除脚本 - 在所有工作表的 Google 电子表格中

使用Google脚本复制电子表格并为每个工作表设置权限

在Google时区(而不是电子表格时区)中使用Google Apps电子表格脚本的日期

Google脚本+电子表格+打开时将整个表格转换为大写

使用Google工作表脚本时,如何在不创建新工作表副本的情况下将电子表格的工作表复制到另一个电子表格中?

对整个列执行Google电子表格查询

如何在其他工作表的应用脚本中请求或获得Google电子表格访问权限?

如何将输入数据从Google电子表格侧边栏发送到工作表脚本功能?

将工作表复制到另一个电子表格[Google Apps脚本]

在单个电子表格熊猫中跨工作表分布的数据框列表

电子表格上的Google脚本-创建链接以通过表格修改答案

在一个电子表格上运行脚本以更改另一个电子表格上的值

Google电子表格:在活动工作表中插入数据透视表。

自动更新 Google 电子表格脚本

Google App脚本电子表格

使用Google电子表格脚本复制并粘贴

Google 电子表格脚本 - 搜索功能

语法错误:Google 电子表格脚本

如何在多个谷歌电子表格上运行独立脚本?

如何将现有的Google工作表插入Google电子表格?

我可以在以Google电子表格为界的Google脚本上执行getCursor()吗?

在同一电子表格中的不同工作表上更改信息

从ASP.NET写入Google工作表中的电子表格