谷歌文档脚本addToFolder



我正试图利用谷歌文档运行脚本的能力为新客户端创建必要的文件夹结构(将内容/图像上传到)。以下是我的简历:

/**
 * 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)),但我很难弄清楚如何在DocList中使用Folder类。我看到DocsList有一个createFolder方法,我可以使用它,比如:

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

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

Clients
    Client Name
        Content
        Images

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

以下是它的工作方式示例,请参阅注释:

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中的folders/后面获取值。bold中的示例:https://drive.google.com/?hl=fr&tab=两个文件夹/0B3qSFxxxxxxxxxdsMTFZMDQ

如果你有更多的文件夹级别,序列可能会更长。。。但是结构总是相同的,并且对于每个文件夹都是唯一的。

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

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; 
   } 
}

我知道这个问题/答案已经有3年的历史了,以前是正确的,但这个解决方案不再像2015年那样与谷歌应用程序脚本一起正确工作,目前在特定文件夹上创建子文件夹的最简单方法是

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来在其下创建一个文件夹,该文件夹返回创建的子子文件夹的对象。

相关内容

  • 没有找到相关文章

最新更新