Is it possible to open a PDF file right after its creation with Apps Script in Google Sheets?
My script is generating a pdf based on a Google Sheet and saving it on Google Drive. I would love to be able to automatically open it to print it.
Would spare some time for the user, not having to find the file and open it manually.
Thanks a lot for your help :)
Well, if you are opening just it to print it; Most modern printers can be WiFi connected with custom email addresses. Then you can just email the file as an attachment to the printer.
It's not the perfect solution but you could present them with a dialog that has a Url to the file. If you right click on it you'll get a menu that gives you the option to open in a new tab or a new window.
function createPDFofSheet() {
var ss=SpreadsheetApp.getActive();
var file=DriveApp.createFile(ss.getBlob());
var url=file.getUrl();
var html=Utilities.formatString('Right Click on this link and chose open in new tab',url);
var ui=HtmlService.createHtmlOutput(html);
SpreadsheetApp.getUi().showModelessDialog(ui, 'title');
}
Here's the output of this simple script
Related
My problem Statement is:
I want to download PDF using JavaScriptExecutor, When I click on the
PDF link it opens the PDF in a new window, It is an online PDF Viewer window, having a toolbar section where print, download..etc, features are available, Manually I can download by clicking on Download button. Now I want to automate this Scenario, I Researched and found that it can be
handled by using "javascript executor" I inspect that opened PDF window and the toolbar section is inside nested shadow root. Then I proceeded with writing the JSPath of the download element, basically, I copied the JSPath of the download Element, I checked it on the Console and it was able and locate and perform the Click operation. Same when I tried to do it through Script it gives me the exception "Cannot read properties of null(reading 'ShadowRoot')"
Screenshots of DevTool Console
The code I used so far:
IWebDriver driver;
driver.FindElement(By.Xpath("Xpath_of_PDF_Link")).Click();
driver.SwitchTo().Window(driver.WindowHandles.Last());
Thread.Sleep(2000);
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
IWebElement downloadButton = (IWebElement)js.ExecuteScript("return document.querySelector(\"#viewer\").shadowRoot.querySelector(\"#toolbar\").shadowRoot.querySelector(\"#downloads\").shadowRoot.querySelector(\"#download\")");
Thread.Sleep(1000);
var element = "argument[0].click()";
js.ExecuteScript(element,downloadButton);
Screenshots of devTool attached below
Does anyone have any possible solutions or insight into How to download the PDF? I'm relatively new to programming and would appreciate any advice.
Please let me know, If any other information required.. Thanks in advance
You could just get the url of the pdf link, then download it using an HTTP client.
I'm not familiar with C#, but here's pseudocode:
var url = driver.findElement(pdflink).getAttribute(href);
var client = new HttpClient();
var result = client.Get(url);
Files.write("myfile.pdf", pdf);
I am trying to make a spreadsheet capable of opening an HTML file as a sidebar, but the HTML file is located on a master spreadsheet. Here is the code that I have already for opening the sidebar using an HTML that is in the spreadsheet.
function sidebar1()
{
var b1 = SpreadsheetApp.getActiveSheet().getRange('C13').getValue();
html = HtmlService.createHtmlOutputFromFile(b1).setTitle(b1);
ui.showSidebar(html);
}
It's only possible if you are the current user of that spreadsheet and that spreadsheet is the container of the script because SpreadsheetApp.getUi() as described in the documentation Returns an instance of the spreadsheet's user-interface environment that allows the script to add features like menus, dialogs, and sidebars. if a spreadsheet is opened by id or url then there is no user interface
I just thought that if you have two spreadsheets already open it might be possible to call a server side function on one to run a server side function on the other using Apps Script API which might be able to launch a Sidebar on the second sheet. So maybe I'm wrong. Give it a try.
One solution would be to open the HTML file into a temporary workbook, and copy the sheet from there into the workbook containing all of them.
I'm trying to figure out how to check a Google Drive URL to see if a specific Gmail account has 'edit rights' to it. Is this possible in Apps Script? If yes, any help is greatly appreciated.
I'm currently researching File.getEditors(), but not sure how to test this code:
// Log the email address of all users who have edit access to a file.
//https://developers.google.com/apps-script/reference/drive/user
var file = DriveApp.getFileById('1mgw9xYO7X99brzrIk4Uel8YzndQa8dpGGQCnrFn4suU');
var editors = file.getEditors();
for (var i = 0; i < editors.length; i++) {
Logger.log(editors[i].getEmail());
}
To test this code, you need to place the code inside a a function inside the Script Editor on Google Drive, then execute the function.
You can access the Script Editor in two ways, either from within a spreadsheet (under Tools->Script Editor), in which case your script will be bound to the Spreadsheet. Binding to a spreadsheet can be useful if you want the script to work with data from sheet, for example you might want to store some configuration settings in the sheet, in which case keeping the sheet and the code together as one file is convenient.
You can also create a stand alone Script by connecting the Google Apps Script app to your Google Drive (+New -> More -> Connect more apps), then creating a stand alone Google Apps Script file directly on drive.
Regardless of how you create the script, the behaviour of this script and the script editor will be exactly the same.
Once you are in the script editor, paste that code into a function (by default you will be given an empty function named "myFunction()". Then run it using the "Run" menu.
Finally, under the "View" menu, go to "Logs" to see the results of Logger.log().
I'm making a browser extension and I have a credentialManager.html file that I want to open in a new window. Currently doing this
var credentialWindow = window.open("credentialManager.html",
"Credential Manager", "width=150,height=250")
but it just appends credentialManager.html onto whatever website domain I'm on and opens that. I want it to open the file with that name, which is in the extension package. Thoughts?
Found the solution: to access a .html file in the extension package you use the following
var credentialWindow = window.open("ms-browser-extension://[id of extension]/[name of html file].html", "[name your popup window]", "width=[some num],height=[some num]");
I am building an windows app using Tide SDK. How can I open a local file from my app. For example, I have a pdf file somewhere in my harddisk. I put the link to that file to my app. When I click on the link, I want it to open the pdf file using default programm associated with pdf type file.
If it is not possible then I have one more general question. Is it possible to access local file system by any app that built using html5 and javascript?
we can access local file system in tidesdk application.
See the following code.
var contents;
var file= 'test.txt';
var Dir = Ti.Filesystem.getUserDirectory();
var readfi= Ti.Filesystem.getFile(Dir, file);
if (readfi.exists())
{
var Stream = Ti.Filesystem.getFileStream(readFi);
Stream.open(Ti.Filesystem.MODE_READ);
contents =Stream.read();
alert( contents );
Stream.close();
}
The above code will read the text file and alert the content. Visit here to know tidesdk file system.
Following code will open the given URL in default browser.
Ti.Platform.openURL('http://stackoverflow.com');
Following code will Open the given application or file in the system's default program.
Ti.Platform.openApplication('C:/Documents and Settings/Thavamani00/Desktop/readme.txt');
Ti.Platform.openApplication('C:/Documents and Settings/Thavamani00/Desktop/cart15.png');
The above code displays the text file in notepad and displays the image in mircosoft picture manager.
it works well for me.i hope its your required answer.