如何将Google文件夹中的文件列表导入Google电子表格?

时间:2015-05-05 16:46:24

标签: google-apps-script google-sheets directory

我被要求将Google文件夹中的文件名和网址列表添加到Google电子表格中。我尝试过应用脚本,但我不知道该怎么做。

你能帮忙吗?

1 个答案:

答案 0 :(得分:2)

没有显示任何工作,但确定。这将为您提供文件名及其网址列表:

var SS = SpreadsheetApp.getActiveSpreadsheet();

/*
The ONLY WAY to get the Id  of a folder by right-clicking it and inspecting the link
Example: link= 'https://drive.google.com/open?id=0Bw34txsxdsVDeuacnV2t0VHg1eGM&authuser=0'
the id = '0Bw34txsxdsVDeuacnV2t0VHg1eGM'
DOUBLE CHECK YOUR ID!
*/
function getFileArray(folderId){
  var folder = DriveApp.getFolderById(folderId);
  var files = folder.getFiles();
  var fileList = [];
  
  //Loop though files and add names and urls to the array
  while (files.hasNext()){ 
    var file = files.next();
    var fileName = file.getName();    
    var fileUrl = file.getUrl();
    fileList = fileList.concat([[fileName, fileUrl]]);
  }
   //See returned fileList in a log
  //Logger.log( fileList ) //Preview Returned Array      
  return fileList
  
 
}

  
//Prints any 2D array to a range that starts with the selected cell
function printArrayToSelection(twoDimArr){  
  var firstCell = SS.getActiveCell();
  var lastCell = firstCell.offset(twoDimArr.length - 1, twoDimArr[0].length - 1);
  var destinationRange = SS.getActiveSheet().getRange(
    firstCell.getA1Notation() + ':' + lastCell.getA1Notation());
  destinationRange.setValues(twoDimArr);
  
}

//Print the actual data
function printFileArray(){
  printArrayToSelection(getFileArray('yourforderid'));
}