我可以在提交Google表单时使用一些帮助来创建脚本。
提交表单后,脚本会自动将Google电子表格打印到基于Google Cloud的打印机,而不会显示打印对话框窗口。
脚本背后的想法很简单:
以下脚本将打印票证,但如何将打印机功能添加到POST请求中?
到目前为止我的代码:
function onsubmit(e) {
var pid = "123456789"; //place printer id here
var sheetid = "123456789"; //place sheet id here
var ss = SpreadsheetApp.getActiveSpreadsheet();
var info = ss.getSheetByName('Form responses 1');
var lastRow = info.getLastRow();
var sheet = SpreadsheetApp.setActiveSheet(ss.getSheetByName('TICKET'))
authorize();
var url = "https://www.google.com/cloudprint/submit?printerid="+pid+"&contentType=google.spreadsheet&content="+sheetid+"&title=PrintingTicket";
var options = {
method: "post",
oAuthServiceName : "print",
oAuthUseToken : "always"
};
info.hideSheet();
var response = UrlFetchApp.fetch(url, options);
return Utilities.jsonParse(response.getContentText());
}
function authorize() {
var oauthConfig = UrlFetchApp.addOAuthService("print");
var scope = "https://www.googleapis.com/auth/cloudprint";
oauthConfig.setConsumerKey("anonymous");
oauthConfig.setConsumerSecret("anonymous");
oauthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope="+scope);
oauthConfig.setAuthorizationUrl("https://accounts.google.com/OAuthAuthorizeToken");
oauthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");
}