+ Reply to Thread
Results 1 to 5 of 5

Google sheets vba to download file and save it to google drive folder and use it's data

Hybrid View

  1. #1
    Registered User
    Join Date
    05-18-2013
    Location
    holland
    MS-Off Ver
    Excel 2011
    Posts
    27

    Google sheets vba to download file and save it to google drive folder and use it's data

    Hi,

    I am new to VBA and have some beginning knowledge but not good for now for what I want to do.
    So I was hoping to find someone who can help.

    so this the issue atm.

    I wont to download a CSV file from a specific address:
    https://www.xxxxxxxxxxxxxxxxx.nl/fee...px?language=nl

    I have for now hidden our site as we can not give out the CSV file to the public atm. anyway if you click it, you will get a CSV file with tables and data in it.
    What I am trying to make atm is that google sheets will download this file every night to a specific google drive location. later I will try to get other scripts to get the data of that file and paste its data inside google sheets.

    I want to do this so we do not have to manually update our stock from our warehouse.

    Thank you all in advance.
    Last edited by western; 01-14-2021 at 08:48 AM.

  2. #2
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,019

    Re: Google sheets vba to download file and save it to google drive folder and use it's dat

    You cannot use VBA in Google Sheets. Also, there is a separate section for non-Excel related questions.
    Everyone who confuses correlation and causation ends up dead.

  3. #3
    Registered User
    Join Date
    05-18-2013
    Location
    holland
    MS-Off Ver
    Excel 2011
    Posts
    27

    Re: Google sheets vba to download file and save it to google drive folder and use it's dat

    Dear Rorya,

    thank you for giving me a reply, will see if i can move the post to not excel related questions.
    In regard of not possible to use VBA in google sheets, i did not know. I was able to adjust the script below to work for other of my projects in google sheets could you tell me what script was this? as i could only adjust it as i learned basic knowledge of VBA XD.

    Thank you in advance

    function UpdateImageList() {
      /* Adapted from Code written by @Andres Duarte in this link:
        https://stackoverflow.com/questions/59045664/how-to-list-also-files-inside-subfolders-in-google-drive/63182864#63182864
      */
    
      // Lista todos los archivos de una carpeta y de sus sub carpetas, y toma el nombre de la carpeta a analizar del nombre de la hoja activa.
      // List all files and sub-folders in a single folder on Google Drive, and get the name of the activesheet to know the folder desired.
      var foldername = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getName();
    
      // Declaramos la hoja // declare this sheet
      var sheet = SpreadsheetApp.getActiveSheet();
      // Borramos los datos de la hoja // clear any existing contents
      sheet.clear();
      // Agregamos una linea con los titulos // append a header row
      sheet.appendRow(["Folder","Naam", "Last Updated", "Size MB", "URL", "ID", "Access","Permission", "Document type"]);
    
      // getFoldersByName = obtener una coleccion de todas las carpetas en la unidad Drive que tienen el nombre buscado "foldername".
      // folders es un "iterador de carpetas" pero hay solo una carpeta para llamar por el nombre, por eso tiene un solo valor (next)
      // getFoldersByName = Gets a collection of all folders in the user's Drive that have the given name.
      // folders is a "Folder Iterator" but there is only one unique folder name called, so it has only one value (next)
      var folders = DriveApp.getFoldersByName(foldername);
      var foldersnext = folders.next();
      var lintotal = 2;
    
      //Iniciamos la funcion recursiva // Initiate recursive function
      lintotal = SubCarpetas(foldersnext, foldername, lintotal);  
    }
    
    function SubCarpetas(folder, path, cantlineas) {
      cantlineas = ListarArchivos(folder, path, cantlineas);
      var subfolders = folder.getFolders();
    
      while (subfolders.hasNext()) {
        var mysubfolders = subfolders.next();
        var mysubfolderName = mysubfolders.getName(); 
        var newpath = "";
        newpath = path + "/" + mysubfolderName;
        cantlineas = SubCarpetas(mysubfolders, newpath, cantlineas);
      }
      return(cantlineas) 
    }
    
    function ListarArchivos(mifoldersnext, mipath, milintotal) {
      var datos = []; //array temporal que vamos a usar para grabar en la hoja
      var files = []; //array con todos los archivos que encontramos en la carpeta que estamos evaluando
      var file = []; //array que usamos para volcar los datos de cada archivo antes de guardarlo
      var total = 0;
      var sheet = SpreadsheetApp.getActiveSheet();
      var myfiles = mifoldersnext.getFiles();
    
    // Creamos un array con los datos de cada archivo y guardamos el total de archivos
    while (myfiles.hasNext()) {
        files.push(myfiles.next());
        total++;
    }
    //ordenamos el array por nombre de archivo alfabeticamente  //sorts the files array by file names alphabetically
    files = files.sort(function(a, b){
       var aName = a.getName().toUpperCase();
       var bName = b.getName().toUpperCase();
       return aName.localeCompare(bName);
    });
    
    ////
    var vuelta = 0;
    var bulk = 200; //Definimos la cantidad de lineas a grabar cada vez, en la hoja de la planilla GoogleDoc
    var linea = milintotal; //definimos en que linea vamos a grabar en la planilla
    for (var i = 0; i < files.length; i++) { //recorremos el array de archivos y formateamos la informacion que necesitamos para nuestra planilla
        file = files[i];
        var fname = file.getName(); //nombre del archivo
        var fdate = file.getLastUpdated(); //fecha y hora ultima modificacion
        var fsize = file.getSize()/1024/1024; //tamaño del archivo, lo pasamos de byte a Kbyte y luego a Mb
        fsize = +fsize.toFixed(2); //lo formateamos a dos decimales
        var furl = file.getUrl(); //url del archivo
        var fid = file.getId(); //id del archivo
        var fdesc = file.getDescription(); //descripcion
        var ftype = file.getMimeType(); //tipo de archivo
        datos[vuelta] = [mipath+" ("+total+")", fname, fdate, fsize, furl, fid, fdesc, ftype]; //ponemos todo dentro de un array temporal
        vuelta++;
        if (vuelta == bulk) {//cuando alcanza la cantidad definida, guarda este array con 10 lineas y lo vacía
          linea = milintotal;
    //      Logger.log("linea = "+linea); //DEBUG
    //      Logger.log("vuelta = "+vuelta); //DEBUG
    //      Logger.log("total = "+total); //DEBUG
    //      Logger.log("lintotal = "+milintotal); //DEBUG
    //      Logger.log("registros en datos = "+datos.length); //DEBUG
    //      Logger.log("data = "+datos); //DEBUG
          sheet.getRange(linea, 1, bulk,8).setValues(datos); //guardamos los datos del array temporal en la hoja
          SpreadsheetApp.flush(); //forzamos que aparezcan los datos en la hoja - sin esto los datos no aparecen hasta terminar (genera mucha impaciencia)
          milintotal = milintotal + vuelta;
          datos = []; //vaciamos el array temporal
          vuelta = 0;
          }
        }
    
    if (datos.length>0) {//Al salir del bucle grabamos lo que haya quedado en el array datos
          linea = milintotal;
    //      Logger.log("linea = "+linea); //DEBUG
    //      Logger.log("vuelta = "+vuelta); //DEBUG
    //      Logger.log("total = "+total); //DEBUG
    //      Logger.log("lintotal = "+milintotal); //DEBUG
    //      Logger.log("registros en datos = "+datos.length); //DEBUG
    //      Logger.log("data = "+datos); //DEBUG
          sheet.getRange(linea, 1, datos.length,8).setValues(datos);
          SpreadsheetApp.flush(); //ansiolítico
          milintotal = milintotal + datos.length;
          datos = [];
          vuelta = 0;
        }
    return (milintotal)
    }

  4. #4
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,019

    Re: Google sheets vba to download file and save it to google drive folder and use it's dat

    That's Javascript.

  5. #5
    Registered User
    Join Date
    05-18-2013
    Location
    holland
    MS-Off Ver
    Excel 2011
    Posts
    27

    Re: Google sheets vba to download file and save it to google drive folder and use it's dat

    Understood! thank you. will now delete this thread

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Download xlsm file from Google Drive
    By karmapala in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 05-19-2020, 10:29 AM
  2. VBA download a file from on FTP and to uploaded to Google drive.
    By starlev in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-02-2020, 05:34 AM
  3. google sheets check-boxes are not appearing in download file
    By Mofasol in forum For Other Platforms(Mac, Google Docs, Mobile OS etc)
    Replies: 0
    Last Post: 01-26-2020, 01:02 AM
  4. google sheets check-boxes are not appearing in download file
    By Mofasol in forum Excel - New Users/Basics
    Replies: 0
    Last Post: 01-26-2020, 01:02 AM
  5. Upload/download file from/to Google Drive (2)
    By jaryszek in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-08-2019, 04:32 AM
  6. Upload/Download file to/from Google Drive
    By TudyBTH in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-08-2016, 07:22 PM
  7. File download from google drive and dropbox
    By patz006 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-26-2015, 10:02 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1