Google Apps scrip

How to get fileInfo in Google Apps Script

Get file info by FileId

var IMG_Doc = "https://dl.dropboxusercontent.com/u/54813368/images/icon_type2/docx.png";
var IMG_Xls = "https://dl.dropboxusercontent.com/u/54813368/images/icon_type2/xlsx.png";
var IMG_Pdf = "https://dl.dropboxusercontent.com/u/54813368/images/icon_type2/pdf.png";
var IMG_Psd = "https://dl.dropboxusercontent.com/u/54813368/images/icon_type2/psd.png";
var IMG_Empty = "https://dl.dropboxusercontent.com/u/54813368/images/icon_type2/unknown.png";
var IMG_404 = "https://dl.dropboxusercontent.com/u/54813368/images/icon_type2/404.png";

 

function getFileInfoById(fileId)
{  
  var data = {};
  data.img = IMG_404;
  data.imageFullSize = IMG_404;
  data.status = 0;
  data.fileName = '';
  data.link = '';
  data.fileid = fileId;
  
  if(fileId !=null && fileId.length>0 )
  {
    
     try { 
        var file = DriveApp.getFileById(fileId);  
        var type = file.getMimeType();
        var name = file.getName();
        data.fileid = fileId;
        data.type = type;
        data.status = 1;
        data.img = IMG_Empty;
        data.fileName = name;
        data.folderId = file.getParents().next().getId();
        var folderUrl = "https://drive.google.com/drive/folders/"+file.getParents().next().getId();
        data.folderUrl =  folderUrl;
        data.link = 'https://drive.google.com/file/d/'+fileId+'/view?usp=drivesdk';
        if(type.indexOf("image")>=0)
        {
         // var linkphoto = fileUrl.replace("https://drive.google.com/file/d/", "https://googledrive.com/host/");
         // linkphoto = linkphoto.replace("/view?usp=drivesdk","");
          
          
         // var fileid=linkphoto.split("/")[4];  
          var thumbnailimg="https://drive.google.com/thumbnail?authuser=0&sz=w50&id="+fileId+"";
          var imageFullSize ="https://drive.google.com/thumbnail?authuser=0&sz=w0&id="+fileId+"";
          var linkphoto="https://googledrive.com/host/"+fileId+"";
          
          var photo = linkphoto.indexOf("404.png")>-1 ? "" :  thumbnailimg; 
          var imgFull = linkphoto.indexOf("404.png")>-1 ? "" :  imageFullSize; 
          data.img = photo;
          data.imageFullSize = imageFullSize;
          
        }
        else if(name.indexOf(".psd")>0)
        {
          data.img = IMG_Psd;
        }
        else if(name.indexOf(".pdf")>0)
        {
          data.img = IMG_Pdf;
        }
        else if(name.indexOf(".doc")>0)
        {
          data.img = IMG_Doc;
        }
        else if(name.indexOf(".docx")>0)
        {
          data.img = IMG_Doc;
        }   
        else if(name.indexOf(".xls")>0)
        {
          data.img = IMG_Xls;
        }
        else if(name.indexOf(".xlsx")>0)
        {
          data.img = IMG_Xls;
        }                  
     }
     catch(e){                
        data.msg = e.message;        
      }
  }
  else
  {    
    data.msg = "Not found ID file";    
  } 
  return data;
}

Get fileInfo by fileUrl

function getIdFromUrl(url) {
 
    var data =  url.match(/[-\w]{25,}/); 
    Logger.log(data);
    return data;
  
}
function getFileInfo(fileUrl)
{
  //  fileUrl='https://drive.google.com/file/d/0B8YFFrr3TEZoNEpRZTFlNXpDU1k/view?usp=drivesdk';
  var data = {};
   
  
  if(fileUrl !=null && fileUrl.length>0 )
  {    
      try {
        var a = getIdFromUrl(fileUrl);               
        data =  getFileInfoById(a[0]);
      }
      catch(e){                
        data.msg = e.message;        
      }
  }
  else
  {    
    data.msg = "Not found ID file";    
  } 
  return data;
}

Result:

 

Get All Files in Folder by FolderId / FolderName

function getFilesInFolderId(folderId){
 

    var folder = DriveApp.getFolderById(folderId);   
    var list = [];    
    var files = folder.getFiles();
    while (files.hasNext()){
      file = files.next(); 
      var row = getFileInfoById(file.getId());
      list.push(row);
    }
    return list;
}
function listFilesInFolderByName(folderName) {
  var folder = DriveApp.getFoldersByName(folderName);   
  var list = [];
  list.push(['Name','ID','Size']);
  var files = folder.getFiles();
  while (files.hasNext()){
    file = files.next();
   // var row = []
    //   row.push(file.getName(),file.getId(),file.getSize())
    
    var row = getFileInfoById(file.getId());
    list.push(row);
  }
   Logger.log(list);
 
};

Result:

How to get fileInfo in Google Apps Script
How to get fileInfo in Google Apps Script

 

Happy Coding 😉

Published by

tuanitpro

Tôi là Lê Thanh Tuấn, và tôi chia sẻ những điều mình cho rằng nó là thú vị, hay giúp ích cho bạn!

Leave a Reply