2

I could use some help with creating a script when a Google Form is submitted.

Once the form submitted, the script automatically prints a Google Spreadsheet to a Google Cloud-based printer without bringing up the print dialog window.

I've read through Google Cloud Print Info

The idea behind the script is simple:

  • User fills out form
  • Form is then submitted
  • Script updates cells with information user put in
  • Script then prints to google cloud printer
  • Shipping label is ready

The below script will print a ticket but how do i add the printer capabilities to the POST request?

The code I have so far:

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");  
}
BBV
  • 21
  • 3
  • This won't be super simple, since the `GCP Web Element` is written in HTML/JS, and Google Apps Script is simply JS. But it should be possible. – rickcnagy Apr 04 '14 at 01:53
  • I've been able to successfully print the spreadsheet from the following website http://www.google.com/cloudprint/simulate.html (scroll down to print dialog and place spreadsheet ID in) – BBV Apr 04 '14 at 02:25
  • I have updated the sample code to include a "post" method but it produces an error of: Request failed for https://www.google.com/cloudprint/submit returned code 403. Truncated server response: User credentials required

    User credentials required

    Error... (use muteHttpExceptions option to examine full response). (line 20, file "Code") Any help would be much appreciated

    – BBV May 13 '14 at 06:55
  • Can any body help with sending the printer capabilities? – BBV Jul 03 '14 at 02:26

0 Answers0