Google Apps脚本:如何将文件从根目录保存到特定文件夹? [英] Google Apps Scripts: How to save files from root to a specific folder?

查看:129
本文介绍了Google Apps脚本:如何将文件从根目录保存到特定文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



所以,当我运行这个函数时:

 函数importXLSX(){
var files = DriveApp.getFolderById('0B5HXuvZIdqQ3bWlKNXFXZzhIcmc')。searchFiles('title contains.xlsx');
while(files.hasNext()){
var xFile = files.next();
var name = xFile.getName();
if(name.indexOf('。xlsx')){
var ID = xFile.getId();
var xBlob = xFile.getBlob();
var newFile = {title:name +'_ converted',
key:ID,
}
file = Drive.Files.insert(newFile,xBlob,{
convert :true
});



$ / code $ / pre

驱动器的根目录,但我喜欢这个



运行代码时,文件保存在根文件夹中,但我希望它们保存到特定的文件夹,像Converteds一样,所以如果有人知道如何解决它,我将非常感激!



这是一个关于函数的引用链接:将文件夹中可用的所有xls文件转换为< Google Doc Spreadsheets> ;?



感谢!

解决方案



我添加了父母:[{id :###文件夹ID ###}] newFile 。请将 ###文件夹ID ### 更改为特定文件夹并尝试此操作。

已修改脚本:

 函数importXLSX(){
var files = DriveApp.getFolderById('0B5HXuvZIdqQ3bWlKNXFXZzhIcmc') .searchFiles('标题包含.xlsx');
while(files.hasNext()){
var xFile = files.next();
var name = xFile.getName();
if(name.indexOf('。xlsx')){
var ID = xFile.getId();
var xBlob = xFile.getBlob();
var newFile = {
title:name +'_ converted',
key:ID,
parents:[{id:###文件夹ID ###} ]
}
file = Drive.Files.insert(newFile,xBlob,{convert:true});





如果你想检索文件夹来自文件夹名称的ID。请使用以下脚本。

  var folderId = DriveApp.getFoldersByName(###文件夹名称###)。next ).getId(); 

如果我误解了您的问题,我很抱歉。

I'm starting to learn google apps script and suffering a little, :S.

So, when I run this function:

function importXLSX(){
  var files = DriveApp.getFolderById('0B5HXuvZIdqQ3bWlKNXFXZzhIcmc').searchFiles('title contains ".xlsx"');
    while(files.hasNext()){
      var xFile = files.next();
      var name = xFile.getName();
         if (name.indexOf('.xlsx')){ 
            var ID = xFile.getId();
            var xBlob = xFile.getBlob();
            var newFile = { title : name+'_converted',
                 key : ID,
            }
            file = Drive.Files.insert(newFile, xBlob, {
            convert: true
            });
         }
       }
    }

The files converteds are set in Drive's root, but I´d like that

When I run the code, the files are saved in the root folder but I would like them to be saved to a specific folder, like "Converteds", so if anyone knows how to solve it, I would be very grateful!

This is a reference link about the function: Convert all xls files available in a folder into "Google Doc Spreadsheets"?

Thanks!

解决方案

Since you use Drive API, you can directly create a file to the specific folder.

I added parents: [{"id": "### Folder ID ###"}] to newFile. Please change ### Folder ID ### to the specific folder and try this.

Modified script :

function importXLSX(){
  var files = DriveApp.getFolderById('0B5HXuvZIdqQ3bWlKNXFXZzhIcmc').searchFiles('title contains ".xlsx"');
  while(files.hasNext()) {
    var xFile = files.next();
    var name = xFile.getName();
    if (name.indexOf('.xlsx')) { 
      var ID = xFile.getId();
      var xBlob = xFile.getBlob();
      var newFile = {
        title : name+'_converted',
        key : ID,
        parents: [{"id": "### Folder ID ###"}]
      }
      file = Drive.Files.insert(newFile, xBlob, {convert: true});
    }
  }
}

If you want to retrieve folder ID from folder name. Please use following script.

var folderId = DriveApp.getFoldersByName("### Folder name ###").next().getId();

If I misunderstand your question, I'm sorry.

这篇关于Google Apps脚本:如何将文件从根目录保存到特定文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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