javascript - 谷歌应用指定创建文档的路径

标签 javascript path google-apps-script document

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

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

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

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

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

提前致谢。

最佳答案

当您调用 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 );

关于javascript - 谷歌应用指定创建文档的路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14265970/

相关文章:

javascript - 如何将 onFormSubmit(e) 和 onEdit(e) 嵌套在一起

google-apps-script - 如何将页面(模式对话框)与其同级页面(侧边栏)进行通信?

javascript - 更新数组中一个对象的值

python - 如何在X*Y网格中找到最短路径

winapi - Windows内核中最大可能的文件名长度

linux - UNIX - 简单 - 什么是相对于主目录的路径名?

regex - 是否可以在 CalendarApp.getEvents() 中使用带有搜索参数的正则表达式?

javascript - 设置配置?

javascript - 为 <div> 设置固定的高度和宽度

javascript - 自定义动态创建的元素,添加通过弹出窗口选择的选项