如何将google驱动器文件夹及其内容复制到文件夹ID列表中

时间:2016-03-24 06:58:04

标签: google-apps-script google-sheets google-drive-api

我有一个循环浏览Google工作表中条目的脚本,为每一行创建一个文件夹并列出该文件夹的ID。我需要能够遍历文件夹ID列表并将模板主文件夹的内容(和子内容)复制到新文件夹中,例如(example here

我找到了一些例子,你可以在一个实例中只通过硬编码目标文件夹和源文件夹来做到这一点,但需要一些帮助才能使它变得更加可变和动态。

这是我到目前为止的代码:

function folderMaker(){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[0];//get first sheet



  var last = sheet.getLastRow();//end of list size


  //prompt for Folder Name

  var ui = SpreadsheetApp.getUi();
  var ask = ui.prompt('What is the Main Agent Folder Name? \nAgent folders will go inside this folder.');
  var response = ask.getResponseText();

  //Create a business folder

  var tFolder = DriveApp.createFolder(response);
  var tFolderId = tFolder.getId();//ID to business folder
  var tFolderUrl = tFolder.getUrl();//URL to business folder
  sheet.getRange('A1').setValue('business Folder');//put business folder info in row 1
  sheet.getRange('B1').setValue(response);
  sheet.getRange('C1').setValue(tFolderUrl);
  sheet.getRange('D1').setValue(tFolderId);
  sheet.getRange('E1').setValue(' ');

  //agent folder names

  var ask2 = ui.prompt('agent folder name');
  var response2 = ask2.getResponseText();

  //call the document to be copied


  Logger.log('last '+last);
  for(var i=3;i<last+1;i++){

    var agent = sheet.getRange(i, 1).getValue();//get agent name
    var email = sheet.getRange(i,2).getValue();//get agent email
    var folder = DriveApp.createFolder(agent + ' '+response2);
    var sFolder = folder.getName();
    var sFolderId = folder.getId();
    var sFolderUrl = folder.getUrl();
    folder.addEditor(email);//add agent as an editor of the folder
    sheet.getRange(i,3).setValue(sFolder);
    sheet.getRange(i,4).setValue(sFolderId);
    sheet.getRange(i,5).setValue(sFolderUrl);

    //Add agent folders to business folder
    var businessFolder = DriveApp.getFolderById(tFolderId);

    var agentFolder = DriveApp.getFolderById(sFolderId);

    businessFolder.addFolder(agentFolder);//put agent folder in business folder

    DriveApp.getRootFolder().removeFolder(agentFolder);//take agent folder out of Google Drive
  }

}

1 个答案:

答案 0 :(得分:0)

您似乎正在尝试遍历工作表,为每行创建一个文件夹,并将模板文件夹的内容复制到该新文件夹。我已经写了一些应该为你做的伎俩,我强烈建议你仔细阅读它,看它是否符合你的要求,以及它是如何工作的。

function main () {
  /*
  Get a template folder id from sheet, and copy that template to custom folders and share with a user.
  */
  var ss, sheet, templateFolderId, templateFolder, rootFolder,
      header, range, values, i,
      folderPath, folder
  ;
// Get spreadsheet and sheet
  ss = SpreadsheetApp.getActiveSpreadsheet();
  sheet = ss.getActiveSheet();

// Get template folder
  templateFolderId = sheet.getRange("C2").getValue();
  templateFolder = DriveApp.getFolderById(templateFolderId);

// Get root folder for where the new folders will live/be created
/*
*** Change this location if you nest them somewhere else***
*/
  rootFolder = DriveApp.getRootFolder();

// Get rows to process
  header = sheet.getRange(3,1,1, sheet.getLastColumn()).getValues()[0]; // Get header row
  header = Map(header); // Map header to an object to give column references clarity later
  range = sheet.getRange(4, 1, sheet.getLastRow() -3, sheet.getLastColumn()); // Get range of rows for processing
  values = range.getValues(); // Get data

// Iterate through rows.
  i = values.length;
  while (i--) {
//   Check for missing folder IDs (indicates it hasn't been processed).
    if (! values[i][header["Folder ID"]]) {
//     Split folder name into pieces
      folderPath = values[i][header["Folder Name"]].split("/");
//     Get destination folder
      folder = EnsureFolderExists(folderPath, rootFolder);
//     Copy template to folder
      CopyMasterTemplate(templateFolder, folder);
//     Share folder with user
      folder.addEditor(values[i][header["Email To Share Folder"]]);
//     Save folderId to row
      values[i][header["Folder ID"]] = folder.getId();
      values[i][header["Folder URL"]] = folder.getUrl();
    }
  }

// Save updated values to range
  range.setValues(values);
}

function CopyMasterTemplate (templateFolder, destinationFolder) {
  /*
  Copy contents of templateFolder to folder
  Must be recursive - folders within folders, unknown depth
  */
  var fileIterator, folderIterator, folder
  ;
// Copy files
  fileIterator = templateFolder.getFiles();
  while (fileIterator.hasNext()) {
    fileIterator.next().makeCopy(destinationFolder);
  }
// Copy folders
  folderIterator = templateFolder.getFolders();
  while (folderIterator.hasNext()) {
    folder = folderIterator.next();
    CopyMasterTemplate(folder, destinationFolder.createFolder(folder.getName()));
  }
  return;
}

function EnsureFolderExists (a, within) {
  /*
  Ensure folder path split in array a is available in folder within
  */
  var folder = within,
      folderIterator,
      i = 0
  ;
  while (i +1 <= a.length) {
    folderIterator = folder.getFoldersByName(a[i]);
    if (folderIterator.hasNext()) {
      folder = folderIterator.next();
    }
    else {
      folder = folder.createFolder(a[i]);
    }
    i++;
  }
  return folder;
}
function Map (a) {
  /*
  Map 1d array to object
  */
  var o = {},
      i = a.length
  ;
  while (i--) {
    o[a[i]] = i;
  }
  return o;
}