使用 Google Script 移动 Google Drive 中的文件 [英] Moving Files In Google Drive Using Google Script

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

问题描述

我正在尝试使用通过 Google 表单发布的信息创建文档,然后在创建文档后,我想将文档移动到共享文件夹中供人们查看.

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.

目前,我的脚本从 Google 表单链接的电子表格中获取所有信息.

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

这是在我的 Google Drive 根文件夹中成功创建文档,但我似乎无法将它移动到我想要移动到的位置.

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.

我看到很多帖子建议使用诸如 targetFolder.addFile(newDoc) 之类的东西,但这不起作用,同样我也看到过类似 newDoc.addToFolder(targetFolder) 之类的示例,但这又对我不起作用.

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.

似乎人们已经提出的所有在线问题都在使用不再适用的以前的 API 版本,并且这些方法不适用于新的 DriveApp 功能.

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

推荐答案

如果我们复制文件并将原始文件丢弃,则会更改文件 URL,并且文件共享设置也不会保留.

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.

p>

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

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);
  var folder = DriveApp.getFolderById(targetFolderId);
  file.moveTo(folder);
}

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

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