I want to show a download prompt when user hits the Apps Script web app URL.
Here is my code which shows the download prompt. But the downloaded file is an invalid file. The zip file does not open.
I opened the invalid file with text editor and compared the same with original file's content. Although most characters match but some characters were missing.
function doGet(e) {
return ContentService.createTextOutput(DriveApp.getFileById("XXXXXXXXXXXX").getBlob().getDataAsString()).downloadAsFile("Example.zip");
}
If you want to view the file as PDF, you can use this method.
You can also try
return ContentService
.createTextOutput()
.append(getFile(fileName))
.downloadAsFile(fileName);
In getFile() method, you can get the actual file from drive.
Hope that helps!!
Related
I am making a Google Apps Script 'Docs' add-in.
I have the following code:
return {
name: DocumentApp.getActiveDocument().getAs('application/pdf').getName(),
base64: Utilities.base64Encode(DocumentApp.getActiveDocument().getAs('application/pdf').getBytes())
}
The getActiveDocument() method is forbidden returning this:
Exception: The document is inaccessible. Please try again later. [line: 130, function: getPDF, file: Code]
This is because google docs works with google doc files and not .docx files.
How could I convert the .docx file I have in Docs to a .pdf file?
Note: copying the file and deleting the previous one is not an option.
Note: I want to do it within the app (in the code). Not manually :)
Open your docx document in MS Word 2013.
2Click on the File tab and select Save as.
3Choose the folder where you want to save the file. In the drop-down menu Save as type, choose PDF. Click the Save button and your docx file will be saved in PDF.
The code.gs has the following code.
function doGet() {
var output = HtmlService.createHtmlOutputFromFile('userendhtml');
output.addMetaTag('viewport', 'width=device-width, initial-scale=1');
return output;
}
The userendhtml has the following code.
Lecture Notes
The Google Drive Link refers to pdf file shared publicly. When user runs the script, The output is correct. However when he clicks on the link, he get the error that drive refused to connect. This works when I make the link with target =_"blank". I want to open pdf in same tab. How to solve this error? Can it be restructured in a way that back (browser back) will also work?
It is a known issue in Apps Script and likely happens when you are logged into multiple Google accounts.
Try the same app inside Chrome's incognito mode or log out of all other Google accounts and only log into the main Google account.
I want to print the Google script I have written (ultimately into a PDF) so I can send it to someone for them to look at.
I am unable to share the spreadsheet with them so I want to send it as another file but I don't want to lose the formatting. Not sure if this is possible but any help would be great. Thanks.
To export a static version of your code as displayed in the Apps Script Editor do the following: "Save Page As..." "Complete Webpage" on Chrome (other browsers will have corresponding method). Then share the resulting .htm file and folder. Delete all the .js files so that you don't get error messages. Done!
Here's an example folder you can download to see what the result looks like with javascript files deleted: https://drive.google.com/drive/folders/1eEhXJ7O7wc_uVzvzxIQ-ZpDnKA6HD_61?usp=sharing
In Google Apps Script I have a project containing a couple html files.
I have a script published as a web app who redirect the authenticated users according to the group they belong:
if (groupA.hasUser(currentUser.getEmail())) {
return HtmlService.createTemplateFromFile('GroupA').evaluate().setSandboxMode(HtmlService.SandboxMode.IFRAME);
} else {
return HtmlService.createTemplateFromFile('GroupB').evaluate().setSandboxMode(HtmlService.SandboxMode.IFRAME);
}
In GroupA.html I want to make a hyperlink to a html file contained in my project, like Click here
How can I get the reference of a html file within a Google Script Project?
Thanks
You can't access the HTML files directly, they don't have their own URL's. When you publish your script, it has a single URL that all requests will be directed too.
To serve different html files, you can pass in a query string argument that indicates which file to display, then have your script load the appropriate file.
When a script is published, the doGet() function is called automatically.
for example, say you have a main page called default.html, and other pages you want the user to load in certain cases:
function doGet(e){
var html = HtmlService.createHtmlOutputFromFile('default.html');
if(e.parameter.page)
html = HtmlService.createHtmlOutputFromFile(e.parameter.page);
return html;
}
To access files other than default.html using this code, you would pass in a "?page=" query string on your url, with the filename after the =.
Click Here
The link above, with [SCRIPT URL HERE] replaced by your actual script url, would display the html file named "second_page.html"
See:
https://developers.google.com/apps-script/guides/web
https://developers.google.com/apps-script/guides/html/
i've a good google apps script that works well, it get parameters from GET/POST for produce pdf and send it by mail and store it in google drive.
all works fine if i execute that script by my user.
i've tryed to publish to everyone, keeping execution as my user
when i try to open the generated url i see only a button "click me"
what's wrong ??
i need to call this script from GAE for execute some stuff on docs.
anyone can help me?
this is the url of my script: script
thanks
Sandro
edit
this is my test code:
function doGet() {
var output = ContentService.createTextOutput()
.setMimeType(ContentService.MimeType.TEXT)
.setContent("Hello World");
return output; }
i've publish as webapp, execution is set to my self, access is set to everyone including anonymous.
When i try to access as anonymous user, i always saw that button :(
You didn't update the version (see the menu: File > Manage Versions) and then republish the app, so you were getting the old code (aka, the sample code you get from "script as webapp"). The test URL is always the newest code, but the real URL gets an explicit version.