Google Sheets — How to set up Direct Printing (Now defunct) from the click of a Button or from a Custom Menu item
Update on 31st Dec 2020: Due to Google removing support for Google’s Cloud printing, it is no longer possible to set up direct printing via the method described below.
In your Google sheets if you want to directly print a sheet without the pop-up of the dialog window for printing then you can use the following steps.
- Create a Button or Custom Menu. (See Image below)
2. Select Tools from the Menu bar and click on the Script Editor Option
3. Once the Google Apps Script Project opens — Copy and Paste the following Script Code.
There are three fields in this code that need values to be entered, they are CLIENT_ID, CLIENT_SECRET and PRINTER_ID.
4. To generate CLIENT_ID and CLIENT_SECRET we need to create a project in Google Cloud Console. Open another tab in your browser and enter https://console.cloud.google.com/
5. Once the console is open. Click on ‘Select a project’
6. A pop-up window will open up from it select ‘New Project’.
7. A new project window will open up. You can use the suggested name for the project or rename it. Leave the ‘Location’ field as is. Then click ‘Create’.
8. You will see a notification once the project has been created. Click on it. Then the created project’s Dashboard will open up.
9. From the left-hand menu select ‘ APIs & Services’ and then select ‘OAuth Consent Screen’
10. From the new window of OAuth Consent Screen, click on ‘External’ and click Create.
11. From the OAuth Consent screen — Fill the Application name field (Give it any name you like) and the Authorized Domains field. In the authorized domains field enter “script.google.com”. Then click ‘Save’.
12. Now, that the OAuth Consent has been completed click on Credentials from the left menu bar. Then click on ‘Create Credentials’ and from its dropdown click on ‘OAuth client ID’. The ‘Create OAuth client ID’ screen will appear.
13. From the ‘Create OAuth client ID’ screen. Select Application type as — ‘Web Application’ and then give the Web Application a ‘Name’ and fill the Authorised redirect URIs with this: https://script.google.com/macros/d/{SCRIPT ID}/usercallback
The {SCRIPT ID} has to be replaced by the script ID of your Apps Script Project. To find the script ID of your project see images below the ‘Create OAuth client ID’ image.
14. Now, that the ‘Script ID’ has been replaced in the URI. Click on the ‘Create’ button in the ‘Create OAuth client ID’ window and a pop-up window with the ‘CLIENT ID’ and ‘CLIENT SECRET’ will pop up.
15. Copy the ‘Client ID’ and ‘Client Secret’ back into the Apps Script Project’s setClientId and setClientSecret fields in the script’s code.
16. Click on the ‘Resources’ item from the Menu bar and then select ‘Advanced Google Services’. A new pop-up window will open up. We have to add the ‘OAuth’ library here.
17. The ‘OAuth’ library has a unique script ID like our project which we can find by opening this github repository link and scrolling down to the ‘Setup’ heading and then copy the script ID.
18. The copied Script ID can be pasted into the ‘Add a library’ field and then click on ‘Add’. The OAuth2 library will appear and then select the latest version from the version dropdown. Then click ‘Save’ and close the window.
19. Now, we need to get the ‘Printer ID’ and paste it into our Apps Script code.
To get the printer ID, make sure that your printer is connected to the same network as your laptop/desktop. Now in your browser, open a new tab and search for “chrome://devices”.
20. Click on ‘Add printers’. A new window listing all the printers in your network and desktop will appear. You can choose to add all printers or just the ones you wish to connect to. Then click on Add printer(s).
21. Then click on ‘Manage your printers’.
22. From the list of printers, click on the printer you want to connect for direct printing. Then click on ‘Details’ to find the details of the printer.
23. Click on ‘Advanced Details’ to find the ‘Printer ID’.
24. Copy the ‘Printer ID’.
25. Go back to your Apps Script Project and scroll to the ‘Printer ID’ location and paste the ‘Printer Id’ in the “printGoogleDocument” function.
Now, you can call the “printGoogleDocument” function from another function or directly and pass the “document ID” and the “document name” values of the document to be printed to print the sheet directly from your printer and no print dialog window will appear. For any queries mail me at vicky.lalani@gmail.com.