Upload a file from Google Drive to a local folder from Google Apps Script

I am trying to download a specific *.csv file from my Google Drive to a local folder on my computer. I tried the following with no luck:

 ContentService.createTextOutput().downloadAsFile(fileName); 

I am not getting an error and nothing is happening. Any ideas on what happened to my attempt?

+4
source share
1 answer

ContentService is used to provide text content as a web application. The line of code you showed does nothing. Assuming this is the one-line body of the doGet() function that you deployed as a web application, this is why you can't see anything:

Since we don’t have content, there’s nothing to download, so you see, well, nothing.

CSV Downloader Script

This script will receive the text content of the csv file on your Google Drive and submit it for download. After you save the version of the script and publish it as a web application, you can direct the browser to the published URL to start the download.

Depending on your browser settings, you can select a specific local folder and / or change the file name. You have no control over this from the server side where this script is executed.

 /** * This function serves content for a script deployed as a web app. * See https://developers.google.com/apps-script/execution_web_apps */ function doGet() { var fileName = "test.csv" return ContentService .createTextOutput() // Create textOutput Object .append(getCsvFile(fileName)) // Append the text from our csv file .downloadAsFile(fileName); // Have browser download, rather than display } /** * Return the text contained in the given csv file. */ function getCsvFile(fileName) { var files = DocsList.getFiles(); var csvFile = "No Content"; for (var i = 0; i < files.length; i++) { if (files[i].getName() == fileName) { csvFile = files[i].getContentAsString(); break; } } return csvFile } 
+3
source

Source: https://habr.com/ru/post/1488806/


All Articles