【问题标题】:google apps specify path for created document谷歌应用程序指定创建文档的路径
【发布时间】:2013-01-10 19:42:19
【问题描述】:

我正在尝试根据 Google 应用中的一些用户信息生成文档。

我有类似以下代码(简化):

var titleAndPath = "./some/other/path/bar.doc"
var info = "foo";
var currentDoc = DocumentApp.create(titleAndPath);
var title = currentDoc.appendParagraph(info);

但是,除了“根”Google Drive 目录之外,我实际上似乎无法保存任何内容。换句话说,我想将文档保存在某个子文件夹中。 google API 有这个功能吗?我检查了 Document API 无济于事(https://developers.google.com/apps-script/class_document#saveAndClose)。

希望这个没有我担心的那么明显!

提前致谢。

【问题讨论】:

    标签: javascript path google-apps-script document


    【解决方案1】:

    当您调用 DocumentApp.create 时,路径的概念不存在,即斜线和点被解释为文件名中的另一个字符。您需要做的是将DriveApp 类与 DocumentApp 类一起使用。如下所示(未经测试):

    var title = "bar.doc";
    var doc = DocumentApp.create ( title );
    var docId = doc.getId ();                     // retrieve the document unique id
    var docFile = DriveApp.getFileById ( docId ); // then get the corresponding file
    // Use the DriveApp folder creation and navigation mechanisms either:
    // - to get to your existing folder, or
    // - to create the folder path you need.
    var folder = …
    folder.addFile ( docFile );
    

    【讨论】:

      【解决方案2】:

      进一步充实上面的答案。您需要使用 DriveApp 类来获取文件夹迭代器,然后在迭代器中获取实际的文件夹(应该是唯一的项目)。见here

      var title = "bar.doc";
        var folderTitle = "GoogleApplicationScriptTesting" ;
        var doc = DocumentApp.create ( title );
        var docId = doc.getId ();                     // retrieve the document unique id
        var docFile = DriveApp.getFileById ( docId ); // then get the corresponding file
      
      
        // Use the DriveApp folder creation and navigation mechanisms either:
        // getFoldersByName returns a folderIterator so you need to do additional checking
      
      
       var folderIter = DriveApp.getFoldersByName(folderTitle);
      
      
        if(folderIter.hasNext()) {
          Logger.log('Folder already exists');
          var folder = folders.next();
        } 
      
        else{
          var folder = DriveApp.createFolder(folderTitle);
          Logger.log('New folder created!');
        }
        folder.addFile ( docFile );
      
      }
      

      【讨论】:

      • 不要忘记从根文件夹中删除...除非您想将其保留在“我的云端硬盘”和所需文件夹中
      • 创建文件夹部分只创建一个文件夹,要创建具有多个子文件夹级别的完整路径,脚本会复杂得多。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-31
      相关资源
      最近更新 更多