Delete files with google-apps-script

2

There is a google spreadsheet and a script that generates PDF files with the information of the spreadsheet, the PDF files are automatically saved in a folder of google drive. How could you erase the PDF files in that folder? Attachment code.

    function Hojascalculo() {
var sheetName = "HOY";
var folderID = "123456789"; // Identificador unico de carpeta.
var pdfName = "Informe salidas dia "+Date();

var sourceSpreadsheet = SpreadsheetApp.getActive();
var sourceSheet = sourceSpreadsheet.getSheetByName(sheetName);
var folder = DriveApp.getFolderById(folderID);

//Copia hoja de calculo
var destSpreadsheet = SpreadsheetApp.open(DriveApp.getFileById(sourceSpreadsheet.getId()).makeCopy("temporal_pdf", folder))

var sheets = destSpreadsheet.getSheets();
for (i = 0; i < sheets.length; i++) {
if (sheets[i].getSheetName() != sheetName){
destSpreadsheet.deleteSheet(sheets[i]);
}
}
    
asked by Jose Santiago 05.08.2017 в 05:53
source

1 answer

3

There are two possibilities:

  • Delete the file permanently

  • Send the file to the trash

  • The code has the first possibility activated by default, while the second one is commented:

    function borrarTodosEnFolder() 
    {
    
      /*Colocar ID del folder*/
      var folderId = '0B...';
    
      var thisFolder = DriveApp.getFolderById(folderId);
      thisFile = thisFolder.getFiles();
    
      while (thisFile.hasNext()) 
      {
        var eachFile = thisFile.next();
        var fileId = eachFile.getId();    
    
        /* Si sólo se quiere enviar a la papelera */
        //eachFile.setTrashed(true);
    
        /* Para removerlo definitivamente usar remove de la API de Drive antes activada :) */
    
        Drive.Files.remove(fileId);
    
        Logger.log("Se removió el archivo: " + eachFile.getName());
      };
    
    }
    

    Note:

    For option 1 to work, you must activate Advanced Drive Service in Google Apps Script.

    The steps are:

    • Go option Recursos of the top menu

    • Go to Servicios Avanzados de Google

    • On the screen that will open, turn on Drive Api

    It will tell you that you must have the Api activated in your Console project. If you already have it activated you do not have to do it.

    • Click on Aceptar

    Now you can use the Drive API (and any other that you activate) in Google Script.

    Look at the options that it will give you when you write for example: Drive. :)

        
    answered by 05.08.2017 / 09:13
    source