0

我正在尝试使用 Google Docs 运行脚本的能力为新客户创建必要的文件夹结构(将内容/图像上传到)。到目前为止,这是我所拥有的:

/**
 * This script creates the necessary folder structure for a new client
 */
function newClientSetup() {

  var initial = DocsList.getFolder("Clients");
  var client = DocsList.addtoFolder.createFolder("Client Name");
  var file = DocsList.addtoFolder(client);

};

现在这不起作用(TypeError: Cannot call method "createFolder" of undefined. (line 7)),但我无法弄清楚如何FolderDocList. 我看到DocsList有一个createFolder方法,我可以像这样使用:

var folder = DocsList.createFolder("Folder Name");

但我试图从一个名为Clients(已在 Google Docs 中)的父文件夹开始,然后创建以下结构:

Clients
    Client Name
        Content
        Images

理想情况下,我可以运行此脚本,但传入一个变量Client Name以实际创建客户端名称,但我没有从文档中找到太多帮助。有什么建议么?谢谢!

4

3 回答 3

3

这是它如何工作的示例,请参阅评论:

function createSubFolder(subfolder) { // the argument is the name of the folder you want to create
    var parentfolder = DocsList.getFolder('Clients'); //begin in the client folder (you could also open by Id, I prefer the ID as I find it more failsafe (ID are unique, names aren't necessarily 
    var newFolder = DocsList.createFolder(subfolder); // create the new subfolder from the argument of the function
    newFolder.addToFolder(parentfolder);// add the newly created folder to 'Clients'
}   

要测试此功能,只需使用以下内容:

function test(){
    createSubFolder('test');// this will create a new folder called test in your Clients folder
}

注意:要获取文件夹的 ID,请在文件夹 url 中的 folder/ 后面取值。粗体示例https: //drive.google.com/?hl=fr&tab=wo#folders/ 0B3qSFxxxxxxxxxdsMTFZMDQ

如果您有更多文件夹级别,则序列可能会更长......但结构始终相同,并且每个文件夹都是唯一的。

于 2012-07-19T00:33:07.903 回答
2

这是我编写的一个函数和一些代码,可能会对您有所帮助。它使用子函数来查看文件夹是否已经存在以及是否不存在。如果该文件夹确实存在,它会返回有助于链接的对象(在此处引用它的使用方式):

function newClientSetup() {
   var ROOT_FOLDER = "Clients";
   var CLIENTNAME_FOLDER = "Client Names";

   // get a the system route folder (if it deosn't existing make it 
   var rootFolder = folderMakeReturn(ROOT_FOLDER); 
   // create/get draft and release folders 
   var clientNamesFolder = folderMakeReturn(CLIENTNAME_FOLDER,rootFolder, ROOT_FOLDER+"/"+CLIENTNAME_FOLDER);   
}

// function to see if folder exists in DocList and returns it 
// (optional - if it doesn't exist then makes it) 
function folderMakeReturn(folderName,optFolder,optFolderPath){ 
   try { 
      if (optFolderPath != undefined){ 
         var folder = DocsList.getFolder(optFolderPath); 
      } else { 
         var folder = DocsList.getFolder(folderName); 
      } 
      return folder; 
   } catch(e) { 
      if (optFolder == undefined) { 
         var folder = DocsList.createFolder(folderName); 
      } else { 
         var folder = optFolder.createFolder(folderName); 
      } 
      return folder; 
   } 
}
于 2012-07-19T09:46:06.590 回答
0

我知道问题/答案已有 3 年历史,而且之前是正确的,但是这个解决方案不再像 2015 年那样与 Google Apps 脚本一起正常工作,目前在特定文件夹上创建子文件夹的最简单方法是,

function createSubFolder(parentId, folderName)
{
  var id="";
  try
  {
    var parent=DriveApp.getFolderById(parentId);   // get parent folder
    var folder =parent.createFolder(folderName);   // create sub folder 
    id=folder.getId();                             // get the subfolder id to return
  }
  catch(e)
  {
  }
  return id;
}

使用您要创建的子文件夹名称传递父文件夹 ID,Folder 类具有方法 createFolder 在其下创建一个文件夹,该文件夹返回创建的子文件夹的对象。

于 2015-05-01T08:49:07.457 回答