且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Google Apps脚本:如何将文件从根目录保存到特定文件夹?

更新时间:2023-11-25 14:10:58



我添加了父母:[{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.