我如何复制&将文件移动到Google Apps脚本中的文件夹? [英] How do I copy & move a file to a folder in Google Apps Script?

查看:120
本文介绍了我如何复制&将文件移动到Google Apps脚本中的文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法复制并重命名文件,并将该副本移动到特定文件夹而不在根文件夹中有第二个副本?我已经使用了复制,重命名和按不同顺序移动的组合,但每次我最终都会得到重命名文件的根驱动器副本。这是默认情况下?至少可以说很烦人。

编辑:我看了一下您的原始问题(在编辑之前),然后从那里我了解到您对Google云端硬盘中文件和文件夹的工作方式造成的困惑。

制作文件的副本时,您会在根文件夹中获得一个新文件,当您将此新文件添加到另一个文件夹时,您必须考虑粘贴标签在这个文件上,而不会创建任何新的副本。



您可以在根目录和其他文件夹中看到该文件,但它是完全相同的文件有两个不同的标签!(你注意到,因为你试图删除它,并看到它在两个地方都被删除了)



必须做的是在文件夹中获取新文件,而不是在根目录中显示它以删除根标签。



这就是Google驱动器的工作原理。当你考虑它并与本地硬盘存储进行比较时,它会变得有逻辑性:当你在同一个逻辑驱动器上将文件从一个文件夹移动到另一个文件夹时,不会移动数据(即磁盘上的字节),而只需告诉磁盘操作系统,该文件位于地图上的其他位置。



因此,考虑Google Drive是一个非常大的磁盘单元,其中有数十亿个文件,移动但你可以随心所欲地贴上尽可能多的标签;-)(最重要的标签是你的谷歌账户ID!)

你所描述的情况只是尝试这个简单的函数:

$ p $ 函数copyAndMove(文件,文件夹){
var newfile = file .makeCopy('copy +'file.getName()); //这里你可以按你想要的方式定义副本名称...
newfile.addToFolder(folder); //将副本添加到文件夹
newfile.removeFromFolder(DocsList.getRootFolder()); //并将其从根文件夹中移除

$ / code>

来测试它只需要使用2个必需的参数:文件对象和文件夹对象,你可以从许多不同的方法获得,参见 DocsList文档,了解如何获取它的详细信息,但我想你已经知道了; - )


Is there a way to copy and rename a file and move that copy to a particular folder without having a second copy in the root folder? I have used combinations of copy, rename, move in different order, but each time, I still end up with a copy of the renamed file the root drive. Is this by default? It is annoying to say the least.

解决方案

EDIT : I had a look at your original question (before edit) and from there I understood the confusion you made about how files and folders work in Google Drive.

When you make a copy of a file you get a new file in the root folder, when you add this new file to another folder you have to consider that as sticking a label on this file without creating any new copy of it.

You can actually see the file in both the root and the other folder but it is the very same file with 2 different labels! (and you did notice that since you tried to delete it and saw it was deleted in both places)

The only thing you have to do to get the new file in its folder and not shown in the root it to remove the "root label".

That's how Google drive works. And when you think about it and compare to a local hard disk storage it gets logical : when you move a file from one folder to another on the same logical drive you don't move data (ie the bytes on the disk) but simply tell the disk operating system that this file is somewhere else on the map.

So consider Google Drive as a (very) large disk unit with billions on files that you can't move but on which you can stick as many labels as you want ;-) (the most important label being your Google account ID !)

And to play with these labels in the case you describe just try this simple function :

function copyAndMove(file,folder){
var newfile=file.makeCopy('copy of '+file.getName());// here you can define the copy name the way you want...
newfile.addToFolder(folder);//  add the copy to the folder
newfile.removeFromFolder(DocsList.getRootFolder());// and remove it from your root folder
}

to test it just use the 2 required parameters : the file object anf the folder object that you can get from many different methods, see the DocsList documentation for details on how to get it if you need to but I guess you already know that ;-)

这篇关于我如何复制&将文件移动到Google Apps脚本中的文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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