使用Google脚本在Google云端硬盘中移动文件 [英] Moving Files In Google Drive Using Google Script

查看:109
本文介绍了使用Google脚本在Google云端硬盘中移动文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用通过Google表单发布的信息创建文档,然后一旦创建文档,我想将文档移动到共享文件夹供人们查看。



目前,我已经将脚本从Google窗体链接的电子表格中获取所有信息。



使用这些信息,我使用下面的代码来创建文档:

  var targetFolder = DriveApp.getFolderById(TARGET_FOLDER_ID); 
var newDoc = DocumentApp.create(requestID + - + requestSummary);

这是在我的Google云端硬盘根文件夹中成功创建文档,但似乎无法移动它是我想要移动到的位置。

我见过很多帖子,建议使用像targetFolder.addFile(newDoc)这样的东西,但这不起作用,同样我见过像newDoc.addToFolder (targetFolder),但这又不适用于我。



看起来,人们已经询问过的所有在线问题都是使用以前的API版本适用时间更长,这些方法不适用于新的DriveApp功能。

如果可能的话,我想要创建如上所述的新文档,以便我可以使用脚本编辑内容,然后将该文件移动到一个共享文件夹。 (据我所知,目前没有移动功能,因此复制并删除旧的功能就足够了)。 解决方案

如果我们复制了文件并将原始文件删除,则会更改文件网址,并且文件共享设置也不会保留。



在驱动器,可以将文件添加到多个文件夹使用DriveApp服务的.addFolder()方法。您可以将文件添加到目标文件夹,然后从直接父文件夹中删除文件。

  function moveFiles(sourceFileId,targetFolderId ){
var file = DriveApp.getFileById(sourceFileId);
file.getParents()。next()。removeFile(file);
DriveApp.getFolderById(targetFolderId).addFile(file);
}


I'm trying to create documents using information posted through Google forms, then once the document is created I would like to move the document into a shared folder for people to view.

At the moment I have the script taking all of the information from the Google Forms linked spreadsheet.

Using that information I'm using the following code to create the document:

  var targetFolder = DriveApp.getFolderById(TARGET_FOLDER_ID);
  var newDoc = DocumentApp.create(requestID + " - " + requestSummary);

This is creating the document successfully in my Google Drive root folder, but I can't seem to move it where I want to move it to.

I've seen a lot of posts suggesting use stuff like targetFolder.addFile(newDoc) but that doesn't work, similarly I've seen examples like newDoc.addToFolder(targetFolder) but again this isn't working for me.

It seems that all the online questions people have already asked about this are using the previous API versions that are no longer applicable and these methods do not apply to the new DriveApp functionality.

What I would like, if possible, is to create the new document as above so that I can edit the contents using the script, then be able to move that file to a shared folder. (From what I understand there is no 'move' function at present, so making a copy and deleting the old one will suffice).

解决方案

If we make a copy of the file and trash the original, it would change the file URL and also the file sharing settings won't be preserved.

In Drive, it is possible to add a file to multiple folders with the .addFolder() method of DriveApp service. You can add the file to the target folder and then remove the file from the immediate parent folder.

function moveFiles(sourceFileId, targetFolderId) {
  var file = DriveApp.getFileById(sourceFileId);
  file.getParents().next().removeFile(file);
  DriveApp.getFolderById(targetFolderId).addFile(file);
}

这篇关于使用Google脚本在Google云端硬盘中移动文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆