簡體   English   中英

函數未使用定義的變量執行

[英]Function not executing with defined variables

該項目的要點是:我有在Google文檔中填充的Google Form答案,我們將此文檔稱為模板。 模板已復制,因此我永遠不會覆蓋原始模板。 該副本將轉換為PDF,發送至電子郵件,然后移至我的雲端硬盤上的特定文件夾。 每次提交表單時,此功能都會完美無缺地發生,並在提交時觸發。 我的下一個功能應該將復制的文檔發送到我的Google雲打印,但是我在編寫代碼方面遇到了麻煩。 我已經准備好要在“表單提交”上打印文檔了,但是我必須專門定義文檔的ID。 不幸的是,該ID並不是一成不變的,因為每次提交時都會創建一個新文檔。 這是我的完整代碼減去所有敏感信息:

 // Work Order // Get template from Google Docs and name it var docTemplate = ""; // *** replace with your template ID *** var docName = "Work Order"; 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 ship_to = values[11]; var address = values[12]; var order_count = values[7]; var program = values[2]; var workspace = values[3]; var offer = values[4]; var sort_1 = values[5]; var sort_2 = values[6]; var image_services = values[9]; var print_services = values[10]; var priority = values[13]; var notes = values[14]; 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('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('keyImageServices', image_services); 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); } 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; createNewDoc(values); printGoogleDocument(copyId, docName); } 

從'createNewDoc(values);'返回新文檔 通過將其添加到createNewDoc()函數的末尾(緊接在右括號之前)來進行更改:

//Starting at the code here
// Move file to folder
var file = DriveApp.getFileById(copyId);
DriveApp.getFolderById("").addFile(file);
file.getParents().next().removeFile(file);

//Add this
var newDocName = docName + ' for ' + job_name;
return [file, newDocName];
//To this point

}

然后如下所示更改onFormSubmit()函數:

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

讓我知道是否有錯誤,因為我自己沒有測試過此代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM