How to use apps-script to override a file with more than 10MB content?

84 views
Skip to first unread message

Elad Ben-David

unread,
Oct 31, 2020, 10:03:36 AM10/31/20
to Google Apps Script Community
I have a drive folder with a few g-sheet files (each larger than 100MB). 
I want to override the content of the oldest file with the content of the newest file.
And then trash all files but the oldest one.


I have two problems:
1) Exception in file "Code".
2) My content can be larger than 10MB.

Is it a dead end?



    function deleteOldSheets() {
      var df = DriveApp.getFolderById("1haqAtxk7ZeXF9ayEbRFIf7yGi3KBu4S_");
      var arr = new Array();
      var files = df.getFiles();
      while( files.hasNext() ) {
        var f = files.next();
        arr.push( [ [ f.getId() ] , [ f.getLastUpdated() ] ] );
      }
        if (arr.length < 2){
        return;
      }  
      arr.sort( sortFunction );
      
      var mediaData = DriveApp.getFileById(arr[0][0]).getBlob().getDataAsString();
         DriveApp.getFileById(arr[arr.length-1][0]).setContent(mediaData);
    
      for( var i=0 ; i<arr.length-1 ; i++ ){
        DriveApp.getFileById( arr[i][0] ).setTrashed( true );
      }
    };
    
    
    function sortFunction( a , b ) {
      var aDate = new Date(a[1]);
      var bDate = new Date(b[1]);
      if ( aDate === bDate ) return 0;
      else if ( aDate < bDate ) return 1;
      else return -1;
    };


[![enter image description here][1]][1]



Clark Lind

unread,
Nov 2, 2020, 7:37:18 AM11/2/20
to Google Apps Script Community
Instead of trying to copy the whole file and adding it to the new file (I don't think that will work with any files, no matter what file size, try it), you may have to open each file, get each tab, get the datarange.values, and add a new tab to the old file and set the values. You would have to do this one tab at a time, one file at a time. Once all tabs are copied over, set the file to trashed, and move on to the next file & repeat. 

That is how I would do it.  Good luck!
Reply all
Reply to author
Forward
0 new messages