如何在活动目录中创建文件

时间:2018-08-06 10:22:31

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

我遇到的问题是该文件是在根目录中创建的,而我找不到正确的指令来更改为命名目录!

 // Create a new Spreadsheet and copy the current sheet into it.
 var newSpreadsheet = SpreadsheetApp.create( invNo );
    var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
       invtoSend.copyTo(newSpreadsheet);

1 个答案:

答案 0 :(得分:0)

只需创建spreadSheet,获取其ID,然后使用DriveApp将其移动(并将其从根目录中删除,否则它将同时可用!)

 var newSpreadsheet = SpreadsheetApp.create( invNo );
 var file = DriveApp.getFileById(newSpreadsheet.getId());
 DriveApp.getFolderById('0B3°°°°°°°1lXWkk').addFile( file );
 DriveApp.getRootFolder().removeFile(file);

编辑

我意识到您的问题可以有不同的理解:您可能希望目标文件夹为“当前电子表格”文件夹,在这种情况下,这里是适当的代码:

function xxxx(){
  var newSpreadsheet = SpreadsheetApp.create( "invNo" );
  var file = DriveApp.getFileById(newSpreadsheet.getId());
  var folders = DriveApp.getFileById(SpreadsheetApp.getActiveSpreadsheet().getId()).getParents();
  while(folders.hasNext()){ // a file might be in multiple folders, include it in all of them except root
    var folder = folders.next();
    if(folder.getName()!=DriveApp.getRootFolder().getName()){  // root folder name changes with user's locale
      folder.addFile(file);
      DriveApp.getRootFolder().removeFile(file);
      Logger.log('destination folder name = '+folder.getName());
    }
  }
}