How To Download / Export Sheets In Spreadheet Via Google Apps Script

前端 未结 4 1714
故里飘歌
故里飘歌 2021-01-01 07:46

The task is to automate the manual process accomplished by the menu option \"File | Download As | Plain Text\"

I want to be able to control the saved file name, whic

相关标签:
4条回答
  • 2021-01-01 08:05

    I have exported a spreadsheet as CSV directly into a local hard drive as follows:

    1. Get the CSV content from current sheet using a variation of function convertRangeToCsvFile_() from the tutorial on this page https://developers.google.com/apps-script/articles/docslist_tutorial#section3

      var csvFile = convertRangeToCsvFile_(...);
      
    2. Then select a drive folder that is syncing to a local computer using Drive

      var localFolder = DocsList.getFolderById("055G...GM");
      
    3. And finally save the CSV file into the "local" folder

      localFolder.createFile("sample.csv", csvFile);
      

    That's it.

    0 讨论(0)
  • 2021-01-01 08:09

    In case you are looking to export all of the sheets in s spreadsheet to csv without having to manually do it one by one, here's another thread about it:

    Using the google drive API to download a spreadsheet in csv format

    0 讨论(0)
  • 2021-01-01 08:18

    The download can be done. But not the "Write to the hard drive" of the computer.

    Write issue: You mean write a file to the hard drive of the computer, using Google Apps Script? Sorry, but you will need more than GAS to do this. For security reasons, I doubt this is possible with only GAS, have never seen anything like this in GAS.

    Google Drive API will let you do a download, just needs OAuth and the URL you gave.

    0 讨论(0)
  • 2021-01-01 08:23

    This app script returns a file for download instead of web page to display:

    function doGet(){
        var outputDocument = DocumentApp.create('My custom csv file name'); 
        var content = getCsv();
        var textContent = ContentService.createTextOutput(content);
        textContent.setMimeType(ContentService.MimeType.CSV);
        textContent.downloadAsFile("4NocniMaraton.csv");
        return textContent;
    }
    
    0 讨论(0)
提交回复
热议问题