Open print preview dialog programmatically? - google-apps-script

I am creating an invoice tool within Google Sheets. The way it works is that after all the data is entered, a script is running, creating a sheet with the actual layed-out invoice.
I know that there is a way to automatically export sheets as PDF (by calling the document url with the "export?exportFormat=pdf" argument). But I don't want to export directly, I just need the "Print" dialog (File->Print) to pop up automatically so the user can adjust the settings before printing. (I know, it's not a big deal to press cmd+P yourself, but having the dialog open automatically would streamline the whole process a bit).
Is there any way to do that? I haven't found anything helpful within the documentation.

There isn't a way to do that with Google Apps Script services but you might do that by using client side code.
Related
JavaScript print preview

Related

Requesting feedback on the feasibility of creating a Google Apps Script to modify the default Gmail 'Save As' filename

I would like to create a means of overriding the default filename generated by Gmail when saving an email as a PDF file. I have a specific naming convention I use. It is tedious to manually modify the default filename each time I want to print an email to PDF.
I would like to create a script that does the following:
Within Gmail, I open an email I want to print to PDF.
I click a button or select a menu entry to initiate my print to PDF script.
The script opens the 'Save As' dialog and populates the filename field following my naming convention.
I then take over and interact with the 'Save As' dialog to save the file to the location of my choice.
I have done some initial research, but I am not confident this is going to be easy to accomplish. I would like some feedback from experts as to the feasibility of my project.
Thank you.
As far as I understand the question: you are not trying to interact at the Gmail web application level, but at the browser.
You can check for yourself when you use Gmail: before trying to save your attachment you can open the console (pressing F12) but as soon as you enter in a "save as" menu either you can't open it, or it will be dimmed.
This, again AFAIU, prevents you from acting even at the extension level, because you would not be able to press an extension button while the save dialog is open.
Unless you want to dig into the inner workings of your browser, I would recommend going the route of checking automation scripts for your operating system.
You could create a hot key on your pc that keys in the characters that respect your file format and, as soon as they are keyed in, you would then be able to modify the file name and press Enter.

Google Sheets Script not sticky

I am trying to create a custom function for a Google Sheet. to do this, I start from within the Sheet that I want to use the function, I then go to the menu Tools|<>Script Editor.
I then write a function. Name the file. And save it.
But, I can not use this function. Error #NAME?
Then if I close the Script Editor and open it again, it's like I have never been there. It presents the same form you see when you first start.
From an OP comment
I have discovered the issue, but not the solution. When the script editor is opened it switches me to another Google account I have. So I switch back. But then I am presented within a screen saying Acess Denied and making me ask permission. To whom and what for I am not sure.
The solution to the root cause of the problem depends on Google, fortunately there are several workarounds.
Use Chrome in incognito mode with all the extensions disabled, then sign in on only one account (the key part is bold)
Use another web browser or web browser profile. Sign in on only one account (the key part is bold)
Signout of all your Google accounts, then sign in first on the account that you will be using for developing your scripts, then sign in on the other accounts. This still migth be problematic under certain circunstances.
Also, as good habit, when you click Tools > Open script editor to create a new bounded project, the first thing that you should do is give a name to your project.
Another god habit, before testing a custom function, be sure that there isn't a project file with a red asterisk (unsaved file). Contrary to spreadsheets, the Apps Script files aren't automacatily saved when a edit is made.

How can I open the print window (export to pdf) via Google App Script?

I have a script that created a menu in a spreadsheet. When I click on the menu option, it asks for what line I want to print and it sets the data in another sheet (formatted for printing). I want to be able to open the print window of one of my sheets. Is there any way to achieve this?
I don't want to export the pdf automatically because I want to be able to choose the printer and the page format.
Thank you
This functionality is currently not possible in Google Apps Script. There is no option to automate opening the print dialog.
As a workaround, you could programmatically export the Sheet you want to print as a PDF and use Apps Script's MailApp to mail it to your printer.
If you are still interested in this feature and would like to communicate it to the Google Sheets team, I suggest you create a Feature Request in the Google's Public Issue Tracker.

Can I create a shortcut to execute a Google Apps script?

I have a script that runs on a Google Sheets that is fed by a Forms entry. Basically, when I fill out a form, it creates an entry on the sheet. From that entry, a new document is created by my script that "prettifies" the fields on the sheet for distribution.
This sheet is located on a Team Drive, but in order to have the resulting document created in my drive, I had to place the unbounded script in my drive and reference the sheet. If I have it bounded to the sheet, it will always put the newly created document in the Team Drive -- which I don't want.
When it was bounded to the sheet, I had a trigger that would automatically create the document when the form was submitted. Now that the script is on my drive, triggering on submission is not an option. The only options I have are time based or calendar based, and neither of these really work -- unless I only want to get the document once a month or every single minute.
So now, the only way I know to execute the script is to do it manually every time, which is a major hassle and requires multiple steps no matter how you go about it.
Is there a way to trigger this script on command without having to open the Script editor, click the script I want, and then click run?
Why are there no keyboard shortcuts in Apps? I know about Ctrl+R -- but this only works if you already have the script you want highlighted in the top action bar. If I type Ctrl+R without it already highlighted, nothing happens. Nothing happens with Alt+R or Alt anything, really. I have tried simply tabbing 187 times to get to where I need, but the tab never seems to focus "in" the script menu area. It just bounces around the address bar, the shortcuts bar, etc.
NOTE: I do not want to make major changes to THIS script. It took forever to get this working right and I don't want to jack it up. I just want to be able to trigger it without having to go through 18 steps to do so.
There is no way to execute a standalone script project's function with a keyboard shortcut from within the Apps Script Editor. If your script was bound, you would have the option of keyboard shortcuts from the container Google Sheets file (as a google-sheets-macro), but macros have some restrictions.
However, you are not without options. Two of them:
Publish the script project as a webapp and use a page visit as the trigger.
Open the page / click a bookmark -> execute the function via your doGet().
Convert your script to a 'polling' style that processes any number of form responses, and use a time-based trigger (e.g. hourly, daily) to automate the whole process.
(Include a "did I process this response yet" flag to prevent reprocessing a response)
There are more elaborate configurations (e.g. Apps Script API + local cron job) but those are unlikely to be simpler than the above two options.
As I mentioned in my comment, you more than likely just needed an extra step for the original bound script to create the file in your Google Drive and not the host Team Drive. If you're interested in that avenue, you'll want to ask the appropriate (read: new) question.
This is a workaround not an answer.
I use the Alt/ menu to efficiently look up functions by name (For me, its faster than using a cursor to work thru the nest of menus). If you design your function names so they differ from the built-in function names, the lookup may take only a few keystrokes)

When a SINGLE user opens their shared Doc, Prompt for authorisation?

I have no idea how to attempt this, nor do I know if it's possible and I have no code to show. However, I shall be sending a doc created from a template to whoever filled a form in.
The script I want to run is to create a custom menu so they can click a button and send the doc to another person. This works. perfectly. The script even gets copied when the Doc is made from the template.
But the script that is copied won't run or even prompt me without manually going in the script editor and clicking run. the script will fail because it's supposed to run on open, but it gets auth.
I know you can't run shared scripts without user auth, unless it's published, That's not a route I'm taking. I just want the good old white google authorisation box to pop up as soon as they open the google doc they receive.
Any suggestions/ideas/code?
Cheers
Please refer to the documentation at https://developers.google.com/gsuite/add-ons/concepts/addon-authorization. Add-on authorization is what you may need to implement.