我在提交谷歌表格时使用谷歌脚本生成PDF文档。 我们的想法是使用标签打印机在表格上打印表格中输入的名称。该打印机是谷歌云打印机。
function getNameOnSubmit() {
var formResponses = FormApp.getActiveForm().getResponses();
var formResponse = formResponses[formResponses.length-1];
var itemResponses = formResponse.getItemResponses();
var itemResponse = itemResponses[0];
Logger.log('Last response to the question "%s" was "%s"',
itemResponse.getItem().getTitle(),
itemResponse.getResponse());
createPDFFile(itemResponse.getResponse());
}
function createPDFFile(response) {
var docName = "Template";
var docTemplate = "1W-asdasdasdasdasd1398612983";
var tz = Session.getScriptTimeZone();
var timeStamp = Utilities.formatDate(new Date(), tz, 'ddMMyyyyhhmmss');
// Get document template, copy it as a new temp doc, and save the Doc’s id
var copyId = DriveApp.getFileById(docTemplate).makeCopy(docName+'_'+ timeStamp).getId();
// Open the temporary document
var copyDoc = DocumentApp.openById(copyId);
// Get the document’s body section
var copyBody = copyDoc.getActiveSection();
// Replace place holder keys located in our google doc template
copyBody.replaceText('Name', response);
// Save and close the temporary document
copyDoc.saveAndClose();
// Convert temporary document to PDF (XYZ-TEMP)
var pdf = DriveApp.getFileById(copyId).getAs("application/pdf");
// Convert temporary document to PDF
var pdf = DriveApp.getFileById(copyId);
var theblob = pdf.getBlob().getAs('application/pdf');
var folder = DriveApp.getFolderById('24234234524sgsdfgsgsgsd');
var movefile = folder.createFile(theblob);
// Delete temp file
DriveApp.getFileById(copyId).setTrashed(true);
printNameFromSheets(movefile.getId(),"43543543regsdfgsdfgsdf", movefile.getName());
}
function printNameFromSheets(docID, printerID, docName) {
var ticket = {
version: "1.0",
print: {
color: {
type: "STANDARD_COLOR",
//STANDARD_MONOCHROME
vendor_id: "Color"
},
duplex: {
type: "NO_DUPLEX"
},
fit_to_page:{
type: "NO_FITTING"
},
media_size: {
width_microns: "88900",
height_microns: "55880",
is_continuous_feed: "false"
}
}
};
Logger.log(' 43 '+JSON.stringify(ticket));
var payload = {
"printerid" : printerID,
"title" : docName,
"content" : DriveApp.getFileById(docID).getBlob(),
"contentType": "application/pdf",
"ticket" : JSON.stringify(ticket)
};
var response = UrlFetchApp.fetch('https://www.google.com/cloudprint/submit', {
method: "POST",
payload: payload,
headers: {
Authorization: 'Bearer ' + getCloudPrintService().getAccessToken()
},
"muteHttpExceptions": true
});
response = JSON.parse(response);
if (response.success) {
Logger.log("%s", response.message);
} else {
Logger.log("Error Code: %s %s", response.errorCode, response.message);
}
}
该功能正常工作,直到我将以下配置添加到代码中以调整打印尺寸
fit_to_page:{
type: "NO_FITTING"
},
media_size: {
width_microns: "88900",
height_microns: "55880",
is_continuous_feed: "false"
}
这是我在Logs中得到的 [18-01-16 10:41:12:154]对“名称”这个问题的最后回应是“测试测试” [18-01-16 10:41:19:067] 43 {“version”:“1.0”,“print”:{“color”:{“type”:“STANDARD_COLOR”,“vendor_id”:“Color”} , “双面”:{ “类型”: “NO_DUPLEX”}, “fit_to_page”:{ “类型”: “NO_FITTING”}, “media_size”:{ “width_microns”: “88900”, “height_microns”: “55880”, “is_continuous_feed”: “假”}}} [18-01-16 10:41:19:657]错误代码:424.0无法解析打印作业的打印票据。
提前致谢。
答案 0 :(得分:1)
以下修改怎么样? wordexp
是布尔值。因此,请删除*
。
false
"
如果这不起作用,我很抱歉。