P
P
Petya Persianov2017-09-20 15:31:33
JavaScript
Petya Persianov, 2017-09-20 15:31:33

How to download a file using google apps script?

I have a Google spreadsheet with data and a script with which I generate an html file from the table, which I then want to download. How to do it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Ivanov, 2017-09-21
@Dr0nk

There are several download options. As well as ways to create a file. It is not clear from the question how the file is created, what are the requirements for downloading, what interface is used.
Let's pretend that:
The main idea of ​​exporting/downloading files from Disk is to get a link to a resource

var file = DriveApp.createFile(fileName, content, 'text/html');
var downloadUrl = file.getDownloadUrl();

Now it's enough to create an event on the client side that will trigger the download. There may be several options. Here is one of them:
// Создаем окно на клиенте и запускаем
var a = window.document.createElement('a');
a.href = downloadUrl;
a.text = 'Download';
document.body.appendChild(a);
a.click();

At this point, the most difficult thing is to provide the user with an interface for implementing the download. It can be a letter, a notification, and anything else that can click links.
Another option would be to supply the data block via the Client-side API. The principle will remain the same, only the format of the transmitted data will change. From the Blob server, on the client, loading a local Blob resource.
Full code and link to working app https://gitlab.com/google-apps-script-russian/zagr...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question