I've created a file in Google Apps Script as follows
DocsList.createFile(
"test.csv",
"Row1Col1,Row1Col2 \r\n Row2Col1,RowCol2 \r\n Row3Col1,Row3Col2");
How to download it programmatically (via a popup Download dialog for example) ?
Try using ContentService, you can trigger a download and even set the file name via the TextOutput.downloadAsFile method. You can also set the content mime type of the returned data, though only to a predefined enum constant. See their examples.
To download a file with apps script you need to either publish the app, and use the doGet() method (otherwise the "downloadFileAs" won't work)
OR...
create the file in Drive, and provide a link in a dialog ui.
here are two options for links, one requires users to be logged in the other doesn't taken from here
var dat=getFileDataFromAnotherFunction();
var file = DriveApp.createFile('name.csv',dat);
var t = HtmlService.createTemplateFromFile('DownloadDialog');
t.url1 = getDlUrl(file);
t.url2 = file.getDownloadUrl().replace('&gd=true','')
rt = t.evaluate().setSandboxMode(HtmlService.SandboxMode.IFRAME);
uia.showModalDialog(rt,'Download the csv')
where "DownloadDialog.html"
is the following file (also in the script editor )
<div>
<a target="_blank" href="<?= url1 ?>"> l1 </a>
<br>
<hr>
<a target="_blank" href="<?= url2 ?>"> link 2 </a>
</div>
If you want to show a download dialog at client end then following code works. It is an example for CSV file.
return ContentService.createTextOutput("bbb,aaa,ccc").downloadAsFile("MyData.csv")
.setMimeType(ContentService.MimeType.CSV);
Related
I have few files in doc/docx/pdf format stored on server's folder and their path are saved in database. I wish to fetch the path of these files from database and then display it on my website.
In the database the files are stored in this format
id path
1 abc/request/file1.docx
2 abc/request/file2.pdf
3 abc/request/file3.docx
To display the file i used the following method
$a = $data->path;
$b = 'http://example.com/';
$r = $b.$a;
<iframe src="http://docs.google.com/gview?url=<?php echo $r; ?>&embedded=true" style="width: 100%; height: 600px;" frameborder="0"></iframe>
Issue
Earlier the file was getting displayed but all of a sudden it is not getting displayed now
I did the following checks to see the validity of file
1) File is in proper format and is not corrupted and does exist on server folder
2) The console is not giving any errors
3) Tried to run http://docs.google.com/gview?url=http://example.com/abc/request/file1.docx on browser, there also the file is not getting displayed, however the other example url given on net are working
Can anyone please tell how to correct the error. And I would also appreciate if anyone could tell any other way(using jquery, javascript or any but reliable way) to display the files on website without disturbing the formatting of the original file
For PDF, you can use ViewerJS to render.
For doc/docx, consider using Microsoft Office Viewer as a walkaround if Google Docs Viewer is not stable enough to you
<iframe src='https://view.officeapps.live.com/op/embed.aspx?src=http%3A%2F%2Fieee802%2Eorg%3A80%2Fsecmail%2FdocIZSEwEqHFr%2Edoc' width='100%' height='900px' frameborder='0'>This is an embedded <a target='_blank' href='http://office.com'>Microsoft Office</a> document, powered by <a target='_blank' href='http://office.com/webapps'>Office Online</a>.</iframe>
P/S: Don't know why the code snippet is not working, but you can take a look here: https://jsfiddle.net/gcuzq343/
you can use
1. google docs viewer
<iframe src="http://docs.google.com/gview?url=http://example.com/my-document.doc&embedded=true"></iframe>
2. Microsoft viewer
<iframe src='https://view.officeapps.live.com/op/embed.aspx?src=http://example.com/my-document.doc.doc' width='800px' height='600px' frameborder='0'>This is an embedded <a target='_blank' href='http://office.com'>Microsoft Office</a> document, powered by <a target='_blank' href='http://office.com/webapps'>Office Online</a>.</iframe>
I'm trying to build a website using Google App Script where I copy all elements from my google form and show it in my own website. I was able to get the details of all elements using : https://developers.google.com/apps-script/reference/forms except for the Image elements.
This link doesn't have the information to get the image URL :
https://developers.google.com/apps-script/reference/forms/image-item
I was able to fetch the BLOB but I couldn't fetch the image URL.
For example in the screenshot, I need the highlighted URL of the image in my form : https://lh6.googleusercontent.com/uZtoCzoraW7vkkrN2289pX83Y6wwaRmMjpgBySWHaT3Vm2p9DVAA7Voy4CclYp2hve6c6zzzLkh-rF1yAX9fFPTTd940WMjwVtjcyMF2XCbdQ7YKQhhCfYxSUyKztcKacWCitDy4C31f9lQ
I need the URL of the image to add in the source tag of my website.
Is there a method in google app script that can fetch me the URL of an image/video.
Solution
After taking a further look to your question, I found out I could use the Blob onject you get from the form image to create a drive file which then can be used to get its webContentLink url to be used in the web app using the Drive API.
To be able to use the Drive API in your Apps Scrip script please, in your script editor go to Resources -> Advanced Cloud Services and enable the Drive API in the dialog that will pop up.
This is the piece of code I have used for achieving what you aimed for with self explanatory comments (this is a simplified web app that only focuses on achieving your purpose):
Code.gs file
// Apps SCript function to serve the HTML file in the web
function doGet() {
return HtmlService.createHtmlOutputFromFile('test');
}
// Function to be called in the HTML that returns the URL to be used by the image tag
function getUrl() {
var form = FormApp.getActiveForm();
// Get image blob
var image = form.getItems()[0].asImageItem().getImage().getAs('image/jpeg');
// Use the image blob for creating a new drive file
var file = DriveApp.createFile(image);
// Use the Drive API get method to get the property webContentLink which will return the link
// of the image to be used in a website
var fileURL = Drive.Files.get(file.getId()).webContentLink;
return fileURL;
}
index.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body id="body">
<h1>MY IMAGE</h1>
<script>
// Get what our function getURL returns
google.script.run.withSuccessHandler(function(URL) {
// Create HTML image tag
var img = document.createElement('img');
// Set its source to our file's sharable URL
img.src = URL;
// Attach image element to the body
document.getElementById('body').appendChild(img);
}).getURL();
</script>
</body>
</html>
I hope this has helped you. Let me know if you need anything else or if you did not understood something. :)
I am trying to embed, into Google Classic Sites, a project from Google Scripts that contains a clickable image pointing to a separate page from within this same Google Classic Site (there are reasons I am not using the Sites editor directly and inserting as an image with a link to the page).
The problem is when I click on the image/anchor, I get the dreaded "sites.google.com refused to connect." with no errors logged in Cloud Platform but console.log error is this: Refused to display 'https://sites.google.com/a/DOMAIN/SITE_NAME/PAGE_NAME/' in a frame because it set 'X-Frame-Options' to 'sameorigin'.
How I have embedded into Classic Sites from Scripts:
from Scripts => Publish/Deploy as web app with settings:
Execute the app as: tried both me and as user
Who has access to the app: Anyone within DOMAIN
from Scripts => Test web app for your latest code results: same as when embedded in Classic Sites
from Classic Sites => Edit Page / Insert / Apps Script, then pasted URL of published Scripts project.
Here is the code from Google Apps Script I am embedding:
Code.gs file
function doGet() {
return HtmlService.createTemplateFromFile('Buttons').evaluate()
.setTitle('Client Home Page');
}
Buttons.html file
<!DOCTYPE html>
<html>
<head>
<?!= HtmlService.createHtmlOutputFromFile('stylesheet').getContent(); ?>
</head>
<body>
<a href="https://sites.google.com/a/DOMAIN/SITES_NAME/PAGE_NAME/" imageanchor="1" target="" >
<img src="https://sites.google.com/a/DOMAIN/IMAGE_LINK" border="0" alt="Image" >
</a>
</body>
</html>
What I have also tried:
Embedding the Script as an iFrame gadget. Results: doesn't show UI at all.
Adding an onclick event to fetch URL instead (see code changes below). Results: embedded area where buttons display turns blank on click with no errors logged in Cloud Platform or console.
fetchUrl attempt: changes to Code.gs file
function doGet() {
return HtmlService.createTemplateFromFile('Buttons').evaluate()
.setTitle('Home');
}
function setUrl(url) {
var response = UrlFetchApp.fetch(url);
return response.getContentText();
}
fetchUrl attempt: changes to anchor tag in Buttons.html file
<a href="" onclick="google.script.run.setUrl('https://sites.google.com/a/DOMAIN/SITES_NAME/PAGE_NAME/')" imageanchor="1" target="" >
<img src="https://sites.google.com/a/DOMAIN/IMAGE_LINK" border="0" alt="Image" >
</a>
Any help or insight would be greatly appreciated!
Leora
Figured it out! I was missing the <base> tag in my HTML file (<base target="_top">).
While this is automatically supplied in any new HTML file you create within Google Apps Scripts, I had copied and pasted from code written in VS Code, therefore omitting it.
As soon as I added it back in everything works as expected. Going to leave this here in case anyone else runs into this problem.
Many thanks to anyone that attempted to look into this...
Leora
I want to display a preview of a(ny) file on a panel in a UiApp using GAS.
I'm using DriveApp, not DocsList.
Using file.getThumbnail() or file.getAs(mimeType) I can get a Blob of any file.
Documentation at https://developers.google.com/apps-script/reference/drive/file#getThumbnail%28%29 states that (at least) I can get those blobs being converted to 'application/pdf'.
I don't know how to display those blobs (or pdf-files as such) on a panel.
Can anybody tell me what I should do?
You cant display them inside the panel because you would need to iframe it, which isnt possible even with htmlServices.
Use an anchor to open the link on another page. You can link to the original file.
You can convert a spreadsheet with a pdf but i don"t know with any File.
I use this code
var pdf = DocsList.getFileById(newSpreadsheet.getId()).getAs('application/pdf').getBytes();
var attach = {fileName:'Weekly Status.pdf',content:pdf, mimeType:'application/pdf'};
You cna test It with this source if he can help you .
https://gist.github.com/ixhd/3660885
I am trying to show a specific privately shared drive folder in my Google page. I am using this guide: https://developers.google.com/apps-script/articles/embedding_docslist_in_sites
I am stuck on Step "8. Replace PASTE_PAGE_URL_HERE in the method getPageByUrl() by the url of your file cabinet."
I have tried every URL combination in "" and I just get a scripting error on the getPageByUrl() method.
I just tested this and it seems to work fine for me. Make sure you are using the full URL for your file cabinet page. For example, on my test site, I created a new page using 'file cabinet' in the template dropdown calling it "myfcpage". This gives me a new page with URL: "https://sites.google.com/site/mytestsite/myfcpage". Therefore my function call looks like this:
var page = SitesApp.getPageByUrl("https://sites.google.com/site/mytestsite/myfcpage");