Get link to previous versions of a file

135 views Asked by At

I am looking for a script like this to pull out the previous versions along with a link to them to put in a google sheet. The goal is to display previous versions and allow others to click on a link to view the previous versions by name.
I found this function here but I am not sure which link in the logs to use to retrieve the old version and display them

  function myF() {
  // Get the file id
   var fileId="FileID"
    
    // Get an array with all the revisions
    var revisions = Drive.Revisions.list(fileId);
    
    // Iterate through each revision
    for(i=0; i<revisions.items.length;i++){
    
    // Get each revision 
    var revision = revisions.items[i];
    
    // Log each revision with all its details
    Logger.log(revision);
  }
 }

Which part of the log text shows the link to the previous version(s)

1

There are 1 answers

0
10101101 On

You could do something like this

  const fid = '1AnneKZdGOXrJUX1-S4JKGrmVzezg416n_2vFl_yye7w';
  const revisions = Drive.Revisions.list(fid);
  let lastRevTime = 0;
  let lastRev = null;
  for (let i = 0; i < revisions.items?.length; i++) {
    const revision = revisions.items[i];
    const revTime = new Date(revision.modifiedDate).getTime();
    if (revTime > lastRevTime) {
      lastRev = revision;
    }
  }
  console.log(lastRev.selfLink);
  console.log(lastRev.exportLinks);

selflink is a link for file revision, and exportLinks is an array with links for downloading the file revision in specific format (for example pdf, docx, zip etc).