1

我正在尝试根据 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 有这个功能吗?我检查了文档 API 无济于事(https://developers.google.com/apps-script/class_document#saveAndClose)。

希望这个不是我担心的那么明显!

提前致谢。

4

2 回答 2

3

当您调用 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 );
于 2015-06-09T10:56:06.453 回答
0

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

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

}
于 2018-08-14T22:12:53.167 回答