简体   繁体   English

Google文件指令码addToFolder

[英]Google Docs Scripts addToFolder

I'm attempting to use Google Docs' ability to run scripts to create the necessary folder structure for new clients (to upload content/images into). 我正在尝试使用Google文档的功能来运行脚本来为新客户端创建必要的文件夹结构(将内容/图像上传到其中)。 Here's what I have thusfar: 这是我到目前为止的内容:

/**
 * 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);

};

Now this is not working ( TypeError: Cannot call method "createFolder" of undefined. (line 7) ), but I'm having trouble figuring out how to use the Folder class within DocList . 现在,这不起作用( TypeError: Cannot call method "createFolder" of undefined. (line 7) ),但是我在弄清楚如何在DocList使用Folder类时遇到了麻烦。 I saw that DocsList has a createFolder method, that I could use like: 我看到DocsList有一个createFolder方法,可以这样使用:

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

but I'm trying to start off with a parent folder, called Clients (already in Google Docs) and then create the following structure: 但我尝试从一个名为Clients (已在Google Docs中)的父文件夹开始,然后创建以下结构:

Clients
    Client Name
        Content
        Images

Ideally I could run this script, but pass in a variable for Client Name to actually create the client name, but I haven't found much help from the docs. 理想情况下,我可以运行此脚本,但是为Client Name传入一个变量以实际创建客户端名称,但是我没有从文档中找到太多帮助。 Any suggestions? 有什么建议么? Thanks! 谢谢!

Here is an example of how it works, see comments : 这是它如何工作的示例,请参阅注释:

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

to test this function simply use something like this : 要测试此功能,只需使用如下代码:

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

note : to get the ID of your folder, take the value right behind folders/ in the url of the folder. 注意: 要获取文件夹的ID,请在文件夹url中的folder /后面加上值。 Example in bold : https://drive.google.com/?hl=fr&tab=wo#folders/ 0B3qSFxxxxxxxxxdsMTFZMDQ 粗体示例https: //drive.google.com/ ?hl= fr & tab = wo#folders/ 0B3qSFxxxxxxxxxdsMTFZMDQ

The sequence might be much longer if you have more folder levels... but the structure is always the same and is unique for every folder. 如果您具有更多文件夹级别,则顺序可能会更长……但是结构始终相同,并且每个文件夹都是唯一的。

Here's a function and some code I wrote which might help you. 这是一个函数和一些我编写的代码,可能会对您有所帮助。 It uses a sub function to see if the folder already exists and if it doesn't makes it. 它使用一个子函数来查看文件夹是否已经存在以及是否不存在。 If the folder does already exist it returns that object which helps with chaining ( referenced here on how it is used ): 如果该文件夹已经存在,它将返回该对象以帮助进行链接( 有关如何使用,请参见此处 ):

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

I know the question/answer is 3 years old and it was correct earlier, but this solution is no more working correctly with Google Apps script as on 2015, current easiest way to create a subfolder on a specific folder is, 我知道这个问题/答案已有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;
}

pass the parent folder id with the sub folder name you want to create, the Folder class has the method createFolder to create a folder under it which returns the created subfolder's object. 传递父文件夹ID以及要创建的子文件夹名称,Folder类使用createFolder方法在其下创建一个文件夹,该文件夹返回创建的子文件夹的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM